C++ Interview Questions
OOP Questions:
Coding Questions
2 Coding Questions
- Implement the Big 5 for a doubly linked list. Do so using a Node with fields
Node* next
andNode* prev
. - Transform this implementation to be well encapsulated via a DoublyLinkedList class that only exposes public methods.
- 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.
- 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.
- 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:
- 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
cout << rs
which prints the stringrs.s
k
times.cin >> rs
which should read in a strings
- For integer
j
,rs * j
andj * rs
which creates a RepeatedString with the same string, and associated integerk â j
.
- 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
- 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.
- 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.
- 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++.
- 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.
- 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 usedelete []
map
andunordered_map
- know the tradeoffsset
andunordered_set
- samevector
string
queue
,list
, andstack
- 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.