C++ Interview Questions
15 essential questions. Clear answers, practical trade-offs and a lesson for every concept.
- 01
What is pass by value versus pass by reference?
Pass by value creates a separate parameter object. A reference aliases the caller’s object, so changes affect it. Use const references for read-only access without copying large objects.
Stage 3 · Pass by reference - 02
How do pointers differ from references?
A pointer stores an address, can be null and can be reassigned. A reference must be initialized to refer to an object and cannot be reseated. Neither guarantees that the referenced object stays alive.
Stage 5 · References versus pointers - 03
What is RAII?
Resource Acquisition Is Initialization ties resources to object lifetime. A constructor establishes ownership and the destructor releases it, including during exception unwinding. Smart pointers, streams and lock guards are examples.
Stage 5 · RAII - 04
When should you use unique_ptr, shared_ptr and weak_ptr?
Use unique_ptr for exclusive ownership and shared_ptr when ownership is genuinely shared. weak_ptr observes shared ownership without extending lifetime; call lock() to obtain a usable shared_ptr and avoid ownership cycles.
Stage 5 · shared_ptr and weak_ptr - 05
What is a memory leak versus a dangling pointer?
A leak is an allocated resource that is never released. A dangling pointer refers to an object whose lifetime has ended; accessing that object is undefined behavior. Prefer scoped ownership instead of manual new/delete.
Stage 5 · Memory leaks and dangling pointers - 06
What is encapsulation?
Encapsulation hides state and implementation behind a controlled public interface. Private members and validated operations help preserve class invariants rather than allowing arbitrary changes.
Stage 6 · Encapsulation - 07
How does runtime polymorphism work?
A virtual function called through a base-class pointer or reference dispatches to the derived override. Use override to catch signature mistakes; passing a derived object as a base value can slice it.
Stage 6 · Runtime polymorphism - 08
Why should a base-class destructor be virtual?
If objects will be deleted through a base pointer, a virtual destructor ensures the derived destructor runs. Deleting a derived object through a base with a nonvirtual destructor is generally undefined behavior.
Stage 6 · Virtual destructors - 09
What is the difference between copying and moving?
Copying creates an independent value; moving can transfer resources from an expiring object. std::move only enables move overload selection by casting to an rvalue. A moved-from standard-library object is generally valid but has an unspecified value.
Stage 9 · Move semantics - 10
How does vector differ from a fixed-size array?
A vector manages contiguous dynamic storage and can grow; an array has a fixed extent. Vector indexing is O(1) and push_back is amortized O(1). Reallocation invalidates all pointers, references and iterators to its elements.
Stage 4 · Vectors - 11
When should you choose map over unordered_map?
map keeps keys sorted and has O(log n) lookup, insertion and removal. unordered_map uses hashing with average O(1) operations but worst-case O(n). Choose map for sorted traversal or range queries.
Stage 8 · map and multimap - 12
What is a lambda and what do captures mean?
A lambda creates a function object. [x] captures x by value; [&x] captures it by reference. Reference captures can dangle if the lambda outlives the captured object. [] means no captures.
Stage 8 · Lambda expressions - 13
What does Big-O describe?
Big-O is an asymptotic upper bound on growth in time or space as input size increases. It ignores constant factors and lower-order terms; specify whether you mean worst-case, average or amortized complexity.
Stage 10 · Big-O notation - 14
What does binary search require?
The range must be sorted or suitably partitioned for the comparison. Each step halves the candidate range, giving O(log n) comparisons. With random-access storage such as vector, lookup time is O(log n).
Stage 10 · Linear and binary search - 15
What is the difference between BFS and DFS?
BFS uses a queue to explore in layers and finds shortest paths in unweighted graphs. DFS explores deeply using recursion or a stack. Both take O(V + E) with adjacency lists and visited tracking.
Stage 10 · Breadth-first search