Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Interview Questions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
25 replies to this topic

#1 Darkbouncer4689   Members   -  Reputation: 110

Like
1Likes
Like

Posted 04 October 2011 - 09:24 PM

Hey all,

Anyone of us who has interviewed for a game company (or any company in general) that focuses on C++ programming has probably had the "on the spot C++ proficiency test" in which they ask you to write some C++ program in front of the team.

This thread is in hopes that people will post what types of C++ questions or programs they have been asked in an interview. My hope is to practice a wide variety of potential questions so I won't look stupid in front of a potential employer. I'll start things off.

In an interview for a game company I was asked to write a function that will reverse a string without using the string class (i.e. use a null terminated char array).

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12522

Like
2Likes
Like

Posted 04 October 2011 - 09:37 PM

Here's a few I've seen asked:
* Reverse a linked list.
* Test if a number is prime.
* Binary search.
* Compute the median of an array of integers.

I think Project Euler is a very good way to practice writing little programs that do clever things.

#3 CRYP7IK   Members   -  Reputation: 865

Like
3Likes
Like

Posted 04 October 2011 - 09:47 PM

Here is one I have had multiple times, although not phrased exactly the same way:

  • Implement an RLE Compression algorithm (using either c-style string or std::string as input\output, this would depend on the interviewer they may even let you pick)

To accomplish great things we must first dream, then visualize, then plan...believe... act! - Alfred A. Montapert
Gold Coast Studio Manager, Lead Programmer and IT Admin at Valhalla Studios Bifrost.

#4 Darkbouncer4689   Members   -  Reputation: 110

Like
0Likes
Like

Posted 04 October 2011 - 10:24 PM

Thanks guys! Please keep them coming. I'm definitely going to practice these problems. I think I would bomb a few of them if asked to do them on the spot.

#5 latent   Members   -  Reputation: 139

Like
1Likes
Like

Posted 04 October 2011 - 10:31 PM

The one I used to ask was write a function that returns a count of all the 'on' bits in a byte parameter.

There are (of course) different ways of doing it, and (astonishingly) watching some candidates balk at the very idea of writing a C function was a little disappointing. I was really hoping at least one candidate would say 'well, why not just have a lookup table', but sadly it never happened...

As an interviewer it was much more interesting to me how they went about trying to do the problem than whether it was actually right. Plus, performance under pressure and all that...

#6 Hodgman   Moderators   -  Reputation: 28713

Like
1Likes
Like

Posted 04 October 2011 - 10:35 PM

In an interview for a game company I was asked to write a function that will reverse a string without using the string class (i.e. use a null terminated char array).

I've seen that one, plus simple extensions like - for each word in the string, reverse the letters in the word, or, reverse the order of the words that make up the string, etc...

These kinds of questions are just to make sure that you're competent at basic, low-level programming concepts, like pointers and bytes.
In this category, you'd probably see other questions like, how would you test if the highest bit in a byte is set, or, how would you count the bits set in a word, or, how would you multiply a number by 8 without using multiplication, etc...

There's also questions that don't require you to explicitly write code, but say a lot about your ability to write code, like:
If you've got two classes that are dependent on each other (i.e. a circular dependency), how would you break this dependency?
Is inheritance useful for code-reuse?
What is the big-O complexity of inserting an item into a sorted array?

I was really hoping at least one candidate would say 'well, why not just have a lookup table', but sadly it never happened...

No one broke out the leet bit-twiddling skillz? Posted Image
count = (((((v - ((v >> 1) & 0x55555555)) & 0x33333333) + (((v - ((v >> 1) & 0x55555555)) >> 2) & 0x33333333)) + ((((v - ((v >> 1) & 0x55555555)) & 0x33333333) + (((v - ((v >> 1) & 0x55555555)) >> 2) & 0x33333333)) >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;

#7 L. Spiro   Crossbones+   -  Reputation: 13004

Like
2Likes
Like

Posted 04 October 2011 - 11:05 PM

They ask for source code to reverse a linked list at Morgan Stanley, 2nd interview (just in case anyone was curious).
In the 3rd interview they ask you to make a date class which can print the month names and the number of days in each month, accounting for leap years. They tell you leap years occur and your job is just to make sure you implement that logic correctly.
You can also expect a lot of questions about design patterns.
There was only one question I could not get, and frankly to this day I still do not know the answer. I wish I could remember exactly how the question was phrased.


Other miscellaneous problems:
#1: Draw a BVH in 2D.
#2: Convert a string to an integer (manually).
#3: Pick a random number between 0 and 110 which is a multiple of 10.
#4: Given that the variables A and B are 32-bit unsigned fixed-point variables with 4-bit fractions, how many fractional bits will the results of each of these have?
-> A: A + B
-> B: A * B
-> C: A / B
#5: Given 2 axis-aligned boxes in 2D, outline an algorithm to determine if they overlap.
#6: Rewrite the following function so that the multiplication or division operators are not used and there are no loops:
int MultiplyInputBy123( int input_value )
{
    return input_value * 123;
}

#7: Is C++ slower than C? Why or why not?


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 VReality   Members   -  Reputation: 436

Like
1Likes
Like

Posted 04 October 2011 - 11:09 PM

This comes up a lot: "When and why should destructors be declared virtual?"

Perhaps the toughest interview question is the one you know the answer to but the interviewer has wrong. ;^)

I once had an interviewer ask me this:

class BaseClass
{
  public:
	int X;
	virtual void SetX() {X = 1;}
	BaseClass() {SetX();}  // <-- Call to a virtual function in the base class constructor.
};

class DerivedClass : public BaseClass
{
  public:
	void SetX() {X = 2;}  // <-- The virtual function is also defined in the derived class.
}

int main()
{
  DerivedClass DerivedObject;  // <-- An object of the derived class is created.

  // What is the value of DerivedObject.X ?

The interviewer thought (as programmers often do) that it was a clever question because he had stumbled upon it the hard way. I happened to know the answer. But he'd never really figured out what was going on when he ran into this problem, and he had (erroneously) come to the conclusion that the behavior was undefined.

#9 Darkbouncer4689   Members   -  Reputation: 110

Like
1Likes
Like

Posted 05 October 2011 - 12:20 AM

It's interesting how humbling computer science can be. Even after 4 years of programming I don't know that I could answer some of these questions with people standing over my shoulder and without using the internet for a quick reference. But that's what this thread is all about! I'm going to practice all of these and hopefully not choke at interview time.

#10 Katie   Members   -  Reputation: 1303

Like
1Likes
Like

Posted 05 October 2011 - 03:20 AM

" the one you know the answer to but the interviewer has wrong"

Frankly, it's amazing how common that is. One of the problems with these things is that all too often they degenerate into an opportunity for interviewers to make themselves feel better. And quite often they're over-reaching their own technical skills.

Fizzbuzz or reverse a string is entirely sufficient to lose the maybe 60% of candidates who actually can't program AT ALL[1]. Anything beyond that is frankly labouring the point; esoteric questions about the C++ spec are boring for everyone concerned and have no relation to work actually done. (It was amazing the amount of interviews I went to as an IT contractor which were all about weird tricky edge cases of C++ exception throwing, but when I actually started work there, the codebase doesn't use exceptions because the developers aren't confident enough at using them. And usually therefore they're banned...)




[1] What I find slightly bonkers is that a substantial number of these candidates actually think they are good developers and are surprised to find themselves struggling. It seems to stem from being in large matrix-managed teams where one can spend many many years being only borderline competent at developing without anyone noticing. The actual output of the team is caused by the work units being regularly and randomly swapped between members until (eventually) they're done by the one or two competent developers. In many industries this is normal working practice[2], and staff can truly believe they're contributing because the work units passed through their hands. Likewise I meet an amazing number of highly experienced OO devs who, on closer inspection, have actually spent many years going to meetings where some OO design got done... not *necessarily* by them.

[2] And the appallingly low productivity is (in a storming bit of double-think) considered normal because... well... software developers just actually aren't very good are they? Well, on average NO, they're not...

#11 Trienco   Crossbones+   -  Reputation: 2091

Like
0Likes
Like

Posted 05 October 2011 - 06:51 AM

" the one you know the answer to but the interviewer has wrong"


That's why I figured making it the point to be wrong. In short, it's a horrible piece of code essentially just appending one c-string to another.. except using all the awful things I've encountered in peoples code, from tacking every problem by typing "while(1) switch(x)" on autopilot, to the annoying "while(1) break, break, break" pseudo-goto, off-by-one bugs, non-descript names. The point is essentially to just ask "what is wrong with this code" and let him bash away (always hoping for "how about using std::string and += instead of this twisted mess?")

The only time I had an interested test to do was essentially "offline" (ie. they send you the questions and expect you to write back in finite time). Stuff like:

-The AI needs to navigate through x waypoints, implement the move function
-Optimize this code (if in nested loops to check if youre in the second half)
-Silly things (finish this linked list implementation, add 1 to unsigned int with only shifts and logical ops)
-Refactor this code

And my favorite, which I kept for not being one of those silly bit-fiddling questions (in summary):

Imagine a chess board as 8x8 columns of different height. Now imagine pouring water over it (which will of course drain off at the edge of the board). Describe an algorithm to calculate the amount of water the board can hold.

Implementation was not wanted, only a description of the steps. Of course after sending back the test I couldn't resist to create a simple OGL program to generate random boards, calculate the water level at each sell and render it for "visual verification".
f@dzhttp://festini.device-zero.de

#12 Telastyn   Crossbones+   -  Reputation: 3726

Like
0Likes
Like

Posted 05 October 2011 - 06:58 AM

In a coding test I had to do sorting, binary search, and a quick pathfinding problem. I've had to read from a file and put it back in sorted order. I've had to create two signals and have them trigger one another (based on some criteria).

In interviews I get the 'reverse a null terminated string', 'now do it in place', 'now do it in place with no temporary' path pretty much all the time.

#13 ApochPiQ   Moderators   -  Reputation: 14673

Like
2Likes
Like

Posted 05 October 2011 - 10:50 PM

I can't get into the details of real interviews I've had, because of NDAs and such, but here's a few generic things to be comfortable with:

  • Basic algorithms: search, sort, and various applications of recursion
  • Basic data structures: arrays, lists, trees, heaps, hash tables, etc.
  • Tree/graph traversal methods - know the difference between depth-first and breadth-first and why each one is preferable in certain circumstances
  • Pre-order, post-order, and in-order traversal is a good one to know as well
  • Simple string processing at the byte/character level
  • Elementary bit manipulations - at the very least, know how bitmasking and such work
  • Simple analysis of existing code to find missing functionality
  • Implementation of new functionality in a fixed, unchangeable code base

Last but very much not least, know how to describe your thought processes in detail and with precision. If you're given an hour to do a two hour job, do your best in 45 minutes. Spend the remaining 15 minutes documenting/explaining exactly where you could improve and why you chose the trade-offs you did. This is a common curve-ball to throw at people in my experience, so be ready for it.

Overall... if you can pick up someone else's code, modify it, and produce something that works, you'll probably be OK.

#14 Darkbouncer4689   Members   -  Reputation: 110

Like
0Likes
Like

Posted 06 October 2011 - 07:34 PM

Thanks guys. Theres a good bit of material here!

Sometimes I'm puzzled by how much is expected out of a programmer. I'm about to graduate from the University of Texas at Austin.... a top 10 CS university where they don't teach you C++ until you're final semester in an OS class, and by teach you C++ I mean have you write a few basic functions in C. Yet straight out of college we are expected to be professional C++ programmers. Luckily I have worked with C++ quite a bit in my free time, but I haven't had the opportunity to program in nothing but C++ for a few years to really master the language.

Outside of the language we have these data structures, bit manipulations, and random algorithms that you learn once and only use on a blue moon (at least in college) and then are expected to know off the top of your head. I don't know how anyone could do it without having years of professional experience or spending a few weeks before interviews memorizing every potential question.

Majoring in CS is generally much more demanding then most majors and then we are expected to go above and beyond that in our free time. I guess were not allowed to get exercise or have lives =)

I guess that's why some people get paid the big bucks. I'll just have to prepare as much as I can and hope for the best.

#15 Telastyn   Crossbones+   -  Reputation: 3726

Like
1Likes
Like

Posted 06 October 2011 - 07:45 PM

You should've perhaps specified what sort of position you were looking for example questions for (or perhaps we should've).

I suspect a lot of the people here didn't get these questions when they interviewed as entry level devs. And the tone of interviews varies a lot depending on your industry and even location. Up here in Minnesota, you're lucky to get any technical questions at all. Out in silicon valley, even the most laid back company still has a fairly rigorous technical gauntlet.

#16 Ravyne   Crossbones+   -  Reputation: 7037

Like
0Likes
Like

Posted 06 October 2011 - 08:07 PM

Apoch has had the best answer so far. The point, after all, is not to memorize clever answers to a few popular problems, but to have a solid grasp of the wide range of fundamentals that these TYPES of questions are meant to evoke. Besides, a good interviewer will recognize a recalled answer, rather than one which is truly being thought out, even if you fake the discovery aspect. Honestly, if you get a question you already know, that should be the first thing you reveal to your interviewer. They may ask you to do it anyways, in which case you want to show that you understand the solution you have (and also that you are sure there are no holes in it.)

In addition to algorithm-ish stuff, also know basic oop and language stuff -- what are the 3 pillars of oop? What are the 3 kinds of inheritance and under what situations are they useful (properly used, not how they work)? What is a 'volatile' variable, and what's an example of when you might use it? Describe how 'public', 'protected', and 'private' affect member visibility. Know what 'static' means in all the various contexts it can be uses. Those types of things.

#17 Darkbouncer4689   Members   -  Reputation: 110

Like
0Likes
Like

Posted 06 October 2011 - 08:08 PM

You should've perhaps specified what sort of position you were looking for example questions for (or perhaps we should've).

I suspect a lot of the people here didn't get these questions when they interviewed as entry level devs. And the tone of interviews varies a lot depending on your industry and even location. Up here in Minnesota, you're lucky to get any technical questions at all. Out in silicon valley, even the most laid back company still has a fairly rigorous technical gauntlet.


Haha, good point. I'm hoping to get into the gaming industry as that is what I spend most of my free time working on and have the most experience with (and of course what I am passionate about). This of course means strong C++ skills required. In my free time I made a WebGL/JavaScript engine which has a lot of basic 3D game engine functionality www.cs.utexas.edu/~josh4689 (feel free to criticize). I feel that I have a pretty solid foundation about how the basic mechanics in a game engine work. Plenty of experience with vector/matrix math, basic lighting techniques, basic skin-and-bone animation, simple terrain (with GeoMipMapLoD), bounding volumes, quite a few intersection algorithms, picking, most of the basic geometry used, view frustum culling, and parsing Collada files.

Unfortunately most of the big companies don't seem to be interested in hiring an entry level software engineer. Bungie has a few openings I feel I am qualified for. Keeping my fingers crossed.

#18 L. Spiro   Crossbones+   -  Reputation: 13004

Like
1Likes
Like

Posted 06 October 2011 - 08:49 PM

You should have been born in Japan. Here, they almost only hire out-of-college kids. Before you even graduate you already know at which company you will be working, and you will work there for basically your life.

That being said raises a point. You don’t have to seek only within America. I started my career in video games overseas. Keep in mind that the world is your oyster, not just your country.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#19 Darkbouncer4689   Members   -  Reputation: 110

Like
0Likes
Like

Posted 07 October 2011 - 12:15 AM

Heh. While I am a big anime fan, I'm also 6'2. I would stick out like a sore thumb! On a side note, could one only speak english and still work over there?

#20 L. Spiro   Crossbones+   -  Reputation: 13004

Like
3Likes
Like

Posted 07 October 2011 - 01:26 AM

It is possible, as I know many who are doing it.
But Japan is not where I would start. This country is very exclusive and it is practically unheard-of to hire someone from overseas who does not speak the language and is just getting out of school.
You would need a fairly hefty resumé to be recruited from outside of the country.

Other countries to consider: Sweden, England, France, Germany, Malaysia, China, Hong Kong, and Thailand.
Few people realize that southeast Asia is trying to get into the gaming industry, and foreigners are considered welcome. The Malaysian government makes game companies tax-exempt.
You have a chance at Game Brains in Kuala Lumpur. Their office used to have a nice view from the 60-something’th floor of the Petronas Towers, but they have since moved.
You have a good chance in Thailand at Lumai Prod or Sanuk Games. Both are run by French people who speak English. Thailand and Malaysia are countries where it is absolutely not necessary to speak the native language to live there.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS