In this article
Basics and memory
Coding rounds in C++ test the same routine as any other language: restate the problem, talk through your approach, then write and test the code. The coding interview guide covers that routine; the entries below cover the language knowledge that sits underneath it.
What is the difference between struct and class in C++?
A struct and a class are the same kind of type with different defaults. Members of a struct are public by default, and so is its base-class inheritance; members of a class are private by default, and so is its inheritance. Nothing else differs, so a struct can have constructors, member functions and virtual functions. By convention, struct is used for plain data and class for types that protect an invariant.
Why it is asked: it checks whether you know the real rule rather than a myth. Read the full explanation: the coding interview guide.
What is the difference between a pointer and a reference?
A pointer is an object that holds an address; it can be null, can be reassigned to point elsewhere, and can be left uninitialized. A reference is an alias for an existing object: it must be bound when created, cannot be null in valid code, and cannot be reseated. The standard does not say how a reference is implemented, so do not assume it is a pointer in memory.
Why it is asked: it tests whether you can choose between them for parameters and ownership. Read the full explanation: the coding interview guide.
What does const correctness mean?
Const correctness means marking everything that should not change as const, so the compiler enforces it. A const member function promises not to modify the object it is called on, and a const& parameter lets a function read a large argument without copying or changing it. Read pointer declarations right to left: const int* is a pointer to a constant int, while int* const is a constant pointer to an int.
Why it is asked: it shows whether you design interfaces that state their intent. Read the full explanation: the coding interview guide.
What is the difference between new/delete and malloc/free?
The new expression allocates memory and then runs the constructor; delete runs the destructor and then releases the memory. The malloc function only returns raw bytes and free only releases them, with no constructor or destructor. Memory from new[] must be released with delete[], and memory must never be released with the other family's function. In modern code you rarely write either directly: containers and smart pointers own the memory.
Why it is asked: it separates object creation from raw allocation. Read the full explanation: the coding interview guide.
What is the difference between the stack and the heap?
Local variables have automatic storage duration: they are created when execution reaches their declaration and destroyed at the end of their scope, and implementations usually keep them on the call stack. Objects made with new have dynamic storage duration and live until you release them, usually in a memory region called the heap. Stack allocation is simple and scoped; heap allocation is flexible but needs an owner. The standard describes storage duration, not stack and heap.
Why it is asked: it tests whether you connect memory placement to object life. Read the full explanation: the coding interview guide.
Ownership and resources
What is RAII?
Resource Acquisition Is Initialization (RAII) is a technique that ties a resource, such as heap memory, an open file or a locked mutex, to the scope of an object. The constructor acquires the resource and the destructor releases it, and the destructor should never throw (cppreference, RAII, accessed October 2026). Because destructors run when scope ends, even through an exception, resources are released without manual cleanup code.
Why it is asked: it is the central idea of safe C++. Read the full explanation: cppreference, RAII.
What are unique_ptr, shared_ptr and weak_ptr?
A smart pointer is a class that owns a dynamically allocated object and releases it automatically, following RAII. The unique_ptr is the single owner and can be moved but not copied. The shared_ptr allows several owners and frees the object when the last one goes away, at the cost of a reference count. The weak_ptr observes a shared_ptr without owning it, which breaks ownership cycles that would otherwise leak. Prefer unique_ptr unless ownership is truly shared.
Why it is asked: it shows whether you can say who owns each object. Read the full explanation: cppreference, RAII.
What are the rule of three, the rule of five and the rule of zero?
The rule of three says that if a class needs a custom destructor, copy constructor or copy assignment operator, it probably needs all three. The rule of five adds the move constructor and move assignment operator. The rule of zero is the better goal: give the class members that manage themselves, such as std::string, std::vector and smart pointers, and write none of the five. Then the compiler's defaults are correct.
Why it is asked: it tests resource-owning class design. Read the full explanation: cppreference, RAII.
What is the difference between an lvalue and an rvalue?
An lvalue is an expression that names an object with an identity you can take the address of, such as a variable. An rvalue is a temporary or an expression whose resources can be taken, such as the result of a function returning by value. An rvalue reference, written T&&, binds to rvalues, which lets a move constructor steal a temporary's contents instead of copying them. The full rules have more categories, such as xvalues.
Why it is asked: it is the base for move semantics. Read the full explanation: cppreference, RAII, which links move semantics.
What does std::move actually do?
The std::move function does not move anything: it casts its argument to an rvalue reference, which tells overload resolution that the object may be moved from. The move itself is done by the move constructor or move assignment operator that the cast selects. After a move the source object is still valid but its value is unspecified, so assign to it or destroy it, and do not read from it.
std::string a = "hello";
std::string b = std::move(a); // b takes a's buffer; a is valid but unspecifiedWhy it is asked: the name is a well-known trap. Read the full explanation: cppreference, RAII.
What is a memory leak and what is a dangling pointer?
A memory leak happens when dynamically allocated memory is no longer reachable and is never released, so the program holds memory it cannot use. A dangling pointer is the opposite fault: it points to an object that has already been destroyed, so using it is a bug. Both come from unclear ownership. Giving every allocation one owner, an RAII object, prevents most of them.
Why it is asked: it tests whether you can diagnose and prevent the classic faults. Read the full explanation: cppreference, RAII.
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 →Classes and polymorphism
How do virtual functions work?
A virtual function is a member function whose version is chosen at run time from the real type of the object, not from the type of the pointer or reference used to call it. A call through a base pointer runs the version belonging to the object's real type. Compilers usually implement this with a table of function pointers, often called a vtable, but the standard does not require it.
Text description of the diagram
- A call through a base pointer runs the version belonging to the object's real type.
- Compilers usually implement this with a table of function pointers, but the standard does not require it.
Why it is asked: it is the core of run-time polymorphism. Read the full explanation: the Java object oriented questions compare the same idea in another language.
Why should a base class have a virtual destructor?
If you delete a derived object through a base-class pointer and the base destructor is not virtual, the behavior is undefined: the derived destructor may never run, so its resources leak or worse. Give a polymorphic base class a public virtual destructor, or a protected non-virtual one if nobody should delete through the base. Marking overrides with override helps the compiler catch signature mistakes.
Why it is asked: it is a short question that exposes real experience with inheritance. Read the full explanation: the Java object oriented questions show the contrast with a garbage-collected language.
What is a pure virtual function and an abstract class?
A pure virtual function is declared with = 0 and has no required implementation in the class that declares it. A class with at least one pure virtual function is abstract, so you cannot create an object of it, only of a derived class that overrides every pure virtual function. Abstract classes act as interfaces: callers depend on the base type, and implementations vary.
Why it is asked: it tests interface design. Read the full explanation: the Java object oriented questions.
What is object slicing?
Object slicing happens when you copy a derived object into a base-class object by value: only the base part is copied and the derived members are cut off, along with the derived behavior. Virtual calls on the copy run the base version. Avoid it by passing and storing polymorphic objects by pointer, reference or smart pointer, and by making the base class non-copyable if copies make no sense.
Why it is asked: it checks whether you know why polymorphism needs indirection. Read the full explanation: the Java object oriented questions.
In what order are constructors and destructors called?
Construction runs from the base outward: base-class constructors first, then member objects in declaration order, then the body of the derived constructor. Destruction runs in the reverse order. One consequence: a virtual call made inside a constructor or destructor runs the version of the class being built or torn down, not a more derived one, because the derived part does not exist yet or is already gone.
Why it is asked: it tests your model of object construction. Read the full explanation: the C# questions show where another language differs.
Containers and undefined behavior
What is the difference between size and capacity in a vector?
The size of a std::vector is the number of elements it holds; its capacity is how many it can hold before it must allocate new storage. Elements are stored contiguously, so a vector allocates extra room for growth and reallocates only when that room runs out, which is why adding at the end is amortized constant time. The standard sets no growth factor, so implementations differ. Call reserve when you know the count (cppreference, std::vector, accessed October 2026).
Why it is asked: it tests cost awareness. Read the full explanation: cppreference, std::vector.
When are vector iterators invalidated?
Growing a vector past its capacity reallocates its storage, which invalidates every iterator, pointer and reference to its elements. Inserting or erasing in the middle invalidates the iterators from the changed position onward, and also moves later elements, a linear-time operation. A common bug is holding a reference to an element across a push_back. The cppreference page for std::vector has an iterator-invalidation section (std::vector, accessed October 2026).
Why it is asked: it is a frequent source of crashes. Read the full explanation: cppreference, std::vector.
What is the difference between map and unordered_map?
A std::map keeps keys sorted and looks them up in logarithmic time; it needs only a way to compare keys. A std::unordered_map hashes keys into buckets and has average constant lookup, but worst-case linear time when many keys collide, and it needs a hash function and equality test. Choose map when you need ordered traversal or range queries, and unordered_map for fast lookup by key.
Why it is asked: it tests whether you pick a structure by its costs. Read the full explanation: the coding interview guide.
What is undefined behavior?
Undefined behavior is what happens when a program breaks a rule of the language for which the standard places no requirements: anything may occur, including a crash, a wrong answer or code that appears to work. Examples are reading past the end of an array, using an object after it was destroyed and signed integer overflow. Compilers may assume it never happens and optimize on that basis, so test with sanitizer tools rather than trusting a run that looked fine.
Why it is asked: it separates careful C++ writers from hopeful ones. Read the full explanation: the coding interview guide.
Follow-up questions
What are the common C++ interview questions?
The twenty above cover the topics that recur: memory and ownership, RAII and smart pointers, move semantics, virtual functions, containers and undefined behavior. If you interview in several languages, compare with the JavaScript, Java and C# lists.
What do coding interview questions in C++ look like?
Usually a short problem, such as transforming an array, using a hash map or implementing a small class, where you write C++ and explain the cost of your choices. Say which container you picked and why, handle the empty input, and name who owns any memory you allocate. The coding interview guide walks through the routine.
What are C++ system design interview questions?
They are design problems, such as a cache or a message queue, where C++ shows up in the performance-sensitive choices: memory layout, ownership, locking and copying. The design steps are the same in any language; the coding interview guide places design in the wider loop, and the computer networks questions cover the network knowledge those designs lean on.
Are C++ interview questions the same at every company?
No. The topics above are the common core, but the depth depends on the role, so read the job description and expect more on concurrency, memory layout or the language standard if the role is performance-critical. Treat any list, including this one, as a map of topics rather than a prediction.
- Use RAII: let objects own resources so cleanup runs when scope ends.
- Prefer
unique_ptr, containers and the rule of zero over rawnewanddelete. std::moveonly casts; the move constructor does the moving.- Give a polymorphic base class a virtual destructor, and pass polymorphic objects by reference or pointer.
- A vector's size and capacity differ, and growth invalidates iterators; undefined behavior has no safe outcome.
Sources
- RAII (cppreference.com) — cppreference.com (accessed 2026-10-26), 26 Oct 2026
- std::vector (cppreference.com) — cppreference.com (accessed 2026-10-26), 26 Oct 2026
- Virtual function specifier (cppreference.com) — cppreference.com (accessed 2026-10-26), 26 Oct 2026