Swap Two Integers Without a Temporary Variable
Good programming question that we were just discussing today. Wikipedia has the solution
Good programming question that we were just discussing today. Wikipedia has the solution
Fred * const ptr = NULL;
const Fred * ptr = NULL;
Q: Do these lines have the same meaning?
A: No. The first line declares a const pointer meaning the value of ptr cannot be reassigned. The second line means that the Fred object itself is const and it cannot be altered unless it has a mutable member declared (mutable members can be altered in const functions)
A lot of people no doubt miss this one. Read bout const correctness here
One really good question I’ve seen interviewers ask is a basic math question dealing with number sets. Just to trip up the applicant just a little, they ask in reverse order.
If you have a number range {10….6} please find the number that is 10% within this number range.
This is actually a very good question to determine basic math skills. Dealing with Number sets might be used for Targeting gameplay systems whereas the joystick value is used to sample a number within a number set (for instance, the number set is our set threshold that we are sampling a value from).
Anyway the solution is simple.
First determine the difference between [x,y] where x=10 and y = 6
size = 4 (10 - 6 = 4)
This is the size of our entire boundary. Now, multiply the size of our number range by z (z = 10%)
result = size * z;(result = 4 x 0.10 = 0.4)
Next, take x and subtract the result to get our answer.
solution = x - result;
solution = 10 - 0.4 = 9.6
Ericson brings up an interesting point on Bit Manipulation. Yep, it’s a very important skill to have and it’s not a bad idea to keep reviewing at the very least, the basics. I’m linking this article more for my own selfish reasons though. Many interviews will contain at least one Bit Manipulation question so this looks like a very good read. I have to go to bed so only made it halfway through atm. So will have to review it more tomorrow when I’m fully rested
Code Sample: Visual Studio 2005 Solution and .cpp
This is a pretty common interview question to ask C++ programmers. The key to solving this one is to write a nested loop. The outer loop iterates through the nodes. The inner loop checks each node’s next pointer member for corruption (node that points to a previous one in the list).
Code Sample: Visual Studio 2003 solution (.cpp included)
The trick to solving this problem is to use Recursion to iteratively search through each branch of our binary tree and tally up how many nodes each node contains. Once you realize you need to take one parameter (depth) and return the maximum size for each branch from “each” node then you’re on the right track. So, the main trick you must do is at each individual node, examine the child, left, and right sibling. Store the number of nodes each branch reports back and store it off. Later, compare all 3 results to find the best number then return this maximum depth you discovered back to the caller.
I can’t recall if I’ve seen a Binary Tree blow the Calling Stack on a small memory machine offhand but I’ve seen other complex recursion routines blow through the Call Stack. So if you’re ever asked why Recursion can possibly be bad is because of limited memory assigned to the Call Stack.
Code Sample: Visual Studio 2003 solution and .cpp
This is another pretty good interview question that often gets asked. It can’t hurt to already know what a Palindrome is before you get asked this question. Palindromes are words that are symmetrical somewhat. A word like DOOD or TAT can be flipped and mean the same thing. Yeah, I know I just made up those words but hopefully you get the gist. This example makes good use of Asserts and pointer arithmetic.
I’ve been in the game industry for a few years now. I’ve been on the receiving end (getting interviewed) and on the attacking end (the interrogator). Most game industry interviews focus on knowledge of C and C++ however it is still helpful to know how to deal with Number Ranges, Vectors (Cross, Dot product), and good ole fashioned problem solving.
The link below is really good it contains many questions you might get asked:
http://vijayinterviewquestions.blogspot.com/
Off hand, I want to say at every single interview I’ve been asked at least one Bit Manipulation question, one question from the link above, perhaps a pointer question, smart pointers, etc.
Others asked about preincrement operators and how they differ from post increment operators, Palindromes, detecting a corrupt node in a Linked List, differences between C and C++, recursive functions, bit operators, multiply a number by 8 without using mult/divide, etc. All of this is pretty much covered by the link above. Soon in the future I’ll give my own solutions for fun
Coding Horror has some good articles on phone interview questions (those aren’t game industry specific however). Keep in mind most game companies stick to C / C++.
Another pretty good subject is called ‘Bit Twiddling Hacks’. If you Goggle for this topic all sorts of interesting links will popup. These questions tend to be stuff I’d probably never ask a candidate but still they are probably very handy to know. I’d prioritize this as tricky, fringe stuff though thats just like handy to know. I’d prioritze the questions covered at the link above much higher (like Palindromes, etc)
Here is one interesting link for example:
http://graphics.stanford.edu/~seander/bithacks.html
Here’s a good example:
unsigned int v; // we want to see if v is a power of 2 bool f; // the result goes here f = (v & (v - 1)) == 0;
Design Patterns is something that is asked at almost every interview too. This link provides a very nice list.
Code Sample: Visual Studio 2005 Solution, .cpp code
I was asked this one once at a job interview for a Junior programming position many years ago. Usually such a thing is a concern for the Lead Engineer or the Low Level Optimization Engineers that are tasked with optimizing code to achieve ideal performance. For PC architecture, usually it is SIMD operands that require the 16 byte aligned structures. On the playstation 2, I believe this was a concern for the Vector Unit programmers.
Nowadays we have the GPU, a specialized graphics unit that automatically takes care of memory alignment as long as you use a High Level language for graphics rendering (like HLSL). So, even a graphics programmer does not have to deal with such things these days unless you bypass the high level shading language and use ASM (assembly). I wrote a shader or two in ASM for the GPU back in the day back when it was believed HLSL did not compile optimal code.
This might be a great question to ask to help determine how experienced a programmer is at low level programming. But even if the programmer fails this question he can still be an excellent programmer yet- just not have been exposed to code at this low level before. At some development studios, there is usually just 1 or 2 ‘hitman’ that focus on code optimization (write code first, optimization last). I have heard of other studios that ‘write code and optimize first’ which is probably the stronger methodology. For heavy Matrix ops it can be desirable to of course employ 16 byte aligned Matrix structures. On some consoles, you might actually be required to allocate byte aligned memory and fire it off to the chip so for these positions an understanding of byte alignment is crucial.
Anyway, Microsoft has some good information on this if you are really interested. And the solution boils down to this:
offset % alignment-requirement = 0
Our offset is our pointer. Our ‘alignment-requirement’ is the alignment (4,8,16, etc). If the result is equal, then our data is properly aligned.
Recent Comments