In this article
Life cycle and ownership
Coding rounds in C++ test the same habits as any other language, so pair this list with the coding interview guide.
What is RAII, and is it only about memory?
RAII (resource acquisition is initialization) is the idiom in which a constructor acquires a resource and the destructor releases it, binding the resource's life cycle to an object's (cppreference, RAII, accessed October 2026). The constructor acquires the resource and the destructor releases it. Resources are released in reverse order of acquisition when the scope ends, so with a file, a lock and a socket the socket goes first. RAII is not only for memory: open files, locked mutexes, sockets and threads count too, as the same cppreference RAII page lists.
Text description of the diagram
- The constructor acquires the resource and the destructor releases it.
- Resources are released in reverse order of acquisition when the scope ends.
Why it is asked: it shows whether you think in terms of ownership rather than manual cleanup. Reference: cppreference, RAII.
Is a raw pointer always bad in modern C++?
No. In the Core Guidelines a raw pointer T* is non-owning (guideline R.3), and so is a raw reference T& (guideline R.4). A raw pointer is a legitimate view of an object that someone else owns. The mistake is to delete through it, or to let it outlive its owner. Ownership belongs to a type that releases the object, such as unique_ptr.
Why it is asked: it separates "never use pointers" slogans from an ownership model. Reference: C++ Core Guidelines.
What is the difference between a pointer and a reference in C++?
A reference is an alias for an existing object: it must be initialized to refer to a valid object, cannot be reseated to refer to another object and so has no null state (cppreference, reference declaration, accessed October 2026). A pointer is an object that holds an address, can be reassigned and can be null. Use a reference when the object must exist, and a pointer when it may be absent or must change.
Why it is asked: it checks that you can state the basic rules of both types and choose between them. Reference: cppreference, reference declaration.
What is the difference between new and malloc?
new allocates memory and runs the constructor, and delete runs the destructor before releasing the memory (cppreference, new expression, accessed October 2026). malloc and free only allocate and release raw memory and run no constructors or destructors. Pair each allocation with its own release, new with delete and malloc with free, and never mix the pairs. In modern code prefer a smart pointer or a container over either.
Why it is asked: it shows whether you know that an object's lifespan is more than a block of bytes. Reference: cppreference, new expression.
What is the difference between unique_ptr and shared_ptr?
A smart pointer is an object that behaves like a pointer and also owns what it points to. A unique_ptr owns its object exclusively and disposes of it when it goes out of scope (cppreference, unique_ptr, accessed October 2026); it cannot be copied, only moved (cppreference, unique_ptr). A shared_ptr retains shared ownership, so several can own one object (cppreference, shared_ptr). Default to unique_ptr; choose shared_ptr only when ownership is truly shared.
Why it is asked: it tests whether you choose an ownership type deliberately. Reference: cppreference, unique_ptr.
How does shared_ptr know when to delete the object?
In a typical implementation a shared_ptr holds two pointers: the stored pointer and a pointer to a control block, which counts the owners. The object is deleted when the number of shared owners reaches zero (cppreference, shared_ptr). Copying a shared_ptr increases the count (cppreference, shared_ptr). Illustrative trace of use_count: 1, 2 and 3 after two copies, back to 1 after two copies are destroyed, then 0 when the last is destroyed and the object is deleted.1
Why it is asked: it checks that you can explain the cost behind shared ownership. Reference: cppreference, shared_ptr.
Why is it a bug to build two shared_ptr objects from the same raw pointer?
Each shared_ptr constructed from the raw pointer starts its own control block with its own count, so neither knows the other exists. When both counts reach zero, each deletes the object, which is a double delete. Create the first shared_ptr once, preferably with std::make_shared, and copy it, so every copy shares one control block and one count (cppreference, shared_ptr). When the pointer is this, derive the class from enable_shared_from_this and call shared_from_this().
Why it is asked: it is a classic ownership trap that shows whether you understand the control block. Reference: cppreference, shared_ptr.
Classes and virtual functions
What does std::move do, and what is a move?
A move transfers a resource from one object to another instead of copying it: a move constructor takes over the source's resource and leaves the source valid but unspecified for standard library types (by convention for your own). std::move itself moves nothing; it is a cast that turns its argument into an rvalue so that a move can be selected (cppreference, std::move, accessed October 2026). After a move, only destroy the source or assign a new value to it.
Why it is asked: it separates knowing the keyword from knowing that a cast enables the move. Reference: cppreference, std::move.
What are the rule of three and the rule of five?
If a class requires a user-defined destructor, copy constructor or copy assignment operator, it almost certainly requires all three (cppreference, rule of three, accessed October 2026). The rule of five adds the move constructor and move assignment operator; a move transfers a resource instead of copying it, as the question above explains. The Core Guidelines put it as a single rule: define or delete all the default operations together, as quoted on the same page.
Why it is asked: it checks whether you know how a class that owns a resource must behave. Reference: cppreference, the rule of three, five and zero.
Why is the default copy wrong for a class that holds a raw pointer?
The implicit copy is a shallow copy: it copies the pointer value "without duplicating the underlying resource", in the wording of cppreference. Two objects then point at one resource, so when both destructors run, the resource is released twice. A user-defined copy constructor and copy assignment operator that allocate and copy the resource, a deep copy, fix it.
Why it is asked: it is the motivating example for the rule of three. Reference: cppreference, the rule of three.
What is the rule of zero?
The rule of zero says that a class with a custom destructor or custom copy or move operations should do nothing but manage ownership, and every other class should define none of them (cppreference, rule of zero, accessed October 2026). In practice, hold resources in members that already own them, such as std::string or a smart pointer, and write none of the special functions.
Why it is asked: it shows whether you reach for RAII types instead of writing cleanup by hand. Reference: cppreference, rule of zero.
What does virtual do, and when does dynamic dispatch happen?
Virtual functions are member functions whose behavior can be overridden in derived classes, and the specifier supports dynamic dispatch (cppreference, virtual, accessed October 2026). It takes effect through a base pointer or reference: a call to an overridden virtual function then invokes the derived behavior. The default form of dispatch is static (Wikipedia, dynamic dispatch). Selecting the function with qualified name lookup, such as Base::f(), suppresses the virtual call.
Why it is asked: it tests whether you know that polymorphism needs a pointer or reference. Reference: cppreference, virtual function specifier.
Why does a base class need a virtual destructor?
A virtual destructor is a destructor declared virtual in a polymorphic base class, so that deleting a derived object through a base pointer runs the derived destructor. Without it, the deletion is undefined behavior, regardless of whether there are resources that would be leaked (cppreference, virtual function specifier). The same page says a base destructor must be public and virtual, or protected and non-virtual.
Why it is asked: "it only leaks memory" is a common wrong answer that interviewers listen for. Reference: cppreference, virtual function specifier.
What is a vtable, and is it part of the C++ standard?
A vtable is a table of function addresses that compilers typically use to implement dynamic dispatch. It is purely an implementation detail, as the C++ specification does not mention vtables (Wikipedia, dynamic dispatch, accessed October 2026). A strong answer says compilers "typically" use one, explains the effect (the call goes to the function for the object's real type), and does not present the table's layout as a language rule.
Why it is asked: it separates language rules from implementation folklore. Reference: Wikipedia, dynamic dispatch.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →Containers
Does push_back invalidate iterators?
It depends on whether the vector reallocates. If the new size would be greater than the old capacity, a reallocation takes place and all iterators, including end(), and all references to the elements are invalidated. Otherwise only the end() iterator is invalidated (cppreference, push_back, accessed October 2026). Illustrative case: with capacity 4 and size 4, the fifth push_back reallocates, so every saved iterator, pointer or reference is then invalid.1
Why it is asked: dangling iterators are a frequent real bug. Reference: cppreference, vector push_back.
What is the cost of push_back, and how fast does a vector grow?
Appending with push_back is amortized constant time: a vector allocates more memory than it needs so it does not reallocate on every insertion (cppreference, vector, accessed October 2026), which is also why it usually occupies more space than a static array. The growth pattern is left to the implementation, so do not quote a factor; print capacity() after each push_back on your compiler to see it.
Why it is asked: it checks that you know the trade of memory for speed and avoid stating an implementation detail as law. Reference: cppreference, vector.
Undefined behavior
What is undefined behavior, and how is it different from implementation-defined and unspecified?
Undefined behavior (UB) means there are no restrictions on the behavior of the program (cppreference, undefined behavior, accessed October 2026). Examples include data races, out-of-bounds access, signed integer overflow and null pointer dereference. Implementation-defined behavior varies between implementations, and the implementation must document it. Unspecified behavior need not be documented, such as the order of evaluation (cppreference mirror).
Why it is asked: the three categories are often confused. Reference: cppreference, undefined behavior.
Does signed integer overflow wrap around?
No. Signed overflow is undefined behavior, so do not describe it as wrapping. Unsigned arithmetic, by contrast, wraps around modulo two to the power of the bit width. Since C++20 the language fixes two's-complement representation for signed integers, but signed overflow is still undefined behavior (cppreference, fundamental types, accessed October 2026). For a signed integer, the expression x + 1 > x is either true or UB due to signed overflow, and compilers may produce unexpected results from a program that has UB when optimization is enabled (cppreference mirror, accessed October 2026).
Why it is asked: it shows whether you know that UB can change optimized code, not only crash. Reference: cppreference, undefined behavior.
Does undefined behavior always crash the program?
No. UB renders the entire program meaningless if certain rules of the language are violated, and the behavior is unrestricted (cppreference, accessed October 2026). The program may appear to work, print garbage or fail only under optimization. A test that passes is therefore no proof that the code is free of UB.
Why it is asked: "it will crash with an error" is a common wrong belief. Reference: cppreference, undefined behavior.
Follow-up questions
What C++ topics come up in technical interviews?
Topics vary by team and role, so ask the recruiter what the round covers. The nineteen above cluster around six areas: resource ownership and memory, smart pointers, special member functions and moves, virtual dispatch, container invalidation and undefined behavior.
How should I practice C++ for a coding round?
Write solutions in C++ under time pressure, and explain who owns each resource while you do. The coding interview guide covers the routine, and the full stack developer questions show how a wider technical round is structured.
Do I need to name a C++ standard version?
Name the version when your answer depends on it, and ask which one the team uses. The language standard evolves: the cppreference undefined behavior page lists erroneous behavior as a category since C++26, so a version-neutral answer should say what you assume.
How is C++ memory management different from Java or C#?
In C++, scope-bound ownership through RAII replaces garbage collection: the Core Guidelines (section P.8) say that combined with RAII it eliminates the need for "garbage collection" by generating no garbage (C++ Core Guidelines). Compare with the Java questions, the C# questions and the Dotnet interview questions.
What if I do not know the answer to a C++ question?
Say what you know, state the rule you would check, and name the reference page you would open. Showing how you reason about ownership and undefined behavior counts for more than guessing. For another language list, see the JavaScript questions.
- RAII ties a resource's life cycle to an object; it covers files, locks and sockets as well as memory.
- A raw
T*is a non-owning view; useunique_ptrby default andshared_ptrwhen ownership is shared. - A move transfers a resource, and
std::moveis only the cast that allows it. - If you need a custom destructor or copy operation, define or delete them all; better, hold resources in owning members and write none.
- A virtual destructor is required for deletion through a base pointer; vtables are an implementation detail.
push_backmay reallocate and invalidate iterators; undefined behavior is unrestricted, not a certain crash.
Sources
- RAII (cppreference.com) — cppreference.com, 26 Oct 2026
- C++ Core Guidelines (Stroustrup and Sutter, editors) — isocpp.github.io, 26 Oct 2026
- std::shared_ptr (cppreference.com) — cppreference.com, 26 Oct 2026
- std::unique_ptr (cppreference.com) — cppreference.com, 26 Oct 2026
- Reference declaration (cppreference.com) — cppreference.com, 26 Oct 2026
- std::move (cppreference.com) — cppreference.com, 26 Oct 2026
- new expression (cppreference.com) — cppreference.com, 26 Oct 2026
- The rule of three/five/zero (cppreference.com) — cppreference.com, 26 Oct 2026
- virtual function specifier (cppreference.com) — cppreference.com, 26 Oct 2026
- std::vector::push_back (cppreference.com) — cppreference.com, 26 Oct 2026
- std::vector (cppreference.com) — cppreference.com, 26 Oct 2026
- Undefined behavior (cppreference.com) — cppreference.com, 26 Oct 2026
- Undefined behavior (cppreference mirror, categories and optimization) — University of Helsinki mirror of cppreference, 26 Oct 2026
- Fundamental types (cppreference.com) — cppreference.com, 26 Oct 2026
- Dynamic dispatch (Wikipedia) — Wikipedia, 26 Oct 2026