How to Pass a C++ Interview: Breakdown of Top Questions with Examples
A C++ developer interview is a test not only of syntax, but also of a deep understanding of how memory, the compiler, and the standard library work. In this article, we will break down the most common questions asked in interviews in 2024-2025. You will learn not just the correct answers, but also see code examples that will help you confidently pass the technical interview.
1. Virtual Functions and Polymorphism
This is a classic. Interviewers want to make sure you understand the difference between static and dynamic binding, as well as the mechanism of the vtable (virtual function table).
Question: What happens if you call a virtual function from a constructor or destructor?
Answer: Calling a virtual function from a base class constructor will not be polymorphic. At the moment the base class constructor is called, the derived class object has not yet been created, so the base class version of the function will be called. The same applies to the destructor — after the derived class destructor is called, the object is "demoted" to the base class.
#include <iostream>
class Base {public: Base() { print(); } // Calling a virtual function in the constructor virtual void print() { std::cout << "Base"; } virtual ~Base() = default;};
class Derived : public Base {public: Derived() { print(); } void print() override { std::cout << "Derived"; }};
int main() { Derived d; // Output: Base (from Base constructor) // Derived (from Derived constructor) return 0;}2. Smart Pointers
No modern interview is complete without this topic. Smart pointers are the foundation of RAII and resource management in C++.
Question: What is the difference between std::unique_ptr, std::shared_ptr, and std::weak_ptr? When should you use each?
Answer:
- unique_ptr — exclusive ownership. Cannot be copied, only moved. Ideal for resources that should be freed upon leaving scope.
- shared_ptr — shared ownership. Uses a reference count. Deletes the object when the count reaches zero.
- weak_ptr — an "observer" for shared_ptr. Does not increase the reference count. Used to prevent circular dependencies.
#include <memory>#include <iostream>
class Resource {public: Resource() { std::cout << "Resource acquired"; } ~Resource() { std::cout << "Resource released"; }};
int main() { // unique_ptr std::unique_ptr<Resource> uptr = std::make_unique<Resource>(); // shared_ptr std::shared_ptr<Resource> sptr1 = std::make_shared<Resource>(); std::shared_ptr<Resource> sptr2 = sptr1; // Reference count = 2 // weak_ptr std::weak_ptr<Resource> wptr = sptr1; // Does not increase the count if (auto locked = wptr.lock()) { std::cout << "Resource is still alive"; } return 0;}3. Move Semantics and Rvalue References
Move semantics is one of the key features of C++11. In an interview, you may be asked to write a move constructor or explain what a forwarding reference is.
Question: What is move semantics and why is it needed? Give an example where it speeds up code.
Answer: Move semantics allows transferring ownership of resources (e.g., dynamic memory) without copying them. Instead of deep copying, we "move" pointers, nullifying the source object. This is critically important for performance when working with large containers.