Expert · Competitive programming, interviews and careers · topic 41 of 42 · 35 min
C++ interview questions and how to answer them
Explanation
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
- A clear one-line definition.
- A tiny example, said aloud or written in 2–4 lines.
- When to use it, or a trap to avoid. This is what shows real understanding.
The most-asked questions
| Question | Short 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
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 destructorQuestions 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").
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 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.
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 alivePractice (0/4 done)
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; }
Which is the best one-line answer to "What is undefined behaviour?"
What prints? int x = 5; int& r = x; int* p = &x; r = 7; *p = *p + 1; cout << x;
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Careers with C++ and your next stepsCompetitive programming, interviews and careers · same module
- Full-stack interviews in IndiaFull-Stack Development · Capstone, portfolio and getting work
- Interview preparationFrontend Web Development · Build, deploy and get hired
- Smart pointers 1: std::unique_ptrTemplates, smart pointers and move semantics
- The C# and .NET interview questions asked most oftenC# and .NET · Interviews, portfolio and careers
- Core concept questionsJavaScript Programming (language in depth) · Interview preparation and career
Questions people ask about this
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.
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.
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.
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.
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 more topic in “Competitive programming, interviews and careers” before its exam: Careers with C++ and your next steps.
- Module 10 exam: Contests, interviews and careers · 12 questions · 20 min, without help. 5 topics of this module still to finish first.
- 42 topics left in the course before the final exam.
← Previous: Classic C++ pitfalls in contests and coding rounds