The questions that come up most often, each with a sample answer you can adapt into your own words. Read them out loud until the explanation feels natural.
What is RAII?
IntermediateRAII (Resource Acquisition Is Initialisation) ties a resource's lifetime to an object's scope: the constructor acquires the resource and the destructor releases it, so cleanup happens automatically when the object goes out of scope, even on an exception. It is the foundation of safe C++: file handles, locks, and memory are wrapped in objects so you never leak them. Smart pointers and std::lock_guard are RAII in action.
What is the difference between unique_ptr and shared_ptr?
Intermediateunique_ptr expresses sole ownership: it cannot be copied, only moved, and it frees its object when it goes out of scope, with zero overhead over a raw pointer. shared_ptr expresses shared ownership via a reference count; the object is freed when the last shared_ptr is destroyed, at the cost of an atomic counter. Prefer unique_ptr by default and use shared_ptr only when ownership is genuinely shared; break reference cycles with weak_ptr.
Explain move semantics and what std::move does.
AdvancedMove semantics let you transfer a resource (like a heap buffer) from one object to another instead of copying it, by stealing the source's internals and leaving it in a valid empty state. std::move does not move anything itself; it is a cast that turns an lvalue into an rvalue reference so the move constructor or move assignment is selected. It is essential for returning large objects cheaply and for storing move-only types like unique_ptr in containers.
What is the Rule of Three / Five / Zero?
AdvancedThe Rule of Three says that if a class needs a custom destructor, copy constructor, or copy assignment, it almost certainly needs all three, because it manages a resource. The Rule of Five extends this to also include the move constructor and move assignment in modern C++. The Rule of Zero is the preferred goal: design classes so they need none of these by holding RAII members (smart pointers, containers) that manage their own resources.
What is the difference between a pointer and a reference?
FundamentalsA reference is an alias for an existing object: it must be initialised, cannot be null, and cannot be reseated to refer to something else. A pointer is a variable holding an address: it can be null, can be reassigned, and supports arithmetic. Use a reference when the binding is fixed and the object always exists; use a pointer when it may be null or needs to change what it points to.
What is iterator invalidation?
AdvancedIterator invalidation is when an operation on a container makes existing iterators (or pointers/references into it) invalid, so using them is undefined behaviour. For example, vector::push_back may reallocate the backing array, invalidating all iterators; erasing from a vector invalidates iterators at and after the erased point. You must know each container's rules and refresh iterators after modifying operations, often by using the return value of erase.
When would you use vector versus list versus unordered_map?
IntermediateUse vector by default: contiguous storage gives O(1) random access and excellent cache locality, with amortised O(1) append. Use list (a doubly linked list) only for frequent middle insertions/removals where you already hold the position, accepting poor cache behaviour. Use unordered_map for average O(1) key-value lookup by hash; use map (a balanced tree) when you need sorted order or O(log n) worst case.
What is a virtual function and how does it work?
AdvancedA virtual function enables runtime polymorphism: a call through a base-class pointer or reference dispatches to the derived class's override. It works via a vtable, a per-class table of function pointers, and a vptr in each object that points to it, so the right function is chosen at runtime. A class with virtual functions should have a virtual destructor, otherwise deleting a derived object through a base pointer is undefined behaviour.