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 developers, purchase one of the paid plans for companies.

1. 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.

Solve question

2. Binary Search Tree
C++ Algorithmic thinking Data structures Public

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 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.

Solve question

3. 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();
Solve question

4. Two Sum
C++ Algorithmic thinking Searching Public

Write a function that, given a vector and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. If there are no such elements, the function should return (-1, -1).

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

  • 1 and 4 (3 + 9 = 12)
  • 2 and 3 (5 + 7 = 12)
  • 3 and 2 (7 + 5 = 12)
  • 4 and 1 (9 + 3 = 12)
Solve question

5. 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.

Solve question

6. Sorted Search
C++ Algorithmic thinking Public

Implement function countNumbers that accepts a sorted vector of integers and counts the number of vector elements that are less than the parameter lessThan.

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

Solve question

7. Train Composition
C++ Algorithmic thinking Data structures Public

A TrainComposition is built by attaching and detaching wagons from the left and the right sides.

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.

Solve question

8. 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 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'.

Solve question

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 developers section.