C++ Interview Questions

Want to become an expert in cracking C++ interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public C++ Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.

1. Merge Names

C++ Arrays Public New

Implement the unique_names method. When passed two vectors of names, it will return a vector containing the names that appear in either or both vectors. The returned vector should have no duplicates.

For example, calling unique_names(std::vector<std::string>{"Ava", "Emma", "Olivia"}, std::vector<std::string>{"Olivia", "Sophia", "Emma"}) should return a vector containing Ava, Emma, Olivia, and Sophia in any order.


  •   Example case: Exception
  •   Each vector has distinct names: Exception
  •   Each vector has duplicate names: Exception
  •   Vectors have some names in common: Exception

2. Palindrome

C++ Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, isPalindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.


  •   Example case: Exception
  •   Lowercase words: Exception
  •   Various words: Exception

3. Binary Search Tree

C++ Algorithmic thinking Data structures Public

A three-node binary tree.Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.

Write a function that, efficiently with respect to time used, checks if a given binary search tree contains a given value.

For example, for the following tree:

  • n1 (Value: 1, Left: null, Right: null)
  • n2 (Value: 2, Left: n1, Right: n3)
  • n3 (Value: 3, Left: null, Right: null)

Call to contains(n2, 3) should return true since a tree with root at n2 contains number 3.


  •   Example case: Exception
  •   Correctness: Exception
  •   Performance test on a large tree: Exception

4. Song

C++ Algorithmic thinking Searching Public New

A playlist is considered a repeating playlist if any of the songs contain a pointer to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to NULL.

Implement a function isRepeatingPlaylist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not.

For example, the following code prints "true" as both songs point to each other.

Song* first = new Song("Hello");
Song* second = new Song("Eye of the tiger");

std::cout << std::boolalpha << first->isRepeatingPlaylist();

  •   Example case: Exception
  •   If playlist repeats, last song is followed by first song: Exception
  •   If playlist repeats, starting songs can be skipped: Exception
  •   Performance test on a large playlist: Exception

5. User Input

C++ OOP Public

User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput, which accepts only digits.

Implement the following methods:

  • add on class TextInput - adds the given character to the current value
  • getValue on class TextInput - returns the current value
  • add on class NumericInput - overrides the base class method so that each non-numeric character is ignored

For example, the following code should output "10":

TextInput* input = new NumericInput();
std::cout << input->getValue();

  •   Example case: Exception
  •   Adding various characters to TextInput: Exception
  •   Adding various characters to NumericInput: Exception

6. Two Sum

C++ Algorithmic thinking Searching Public

Write a function that, when passed a list and a target sum, returns, efficiently with respect to time used, two distinct zero-based indices of any two of the numbers, whose sum is equal to the target sum. If there are no two numbers, the function should return (-1, -1).

For example, findTwoSum({ 3, 1, 5, 7, 5, 9 }, 10) should return a std::pair<int, int> containing any of the following pairs of indices:

  • 0 and 3 (or 3 and 0) as 3 + 7 = 10
  • 1 and 5 (or 5 and 1) as 1 + 9 = 10
  • 2 and 4 (or 4 and 2) as 5 + 5 = 10

  •   Example case: Exception
  •   Distinct numbers with and without solutions: Exception
  •   Duplicate numbers with and without solutions: Exception
  •   Performance test with a large list of numbers: Exception

7. Pipeline

C++ Closures Public New

As part of a data processing pipeline, complete the implementation of the makePipeline method:

  • The method should accept a vector of functions, and it should return a new function that accepts one parameter arg.
  • The returned function should call the first function in the makePipeline with the parameter arg, and call the second function with the result of the first function.
  • The returned function should continue calling each function in the makePipeline in order, following the same pattern, and return the value from the last function.

For example,

std::vector<std::function<int (int)>> functions;
functions.push_back([] (int x) -> int { return x * 3; });
functions.push_back([] (int x) -> int { return x + 1; });
functions.push_back([] (int x) -> int { return x / 2; });
std::function<int (int)> func = makePipeline(functions);

calling the func(3) should return 5.


  •   Example case: Wrong answer
  •   Various functions: Wrong answer
  •   Various data types: Wrong answer

8. Multiple Choice Test

C++ Bug fixing Memory management OOP Public

Multiple choice test has several multiple choice questions. Each question can have only one correct answer. Additionally, timed multiple choice test can specify the time allowed for solving each question in the test.

The code below satisfies this specification, but the customer complained that the memory usage of the program constantly increases. Fix this problem.


  •   Using multiple choice test: Memory limit exceeded
  •   Using timed multiple choice test: Memory limit exceeded
  •   Using timed multiple choice test as multiple choice test: Memory limit exceeded

9. Sorted Search

C++ Algorithmic thinking Public

Implement function countNumbers that accepts a sorted vector of unique integers and, efficiently with respect to time used, counts the number of vector elements that are less than the parameter lessThan.

For example, for vector v containing { 1, 3, 5, 7 }, countNumbers(v, 4) should return 2 because there are two vector elements less than 4.


  •   Example case: Exception
  •   Various small vectors: Exception
  •   Performance test when sortedVector contains lessThan: Exception
  •   Performance test when sortedVector doesn't contain lessThan: Exception

10. Train Composition

C++ Algorithmic thinking Data structures Public

A TrainComposition is built by attaching and detaching wagons from the left and the right sides, efficiently with respect to time used.

For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.

Implement a TrainComposition that models this problem.



  •   Example case: Exception
  •   Several wagons: Exception
  •   Performance test with a large number of wagons: Exception

11. Path

C++ Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.


  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as "..".
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example:

Path path("/a/b/c/d");
std::cout << path.getPath();

should display '/a/b/c/x'.


  •   Example case: Exception
  •   Selecting child directories: Exception
  •   Selecting parent directories: Exception
  •   Selecting complex paths: Exception

If you feel ready, take one of our timed public C++ Interview Questions tests:
  • C++ Online Test (Easy / Hard)
  • C++ Algorithms Online Test (Easy / Hard)
  • C++ Algorithms and SQL Online Test (Easy / Hard)
  • C++ and SQL Online Test (Easy / Hard)
Not exactly what you are looking for? Go to our For Jobseekers section.