Huru
Technical Interviews

C++ Interview Questions and Answers (2026)

C++ interview questions with short answers and code: pointers vs references, the stack and heap, RAII, smart pointers (unique_ptr, shared_ptr, weak_ptr), the rule of zero and five, move semantics, virtual functions and destructors, const, templates, the STL, undefined behavior, and concurrency, with rules from the C++ Core Guidelines.

By Amine Boukioud Updated 5 min read
The Huru mascot holding a glass capsule with a padlocked memory block and pointing at glowing memory blocks linked by arrows on a shelf, with gears on the floor

The short answer: C++ interviews focus on how you manage resources and objects safely: pointers and references, the stack and heap, RAII and smart pointers, copy and move semantics (the rule of zero and the rule of five), virtual functions and destructors, const-correctness, templates and the STL, plus undefined behavior and concurrency for experienced roles. Interviewers like answers grounded in modern C++ (C++11 and later), which favors automatic resource management over manual new and delete. The C++ Core Guidelines, edited by Bjarne Stroustrup and Herb Sutter, are a good reference for the “right” answers. Below are the common questions with short answers and code.

Basics

“What’s the difference between a pointer and a reference?”

A pointer holds an address, can be null, can be reseated and needs dereferencing; a reference is an alias that must be initialized, can’t be null in valid code, and can’t be reseated. Use references for parameters that must refer to an object, pointers when “no object” is a valid state.

“What’s the difference between the stack and the heap?”

Stack memory holds local variables and is freed automatically when they go out of scope; it’s fast but limited. Heap memory is allocated dynamically and lives until it’s freed, which is why it needs an owner.

“What does const mean in different positions?”

const int* p is a pointer to a constant int; int* const p is a constant pointer; a const member function promises not to modify the object’s observable state.

Resource management

“What is RAII?”

Resource Acquisition Is Initialization: tie a resource (memory, a file, a lock) to an object’s lifetime, so the constructor acquires it and the destructor releases it, even when exceptions are thrown. The C++ Core Guidelines’ rule R.1 is: “Manage resources automatically using resource handles and RAII.”

{
    std::lock_guard<std::mutex> lock(m);  // locks here
    counter++;
}                                         // unlocks automatically here

“Which smart pointer should you use?”

  • std::unique_ptr: single owner, no overhead, movable but not copyable. The default choice.
  • std::shared_ptr: shared ownership with reference counting.
  • std::weak_ptr: a non-owning reference to a shared_ptr object, used to break cycles.

The Core Guidelines advise: “Prefer unique_ptr over shared_ptr unless you need to share ownership” (R.21), and “Use make_unique() to make unique_ptrs” (R.23).

auto widget = std::make_unique<Widget>(42);
std::shared_ptr<Cache> cache = std::make_shared<Cache>();

“Why avoid raw new and delete?”

They make leaks and double frees easy, especially with exceptions. Rule R.11 says: “Avoid calling new and delete explicitly.”

Copy and move semantics

“What are the rule of zero and the rule of five?”

Rule of zero (C.20): “If you can avoid defining default operations, do.” Let members such as std::string, std::vector and smart pointers manage themselves. Rule of five (C.21): if you define or delete any of the copy constructor, copy assignment, move constructor, move assignment or destructor, define or delete them all.

“What is move semantics?”

Moving transfers a resource from one object to another instead of copying it, leaving the source in a valid but unspecified state. It uses rvalue references (T&&); std::move casts an object to an rvalue so it can be moved from.

std::vector<std::string> names = load();
std::vector<std::string> other = std::move(names);  // no deep copy

Object-oriented features

“How do virtual functions work?”

They enable runtime polymorphism: calls through a base pointer or reference dispatch to the derived implementation, usually via a per-class table of function pointers (a vtable). Use override to let the compiler check overrides.

“Why should a base class destructor be virtual?”

If you delete a derived object through a base pointer and the destructor isn’t virtual, the derived destructor doesn’t run, which is undefined behavior. Guideline C.35: “A base class destructor should be either public and virtual, or protected and non-virtual.”

“What’s the difference between overloading and overriding?”

Overloading is several functions with the same name and different parameters, resolved at compile time; overriding is a derived class replacing a virtual function, resolved at runtime.

Templates and the STL

  • “What are templates?” Code generated for each type it’s used with, enabling generic containers and algorithms at compile time.
  • “vector vs list?” std::vector stores elements contiguously, with fast random access and cache-friendly iteration; std::list allows fast insertion anywhere but poor locality. Prefer vector by default.
  • “map vs unordered_map?” std::map is ordered (usually a balanced tree, O(log n)); std::unordered_map is a hash table with average O(1) lookups and no ordering.
  • “What is iterator invalidation?” Some operations (for example, inserting into a vector that reallocates) invalidate existing iterators, pointers and references.

Undefined behavior and concurrency

  • “Give examples of undefined behavior.” Reading uninitialized variables, out-of-bounds access, dereferencing null or dangling pointers, signed integer overflow and data races. Guideline ES.20: “Always initialize an object.”
  • “What’s a data race and how do you prevent it?” Two threads access the same memory, at least one writes, without synchronization. Use mutexes (with std::lock_guard or std::scoped_lock), atomics, or avoid shared mutable state.

Coding exercises you might get

Reverse a linked list, implement a simple string class or a smart pointer (tests the rule of five), an LRU cache with unordered_map and list, or a thread-safe queue.

See also our software engineer interview questions, Java interview questions and Java 8 interview questions.

Frequently asked questions

What C++ questions are asked in interviews?
Pointers vs references, stack vs heap, const, RAII, smart pointers, the rule of zero and five, move semantics, virtual functions and destructors, overloading vs overriding, templates, STL containers, undefined behavior and concurrency.
What is RAII in C++?
Resource Acquisition Is Initialization: an object acquires a resource in its constructor and releases it in its destructor, so resources are freed automatically, even when exceptions occur.
When should I use unique_ptr vs shared_ptr?
Use unique_ptr by default for single ownership; use shared_ptr only when ownership is genuinely shared, and weak_ptr to observe shared objects or break reference cycles.
What is the rule of five?
If a class defines or deletes any of the copy constructor, copy assignment, move constructor, move assignment or destructor, it should define or delete all five. The rule of zero says to avoid defining them at all when members can manage themselves.
Why should a base class destructor be virtual?
So deleting a derived object through a base pointer runs the derived destructor. Otherwise the behavior is undefined. The Core Guidelines say it should be public and virtual, or protected and non-virtual.

Sources

Practice this with Huru AI

Turn what you just read into a rehearsal. Unlimited AI mock interviews with instant, specific feedback.

Amine Boukioud

Amine Boukioud is a co-founder of Beatview, Inc., the company behind Huru, and Head of Growth at Huru. He writes and reviews Huru’s interview guides; each one starts from primary sources such as employers’ own careers pages, official data and research, and follows Huru’s editorial policy.

Published · Updated

How this article was made: researched and drafted with AI assistance, then written, fact-checked and signed by Amine Boukioud on October 1, 2026. Sources are linked where they are used. How we work: editorial policy.