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. TwoSum
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 any of the following pairs of indices:

  • 1, 4 (3 + 9 = 12)
  • 2, 3 (5 + 7 = 12)
  • 3, 2 (7 + 5 = 12)
  • 4, 1 (9 + 3 = 12)
Easy  
30min
Solve question

2. UserInput
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();
input->add('1');
input->add('a');
input->add('0');
std::cout << input->getValue();
Easy  
15min
Solve question

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

Easy  
10min
Solve question

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

Easy  
15min
Solve question

5. Path
C++ Data structures Strings Public

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

Notes:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as "..".
  • Directory names consist only of English alphabet letters (A-Z and a-z).

For example, Path("/a/b/c/d").cd("../x").getPath() should return "/a/b/c/x".

Note: Do not use built-in path-related functions.

Hard  
30min
Solve question

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

Hard  
15min
Solve question

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

Hard  
20min
Solve question

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

Hard  
20min
Solve question

If you feel ready, take one of our timed public C++ interview questions tests:

Not exactly what you are looking for? Go to our For developers section.