Followers 0

# Programmers: Your favorite interview questions

## 115 posts in this topic

And reading the coding challenges from majestic. I decided to try the Integer one. This was how I did it in C#, 3 lines of code for swapping. Not sure if it will always work, but it does for me.

`			int a = 100;			int b = 200;			//Point is to make a == b and b == a			Console.WriteLine('\n' + a.ToString());			Console.WriteLine(b.ToString());			b += a;			a = b - a;			b -= a;			Console.WriteLine(a.ToString());			Console.WriteLine(b.ToString());[/CODE]`

It's probably worth mentioning why that won't always work... what happens if a = MAX_INT - 2 and b = MAX_INT - 3? The addition of a and b will overflow...

Can you think of another operator you can use?

##### Share on other sites

I no longer ask technical questions because we aren't allowed to do face to face interviews and over the phone, any one can do fairly well so I base my judgement on the applicants technical inquiries.

##### Share on other sites

It's probably worth mentioning why that won't always work... what happens if a = MAX_INT - 2 and b = MAX_INT - 3? The addition of a and b will overflow...

Can you think of another operator you can use?

There's a cute trick with the xor operator but I don't see any value in testing if the candidate knows about it. If he can find it on the spot then that's certainly impressive, but is it really the kind of skills you're looking for? On modern CPUs this is even counter-productive compared to using a temporary variable, so it's a typical example of premature and misguided "optimization". I view this type of question as outdated and as an interviewee it'd raise doubts in my mind about the company.
1 person likes this

##### Share on other sites

I no longer ask technical questions because we aren't allowed to do face to face interviews and over the phone, any one can do fairly well so I base my judgement on the applicants technical inquiries.

Wow, really? I would struggle to employ anyone I haven't met face to face. Just out of interest, how many of the candidates you employ turn out to be a waste of space?

##### Share on other sites

As already mentioned by a few of the people above, FizzBuzz (well, a slight variation on the FizzBuzz question) is probably my favourite. While it doesn't find "good" programmers it does well to quickly weed out the 80-90%+ of applicants applying for programming jobs who can't code at all. At work we always ask it before the proper interview, if the applicant can't do it with and without modulo in a reasonable time we don't even bother with the full interview.

##### Share on other sites

if you can't do a modulus program..... and 99.5% don't get it? seriously?!

There's a cute trick with the xor operator

I haven't really used modulus for anything besides rand(), and to be honest I would be tripped up on a modulus question. XOR operators I have used, and do use on a regular basis, but the mechanics behind xor I would only consider asking if I'm hiring for a high level position that has a lot of technical work behind it that would benefit from knowing how xor stuff works(e.g like a video codec). I try to steer clear of nitty gritty stuff like this because people are coming in knowing there competing against a lot of people so they come in scared.

I have been doing programming now for 17 years and professionally for 10, it really wasn't until I went to college and talked to people in the game industry that I was able to land my first gig. I'm entirely self taught(I really just went to college to do the meet and greet stuff), and I remember when I was first out of high school I got a dry interview at Square Enix. The guy asked me what was global scope and local scope; I knew what scope was but never heard that term before. Needless to say I botched up the interview, but now that I actually have to do interviews I try to take that experience into account.

Their are a lot of veterans in the game industry(my field anyway) who are entirely self taught like myself that haven't heard various terms like scope(until someone brings it up) but they know what it is. This is why I try not to be a term nazi and if someone doesn't know something, I give them the chance to look it up. If they see the term and go drr and from there look away from the screen, and tell me exactly what it is I'm just fine with that. But you would be surpised on how many college graduates can't look stuff up, and I know for a fact they probably haven't done some parts of what I need them to do(just becuase a lot of game programming stuff isn't taught in any school), and if they can't figure it out I can't hold someones hand through the work.

While it doesn't find "good" programmers it does well to quickly weed out the 80-90%+ of applicants applying for programming jobs who can't code at all.

Again it finds programmers that paid attention in school, doesn't mean they have good logic, just good retention :p.

"C++/CLI should be thought of as a language of its own (with a new set of keywords, for example), instead of the C++ superset-oriented Managed C++ (MC++) (whose non-standard keywords were styled like __gc or __value)." http://en.wikipedia....iki/C%2B%2B/CLI

C++/CLI has its own ECMA standard distinct from the ISO C++ standard, it is not designed by the same comittee, it has a different syntax, a different compiler, it compiles for a different platform.

CLI I don't think is used enough and have enough practical usage to be taught as its own course. Personally, I wish CLI would just go away, it really clusters up a codebase :/.

if the applicant can't do it with and without modulo in a reasonable time we don't even bother with the full interview.

To each his own, but I personally like the steer away from stuff like that because as I said just above that shows more retention rather than logic, and since a lot of things I need my employess to get done aren't taught in school I really try and group logic testing and ability to research something in one section of the interview. This why I ask someone how to do something I know isn't taught in school, and they can outline and do research on what needs to get done as quickly as possible odds are there pretty logical.

Before that if they know polymorphism, pointers, etc I know they can at least program, so I try not to focus to much on if they can program or not. I want to find someone that can think out side the box. One other question I ask is, since I always look for someone that knows C# and C++, I ask them how would automate hooking up managed and unmanaged code without using CLI or Com.

##### Share on other sites

if else man .. ftw ... then again the most i did was arrays

That's the whole pitfall of FizzBuzz (unless you do duplicate checks, which is usually not wanted)

Given two integer variables, A and B, how could you swap their values without using a third variable? (This is my favourite. It wasn't actually given to me, but to my housemate at the time. He didn't get it, but the interviewer gave it to him to do on the train home, he figured it out on the train and emailed the answer to the interviewer, subsequently got the job :p)

Could you give the solution? Or is the solution language specific, like Python assigment:

`A, B = B, A `

But still, internally I'm sure a temporary variable(s!) is being used.

EDIT: Forgot to add my solution.

`void FizzBuzz(){	for (int num = 1; num  < 101; num ++)	{		bool multiple = false;		if (num % 3 == 0)		{			cout << "Fizz";			multiple = true;		}		if (num % 5 == 0)		{			cout << "Buzz";			multiple = true;		}		if (!multiple)			cout << num;		cout << endl;	}}[/CODE]`

I'm still not sure if it's better to declare a variable outside of the loop, but I'm fairly sure it will be optimized by the compiler.

And my solution is fairly readable (I hope).

##### Share on other sites

Again it finds programmers that paid attention in school, doesn't mean they have good logic, just good retention :p.

I think asking a candidate to solve FizzBang with and without modulus is silly and defeats the point of the exercise. They just need to make sure they produce some code that solves the problem as expressed in plain English through whatever means necessary in a reasonable amount of time without making any of the common mistakes.

That way it does weed out 90% of those people that claim they can code when they can't.

##### Share on other sites

There's a cute trick with the xor operator but I don't see any value in testing if the candidate knows about it. If he can find it on the spot then that's certainly impressive, but is it really the kind of skills you're looking for? On modern CPUs this is even counter-productive compared to using a temporary variable, so it's a typical example of premature and misguided "optimization". I view this type of question as outdated and as an interviewee it'd raise doubts in my mind about the company.

Then I'd be inlined to say you missed the point of the question, which (of course only in my opinion) was to test an applicant's ability to think outside the box to create an alternative solution to a common textbook problem given a specific constraint. In the context of the job being applied for, it may also have been relevant (although I don't know for sure) since the job being applied for was to write software for very specialized hardware, which was very environmentally constrained (something to do with turbochargers IIRC).

Of course, in reality for most systems, saving 1-8 bytes of memory is pointless and complicates a simple problem, so temporary variables would be used. But there are certainly specific cases where optimisation like that is useful, especially in extremely memory constrained systems (embedded systems, for example) where the cost of an extra couple of cycles is worth the bytes saved.

Could you give the solution? Or is the solution language specific, like Python assigment:
`A, B = B, A `

But still, internally I'm sure a temporary variable(s!) is being used.

Firey posted the solution that my housemate came up with. As rfirth said, if the sum of the values would be greater than INT_MAX, then the solution fails, but no specific values were provided, so I think the answer is reasonable. In fact, I'd say that a good follow up quesiton would have been "How would your solution for the question above react to the following test cases. Which cases, if any, would fail?".

##### Share on other sites

I've been asked that question. As soon as you give the solution using modulus, they ask you to do it again without using modulus/multiplication/division.

Only solution I can think of without using modulus/multiplication/division would be to loop subtracting the number each time until the number is less than that number and checking it for 0.

##### Share on other sites

Firey posted the solution that my housemate came up with. As rfirth said, if the sum of the values would be greater than INT_MAX, then the solution fails, but no specific values were provided, so I think the answer is reasonable. In fact, I'd say that a good follow up quesiton would have been "How would your solution for the question above react to the following test cases. Which cases, if any, would fail?".

Ah many thanks, completely missed it! I also remember now that it wasn't that 3 variables are mandatory for a swap, but at least 3 instructions are.

Also, I completely agree on the fact that when hiring programmers for highly constrained systems like a small embedded ARM board, things like a washing machine or even better example, car computer.

Working in such environments would require very safe code and there's very little overhead available. So doing such interview tests will filter out people who most likely won't be able to work on such projects.

##### Share on other sites

Then I'd be inlined to say you missed the point of the question, which (of course only in my opinion) was to test an applicant's ability to think outside the box to create an alternative solution to a common textbook problem given a specific constraint. In the context of the job being applied for, it may also have been relevant (although I don't know for sure) since the job being applied for was to write software for very specialized hardware, which was very environmentally constrained (something to do with turbochargers IIRC).

Of course, in reality for most systems, saving 1-8 bytes of memory is pointless and complicates a simple problem, so temporary variables would be used. But there are certainly specific cases where optimisation like that is useful, especially in extremely memory constrained systems (embedded systems, for example) where the cost of an extra couple of cycles is worth the bytes saved.

If the job involves designing algorithms for unorthodox CPUs, extremely memory constrained systems, or requires solid mastery of bitwise operations, then the question is useful, but either I missed something or no mention was made of the specific job for which this question was asked.

I would be very impressed if someone who has never heard of the trick before were able to find it on the spot in an interview. It's not a trivial algorithm. As far as testing "out-of-the-box" thinking goes, there are much better questions to ask. In general, the xor swap algorithm:

- Is slower than using a temporary variable

- Obfuscates the code

- Doesn't even result in any memory savings as optimizers can optimize a temporary variable away, perform exchange with a single XCHG x86 instruction, etc. See http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice

There's something just wrong about expecting any programmer to know about an obscure algorithm that's generally best avoided in practice.

##### Share on other sites

If the job involves designing algorithms for unorthodox CPUs, extremely memory constrained systems, or requires solid mastery of bitwise operations, then the question is useful, but either I missed something or no mention was made of the specific job for which this question was asked.

I would be very impressed if someone who has never heard of the trick before were able to find it on the spot in an interview. It's not a trivial algorithm. As far as testing "out-of-the-box" thinking goes, there are much better questions to ask. In general, the xor swap algorithm:

- Is slower than using a temporary variable

- Obfuscates the code

- Doesn't even result in any memory savings as optimizers can optimize a temporary variable away, perform exchange with a single XCHG x86 instruction, etc. See http://en.wikipedia....nce_in_practice

There's something just wrong about expecting any programmer to know about an obscure algorithm that's generally best avoided in practice.

Like I said, I expect that the question was posed to see what answers the candidates could come up with, a "thinking outside the box" problem solving aptitude question, rather than a know the spec/processor/application question. A very very elementary question with a twist intended to remove the candidate from their comfort zone to see if they can think creatively. That's why I liked it anyway.

Then again, the job was writing systems (again IIRC) using RPG, and very old, highly constrained systems; so within the context of the job it may well have been relevant. Not every programming job has the luxury of optimizing compilers and x86 instruction sets.

##### Share on other sites

The one I use most often in interviews is "How would you crash the runtime/virtual machine for your favorite language (or the one we're using here)"

Whether you're using Java, C++, Objective C, MSIL, Python, damn near anything that isn't ANSI C is operating in some sort of a runtime environment - knowing where the weaknesses are and how you might exploit them to cause the runtime to crash (rather than your application) is a good test of their knowledge of the tools.

I don't necessarily expect an "this is the step-by-step guide", just a pretty good explanation of where they would go looking. "I know that the VM can leak memory if certain kinds of interproces communication goes wrong - I also know that I can force that communication to go wrong by doing x so I would check the buglist for issues in y and then?" It's the thought process that matters.

##### Share on other sites

I was curious about the FizzBuzz question, so I tried to do something a little more original.

`void fizzBuzz(){	foreach(i; 1 .. 100 + 1) switch((i % 3 == 0) + (i % 5 == 0) * 2)	{		default: writeln(i); break;		case 1: writeln("Fizz"); break;		case 3: write("Fizz");		case 2: writeln("Buzz");	}}[/CODE]`

##### Share on other sites

Dang FizzBuzz. I had to do one once in Java and completed it in like 15 seconds using ternary operators. The most difficult part isnt the code, but rather the mindset of "how in the hell is this going to be applied".

##### Share on other sites

Wow, really? I would struggle to employ anyone I haven't met face to face. Just out of interest, how many of the candidates you employ turn out to be a waste of space?

Oddly enough, letting the applicant do the talking has worked out pretty well. I wish I had body language to read but just listening to them ask questions certainly hasn't resulted in any worse candidates and maybe even a better hiring average. I once had a calc instructor that graded strictly based on the questions a student asked and not on test scores. He believed that the deeper your understanding of something is, the level of the questions you ask will reflect that. Since I was over 30 at the time and already working in the industry I thought that was an interesting way to analyze a persons understanding of something. I've applied that here because I have no choice.

You'd be surprised. If you really understand what a developer candidate NEEDS to do in a position its easy to spot each persons skill levels and who can technically do the job. The problem is technically capable is not really what we look for. Even in previous companies with in person interviews, the most I'll ever really do is maybe a fizzbuzz. The average business application coder isn't doing rocket science. Every team I've been on in the last 16 years has basically done nothing more than schlep data from some DB to a UI and back. Scanning back on this thread, I looked at one of Dr_Asiks questions

"You have a cloud of points in 2d. Design an algorithm to draw the smallest circle that contains all the points."

Nope, haven't needed to analyze 2D points in 16 years, probably don't need a dev that can now. One of the 'trigger' things I may listen for as an example is what tool sets or frameworks they have used in the last year or so and how do they talk about them. Do they just hit the buzz words or do they really talk about them at a deeper level.

In practical terms, what we found to be important for us is:

• Do they have an understanding of the fundamentals of whatever language/s we are using?
• Can they/are they willing to learn?
• Can they interact with end users?
• Can they interact with business owners?
• Are they a prima donna?

If the first four are yes and the last no, then we probably have a winner - assuming they can pass the background and drug test...

##### Share on other sites
Scanning back on this thread, I looked at one of Dr_Asiks questions

"You have a cloud of points in 2d. Design an algorithm to draw the smallest circle that contains all the points."

Nope, haven't needed to analyze 2D points in 16 years, probably don't need a dev that can now.

The question was for a game programming position, where the ability to design algorithms that work with points and vectors in 2D and 3D space is very important. Actually I was tasked to design such an algorithm during my internship there.

Also the answer to that is not particularly difficult:

A circle is defined by a center point and a radius. To find the center point, find Xmax, Xmin, Ymax, Ymin in your cloud of points, and your center is the point in the middle of that (Xmin + ((Xmax - Xmin) / 2), Ymin + ((Ymax - Ymin) / 2)).

Now the radius is the smallest one that ensures the circle contains all the points: it therefore has to be as long as the distance to the furthest away point. The distance from the center to a point is simply the norm of the Point-Center vector. Go over your cloud of points, calculate the distance from the center for each, select the largest. Done.

##### Share on other sites

Here are a couple of somewhat evil questions. I'd never ask them, but these are my favourites from discussions with my colleagues:

1. Given an RBGA pixel, with 8 bits per channel packing into a 32-bit integer, write a function which does saturated addition on two pixels.

2. In Java, write a function which takes a string and returns a string that is the input if the input is 6 characters or less, and the first 3 characters + "..." if it's greater than 6.

The first question is fairly straight forward. The second... that's an exercise for the reader.

##### Share on other sites

Here are a couple of somewhat evil questions. I'd never ask them, but these are my favourites from discussions with my colleagues:

1. Given an RBGA pixel, with 8 bits per channel packing into a 32-bit integer, write a function which does saturated addition on two pixels.

2. In Java, write a function which takes a string and returns a string that is the input if the input is 6 characters or less, and the first 3 characters + "..." if it's greater than 6.

The first question is fairly straight forward. The second... that's an exercise for the reader.

The second one isn't that hard... is it?

Pseudocode

```String Foo(String bar)
{
if (bar.Length &lt;= 6)
return bar

return bar.Substring(0, 3) + "..."
}
```

##### Share on other sites

MrA really? lol

`String Shortten(String text) {  return text.Length <= 6 ? text : text.Substring(0, 3) + "...";}[/CODE]`

##### Share on other sites

MrA really? lol

`String Shortten(String text) {  return text.Length <= 6 ? text : text.Substring(0, 3) + "...";}[/CODE]`

Hehe. I guess I wasn't clear. It's a trick question (and arguably, poorly defined), and according to my friend, no-one's been able to answer it. I certainly couldn't answer it, but at least I understand why I can't answer it.

##### Share on other sites

Hehe. I guess I wasn't clear. It's a trick question (and arguably, poorly defined), and according to my friend, no-one's been able to answer it. I certainly couldn't answer it, but at least I understand why I can't answer it.

Huh? How is it a trick question? Seems extremely straight forward... what are we missing here?

##### Share on other sites

The question was for a game programming position, where the ability to design algorithms that work with points and vectors in 2D and 3D space is very important. Actually I was tasked to design such an algorithm during my internship there.

Also the answer to that is not particularly difficult:

A circle is defined by a center point and a radius. To find the center point, find Xmax, Xmin, Ymax, Ymin in your cloud of points, and your center is the point in the middle of that (Xmin + ((Xmax - Xmin) / 2), Ymin + ((Ymax - Ymin) / 2)).

Now the radius is the smallest one that ensures the circle contains all the points: it therefore has to be as long as the distance to the furthest away point. The distance from the center to a point is simply the norm of the Point-Center vector. Go over your cloud of points, calculate the distance from the center for each, select the largest. Done.

Actually, the problem is considerably harder than that. How do you know that aabb center is always the center of the smallest circle? It's not clear that there's a relationship between the two. Here's an example:

The blue points are the point cloud, and the white subdivided box is their aabb (note the blue point in the upper-right corner). The red circle is the one described by your algorithm with red dot as center; the green circle with green dot as center is smaller (possibly the smallest) and still covers all points. Anyway, this problem is called the minimum bounding circle, and there are a lot of ways to approach it but it is basically an optimization problem.

##### Share on other sites
Anyway, this problem is called the minimum bounding circle, and there are a lot of ways to approach it but it is basically an optimization problem.
I hadn't thought of that, so thanks for the explanation. That said, now that I think about it, I don't think they mentionned it had to be smallest possible circle during the interview. That would've been kinda hard for an internship position. :p

## Create an account

Register a new account

Followers 0