Skip to content

Expert · Competitive programming, interviews and careers · topic 41 of 42 · 35 min

C++ interview questions and how to answer them

Explanation

C++ interviews mix DSA coding with language questions: pointers versus references, the virtual function mechanism, RAII and smart pointers, the rule of three/five/zero, move semantics, const, the STL's complexities and undefined behaviour. Answer with a one-line definition, a tiny example and when you would use it.

How C++ interviews usually go

For freshers, rounds are typically an online test (DSA problems), one or two technical interviews (DSA plus language and core CS questions such as operating systems and OOP), and an HR round. For C++-heavy roles (systems, embedded, games, trading), expect deeper language questions and sometimes "what does this code print?" puzzles.

A good answer has three parts

  1. A clear one-line definition.
  2. A tiny example, said aloud or written in 2–4 lines.
  3. When to use it, or a trap to avoid. This is what shows real understanding.

The most-asked questions

QuestionShort answer
Pointer vs reference?A reference is an alias that must be bound once and cannot be null; a pointer holds an address, can be null and can be re-pointed.
struct vs class?Only the default access differs: public for struct, private for class.
What is RAII?Tie a resource to an object's lifetime: acquire in the constructor, release in the destructor; safe with exceptions.
unique_ptr vs shared_ptr?unique_ptr: one owner, move-only, no overhead. shared_ptr: shared ownership via reference counting; use weak_ptr to break cycles.
How do virtual functions work?Usually through a per-class vtable of function pointers and a hidden vptr in each object; the call is resolved at run time.
Why a virtual destructor?So deleting a derived object through a base pointer runs the derived destructor; otherwise undefined behaviour.
Rule of three/five/zero?If you write one of destructor/copy/move operations you probably need all; better, use members that manage themselves and write none.
What does std::move do?It only casts to an rvalue, allowing a move constructor or move assignment to be chosen.
map vs unordered_map?Balanced tree, sorted, O(log n) vs hash table, unordered, O(1) average, O(n) worst.
What is undefined behaviour?Code the standard gives no meaning to (out-of-bounds, signed overflow, use-after-free); anything may happen, including "works on my machine".
const in a member function?It promises not to modify the object, so it can be called on const objects.
Stack vs heap?Stack: automatic, fast, limited, freed at scope end. Heap: manual or smart-pointer managed, larger, lives until freed.

Code-reading questions

cpp
struct Base { virtual void hi() { cout << "Base"; } };
struct Derived : Base { void hi() override { cout << "Derived"; } };

Base* p = new Derived;
p->hi();          // Derived (virtual dispatch)
Base b = *p;      // slicing
b.hi();           // Base
delete p;         // undefined behaviour: Base has no virtual destructor
Interviewers love this one: virtual dispatch, slicing and the missing virtual destructor in a few lines.
Think of it like this: Treat the interviewer like a junior colleague you are teaching. A clear explanation with a small example beats a long list of buzzwords, just as a good teacher's single example beats ten definitions.

Questions you can ask about your own code

  • What is the time and space complexity? Can it be better?
  • What happens with empty input, one element, the largest values?
  • Where could this overflow?
  • Who owns this memory, and when is it freed?
  • Is this thread-safe? Does it need to be?

Behavioural part

Prepare a 60-second introduction and two project stories using the STAR method: Situation, Task, Action, Result. For a C++ project, mention a concrete technical decision ("I used unique_ptr for the level objects so there were no leaks between levels") and a measurable result ("load time fell from 3 s to 1 s").

Careful: Do not claim experience you do not have. If you do not know an answer, say what you do know and how you would find out ("I believe it is O(log n) because map is a tree; I would confirm on cppreference"). Interviewers value honesty and reasoning more than bluffing.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

C++ interview questions and how to answer them · 1/6

C++ interviews

C plus plus interviews mix coding problems with language questions.

Prefer a video by a teacher? Find free videos on “c++ interview questions for freshers virtual function smart pointers raii” on YouTube ↗

In real life: A mock answer to "Explain smart pointers"

Candidate: "A smart pointer is an RAII wrapper that deletes a heap object automatically. unique_ptr has a single owner, cannot be copied but can be moved, and costs nothing extra; I use it by default, for example to own polymorphic objects in a vector. shared_ptr counts owners and deletes when the count hits zero; it costs a control block and atomic updates, so I use it only for truly shared ownership, and weak_ptr to break cycles like parent links." Short, correct, with when-to-use.

cpp
auto shape = std::make_unique<Circle>(2.0);          // single owner
std::vector<std::unique_ptr<Shape>> scene;
scene.push_back(std::move(shape));                   // ownership moves into the vector

auto cfg = std::make_shared<const Config>();         // shared, read-only
std::weak_ptr<const Config> observer = cfg;          // does not keep it alive
The code you might sketch on the whiteboard.

Practice (0/4 done)

Task 1 · Question

What does this print? struct A { A() { cout << "A"; } ~A() { cout << "a"; } }; struct B : A { B() { cout << "B"; } ~B() { cout << "b"; } }; int main() { B x; }

Task 2 · Question

Which is the best one-line answer to "What is undefined behaviour?"

Task 3 · Question

What prints? int x = 5; int& r = x; int* p = &x; r = 7; *p = *p + 1; cout << x;

Task 4 · Practice

Record yourself (or practise with a friend) answering these five questions in under 90 seconds each, using "definition, example, when to use": (1) pointer vs reference, (2) RAII, (3) virtual destructor, (4) std::move, (5) map vs unordered_map. Then write your best answer to each in 3–4 lines.

Quick quiz

Question 1

1.Which answer about std::move is correct?

Question 2

2.Which is NOT undefined behaviour?

Question 3

3.What is the STAR method?

Question 4

4.An interviewer asks something you do not know. Best response?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which answer about std::move is correct?

Answer: It is a cast to an rvalue; the actual move happens in the move constructor or assignment

std::move itself does no work at run time.

Which is NOT undefined behaviour?

Answer: Unsigned int overflow (it wraps around)

Unsigned arithmetic is defined to wrap modulo 2^bits.

What is the STAR method?

Answer: Situation, Task, Action, Result: a way to tell project stories

It keeps behavioural answers structured and concrete.

An interviewer asks something you do not know. Best response?

Answer: Say what you do know, reason aloud, and say how you would verify

Reasoning and honesty are valued over bluffing.

In a STAR answer, what goes in Action?Related

Answer: What you personally did, in detail

Action is the main part: what you did, said with "I", so the interviewer can judge your skills.

From Full-stack interviews in India

In a 90-minute machine-coding round, what should you do first?Related

Answer: Clarify the requirements and plan briefly

A few minutes of questions and planning avoid building the wrong thing. Then make it work, then make it nice.

From Interview preparation

What does the rule of three say?Related

Answer: If a class needs a custom destructor, copy constructor or copy assignment, it probably needs all three

These three manage the same resource, so they come as a set.

From Copying objects: the rule of three, five and zero

What is the time complexity of BFS with an adjacency list?Related

Answer: O(V + E)

Each vertex and edge is processed a constant number of times.

From Core DSA patterns in C++

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “Competitive programming, interviews and careers” before its exam: Careers with C++ and your next steps.
  2. Module 10 exam: Contests, interviews and careers · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. 42 topics left in the course before the final exam.

← Previous: Classic C++ pitfalls in contests and coding rounds