Friday, May 24, 2013

Top 10 C++ Interview Questions

For those of you who applying for the dwindling number of pure C++ coding jobs, here seems to be the most popular questions interviewers like to ask. I guess some of these questions are rather general and can apply to other object oriented languages as well, but I try to focus on those that are more C++ specific. I've included some STL questions as well, because most C++ shops WILL expect you to have at least some familiarity with STL. 

1. What is a virtual function? How is it implemented in C++?

Usually they would start out with the question, "What is polymorphism"? Which usually leads to the virtual function question. 

2. What is the meaning of mutable?

This is a favorite question of interviewers because it leads into you having to explain const functions in more detail.

3. What is the difference between a class and a struct?

Still a popular question after all these years. 

4. Explain the different uses of the keyword "static".

There are several variations of this question. A popular version is "what is the difference between a static method and a regular method"? (Answer: static method lacks the "this" pointer). 

5. Why do you need a virtual destructor?

This used to be the favorite question asked by interviewers, but I noticed a drop in popularity recently. Maybe they realized just how lame it is?

6. Is it ok to call a virtual function from a constructor? Why or why not?

Sometimes they like to throw a more "advanced" question in there to trip you up. This is one of the more popular ones. It's not easy to explain, so make sure you read up on it. 

7. Name some STL containers and why you would choose one over the other.

Just make sure you know your associative containers from the sequenced ones. Also to really knock their socks off, read up on which data structures are used internally by the containers (for example sets are implemented as binary search trees.)

8. Is it safe to use auto_ptrs in an STL container?

9. Explain the concept of "Resource Acquisition Is Initialization". 

Questions 8 and 9 kind of tie in together. It's not surprising to get both of them in an interview.

10. Explain what is the keyword "explicit". 

Another favorite question of interviewers because it leads to discussions on copy constructors, and when/why you would need them.

Some extra questions to consider:

What is "volatile"? Will be most likely asked if your tasks involve threading.
What is "inline"? I don't get asked this very often anymore.
Difference between "const int *" and "int* const". Used to be a popular questions. The "trick" I use to keep myself from getting mixed up is looking if the const keyword is before or after the "*".
Explain the pros and cons of templates. This is actually a question I like, and don't let your lack of knowledge of templates scare you, it's actually a good chance for you to impress your interviewer. Just remember the important points like code bloat, debugging difficulty, type safety, etc.


Thursday, May 23, 2013

Top 10 Most Overused Coding Interview Questions

These are questions that I've been asked at least twice, sometimes even more. I think there's a pretty good chance of running into at least one of them during an interview at a typical tech firm. Note that for some of the bigger compaies like Google or Microsoft, these are usually just "screening" questions, you'll get much tougher ones at the real in-person interviews.

1. Write a function to calculate the Nth number in a Fibonacci sequence.

2. Write a function to reverse words in a string.

3. Implement a data structure that behaves like a heap but only using stacks.

4. Write a function to reverse a linked list.

5. How do you find out if a linked list is cyclical?

6. Given a series of numbers, find the longest sequence of numbers that add up to the largest sum.

7. Given a list of 101 numbers from 1 to 100, with a number being duplicated, find the duplicate number.

8. Implement the itoa() function (converting an integer to a string).

9. Try to implement the divide operator without using any actual division (or modulo).

10. Write the preorder, postorder, and inorder traversal functions for a binary tree.