C++ Interview Questions

OOP Questions:

Coding Questions

2 Coding Questions

  1. Implement the Big 5 for a doubly linked list. Do so using a Node with fields Node* next and Node* prev.
  2. Transform this implementation to be well encapsulated via a DoublyLinkedList class that only exposes public methods.
  3. Having implemented the big 5 for the doubly linked list, implement a bi-directional iterator. A bidirectional iterator allows both ++it to take you to the next element, as well as –-it to take you to the previous element.
  4. Implement a reverse iterator for your linked list. Reverse iterators can be created using rbegin() and rend(). They start at the end of the data structure, and ++it takes them to the previous element.
  5. Consider a class RepeatedString, which contains a string s and a positive integer k. For a RepeatedString rs, implement overloads so that the following code performs the desired behaviour:
    1. A single constructor that allows both default construction (with s set to the empty string and k set to 0), as well as construction given the string s and int k
    2. cout << rs which prints the string rs.s k times.
    3. cin >> rs which should read in a string s
    4. For integer j, rs * j and j * rs which creates a RepeatedString with the same string, and associated integer k ∗ j.
  6. Consider creating a chess game. Consider how you may represent different pieces using an inheritance hierarchy. Each piece should have a column and row field that range from 0 to 7, and also a 2D board that allows them to determine whether a piece is occupied at a particular location. Implement a virtual method getMoves that returns a vector of possible coordinates a given piece could move to. Do so for each possible piece on the chessboard, keeping in mind that shared logic should be abstracted into the base class where possible.

2 Coding Questions

  1. Write a full implementation of shared_ptr. It should have a default constructor, a constructor taking in a ptr of type T, and support all big 5 operations, as well as an overloaded dereference operator.
  2. A class that defines operator< can define the other methods in terms of the < operator. For example: (a > b) is equivalent to (b < a). (a == b) is equivalent to !(a < b) && !(b < a). Write a CRTP class Compareable. Compareable should add the various comparison operators to a class that supports operator<. Give an example of a class that implements operator< and show how Compareable can be used with it. Explain why one might prefer this over an abstract class “AbstractCompareable” that defines the various comparison operators in terms of <, and defines operator< as a virtual method to be overridden in the subclass.
  3. Consider making the game of Chess. Mock up an inheritance hierarchy in UML where a computer class uses the strategy pattern to make moves depending on the difficulty level of the AI. Implement this in C++.
  4. Consider a hierarchy representing a file system. We could have a superclass Entry, then subclasses for things like Files, Directories, links, network devices, etc. Write a hierarchy including at least Entry, File, and Directory, with an accept method taking in a visitor. Write visitors to count the number of bytes in a directory/file, and one to print out all the file names in a directory recursively.
  5. Consider performing manipulations to a string via the command pattern. You start with some initial string s, then have different commands such as Slice (which returns a substring of s given some starting and ending index), Insert (which inserts a string t into s at some index), and Repeat (which causes s to repeat n times). Implement these commands via the Command pattern, along with an Invoker and the ability to undo each command. Try implementing it in the simple way (the invoker simply keeps a history of the state of the string in-between each command) and in the memory efficient way (the commands have code and extra fields to undo their action).

Other Questions

From reddit

  • If you encounter a class with the word “virtual” in a method declaration, tell me about what it does. What happens if a derived class overrides a method that isn’t virtual?
  • How does compiler and run time make virtual methods work.
  • When should a destructor be declared “virtual” ?

Master these C++ classes from the std namespaces

  • new, delete and when to use delete []
  • map and unordered_map - know the tradeoffs
  • set and unordered_set - same
  • vector
  • string
  • queue, list, and stack
  • Basic iteration over a collection. Either with iterators in a for\while loop, or with a range based for loop

The above is all you’ll need for leetcode type practice Some advanced concepts:

  • shared_ptr, weak_ptr, unique_ptr
  • Basic concurrency: thread, mutex, condition_variable, and the different lock classes.
  • lambda, captures, and std::function

Of course there’s more like universal references and template meta programming. Stuff isn’t likely to come up in an interview.