P
prepair.app
Start interview →
EnglishУкраїнськаРусский
🔷

Middle C++C++ interview questions

Middle · 2–4 years of experience

C++ interviews test three things: whether you understand the object model, whether you can be trusted with memory, and whether you know what the standard library costs. Below are the questions asked most often, each with a model answer. Middle: deeper understanding, optimization, and real-world situations.

Topics to prepare

Memory, pointers and RAII
OOP, virtual functions and vtable
Move semantics and the rule of five
Templates and metaprogramming
STL containers and complexity
Concurrency and data races

6 Middle-level questions with answers

1

What is the rule of five, and what does the compiler generate for you?

Answer

If a class needs a custom destructor, copy constructor or copy assignment, it almost certainly needs all of them plus the move constructor and move assignment — because it owns a resource. The compiler generates them implicitly, but declaring any of them suppresses some of the others: writing a destructor stops the move operations from being generated, and copies silently take their place. The rule of zero is better still — own resources through members that already manage themselves.

2

What does `std::move` actually do?

Answer

Nothing at runtime. It is an unconditional cast to an rvalue reference, which changes overload resolution so a move constructor or move assignment can be selected. If the type has no move operations, the copy is chosen and the code silently keeps copying. After a move the source is valid but unspecified — you may assign to it or destroy it, but not assume its contents.

3

What is copy elision and RVO?

Answer

The compiler constructing a returned object directly in the caller's storage instead of building it and then copying or moving. Named RVO and unnamed RVO were long-standing optimisations that were allowed to remove observable side effects; since C++17 elision is mandatory for returning a prvalue. The practical consequence is that returning by value is not the pessimisation people still assume, and return std::move(x) usually makes things worse by blocking NRVO.

4

When are iterators invalidated?

Answer

It is per container and worth knowing exactly. vector invalidates everything on reallocation and everything from the erase point onward; deque invalidates all iterators on insertion at the middle; list and the associative containers keep iterators valid except to the erased element. The classic bug is erasing inside a loop, which is why erase returns the next valid iterator and why the erase-remove idiom exists.

5

`std::map` vs `std::unordered_map` — how do you choose?

Answer

map is a balanced tree: ordered iteration, O(log n) lookup, stable iterators, predictable. unordered_map is a hash table: average O(1) but O(n) worst case, no ordering, rehashing invalidates iterators, and it depends on the hash quality. Take map when you need order or bounded worst case, unordered_map when lookups dominate and order does not matter.

6

What are the exception safety guarantees?

Answer

Four levels: no-throw, strong (the operation either completes or leaves the state unchanged), basic (invariants hold and nothing leaks, but the state may have changed), and none. The strong guarantee is usually implemented by doing the work on a copy and swapping at the end. Destructors must not throw — during stack unwinding a second exception calls std::terminate, which is why destructors are noexcept by default.

🦎

Reading answers is not enough

In a real interview you speak under pressure. Cam asks these same questions, scores every answer, and shows exactly what to fix.

Practice a Middle C++ interview →
Free · 3 interviews per month

Other levels — C++

Junior C++Senior C++All C++ questions

Other specializations

🔍Middle Manual QA🤖Middle QA AutomationMiddle Java Backend🐍Middle Python Backend🐘Middle PHP Backend🦫Middle Go Backend🟢Middle Node.js Backend💎Middle Ruby on Rails🟨Middle JavaScript⚛️Middle React Frontend