C++ Interview Questions & Answers
All 200 unique questions in one place — 60 easy, 96 medium, 44 hard — grouped by topic.
Practise by topic
Practise the way your interview works
What is the difference between a declaration and a definition?
Show answer
What are the fundamental data types in C++ and their typical sizes?
Show answer
What is the difference between ++i and i++?
Show answer
A teammate added using namespace std; to a shared header file. What problems can this cause?
Show answer
What are default arguments and what rules apply to them?
Show answer
What is the difference between struct and class in C++?
Show answer
Compare pass by value, pass by pointer and pass by reference.
Show answer
Write a function to reverse a string in place without using std::reverse.
Show answer
Write an efficient function to check whether a number is prime.
Show answer
Write a swap function using references and explain why a pass-by-value version fails.
Show answer
Why did you choose C++ for your career instead of a language like Python or Java?
Show answer
A program that prints a million lines with std::endl is very slow. Why, and how do you fix it?
Show answer
Explain the stages of compiling and running a C++ program.
Show answer
What is the difference between const and constexpr?
Show answer
What is function overloading and how does the compiler choose an overload?
Show answer
What does the static keyword mean in different contexts?
Show answer
What is the difference between an inline function and a macro?
Show answer
The linker reports undefined reference to a function that is declared in a header. What could be wrong?
Show answer
Two third-party libraries both define a global function called init and your build fails with duplicate symbols. How do you fix it?
Show answer
What are lvalues and rvalues?
Show answer
Write a C++ program that counts how often each word appears in the input.
Show answer
Tell me about a time you found a difficult bug in your C++ code.
Show answer
What is undefined behaviour in C++? Give examples.
Show answer
Your program gives correct output in a debug build but wrong output in a release build. How do you investigate?
Show answer
What is a pointer and how is it different from a reference?
Show answer
What is the difference between stack and heap memory?
Show answer
What is a dangling pointer?
Show answer
What is pointer arithmetic?
Show answer
What is the difference between delete and delete[]?
Show answer
What is a void pointer and when would you use it?
Show answer
Find the middle node of a linked list in a single pass.
Show answer
What is a memory leak and how do you detect one?
Show answer
What is the difference between new/delete and malloc/free?
Show answer
What is RAII and why is it central to C++?
Show answer
Explain unique_ptr, shared_ptr and weak_ptr.
Show answer
Why should you prefer make_unique and make_shared over calling new directly?
Show answer
Two objects hold shared_ptr references to each other and their memory is never released. Why, and how do you fix it?
Show answer
What is the difference between const int*, int* const and const int* const?
Show answer
What is the difference between a shallow copy and a deep copy?
Show answer
Reverse a singly linked list using pointers.
Show answer
Your program crashes with the message double free or corruption. What could cause it?
Show answer
A function returns a reference to a local variable and the code sometimes works. Explain what is happening.
Show answer
Describe a time you improved code quality by changing how memory was managed.
Show answer
Write a function that allocates a 2D matrix safely without raw new and delete.
Show answer
Implement a minimal unique_ptr-like class.
Show answer
A production service's memory usage grows steadily over several days. How do you find the cause?
Show answer
What are memory alignment and padding?
Show answer
What is a custom deleter for a smart pointer and when do you need one?
Show answer
What are placement new and memory pools?
Show answer
Should a function take a shared_ptr by value, by const reference, or take a raw pointer or reference?
Show answer
What are the four pillars of object-oriented programming in C++?
Show answer
What kinds of constructors does C++ have?
Show answer
What is a pure virtual function and an abstract class?
Show answer
What is the difference between overloading and overriding, and what do override and final do?
Show answer
Write a Shape hierarchy with a virtual area function and use it polymorphically.
Show answer
How would you explain polymorphism to a junior developer who has just joined your team?
Show answer
Explain the rule of three, rule of five and rule of zero.
Show answer
How do virtual functions work internally?
Show answer
Why should a base class have a virtual destructor?
Show answer
What is object slicing?
Show answer
Memory leaks appear when derived objects stored as Base* are deleted. What is wrong and how do you fix it?
Show answer
Design a class structure for UPI, card and net banking payments in a checkout system.
Show answer
Should a Car class inherit from Engine or contain an Engine? Explain your choice.
Show answer
A function taking a Money object is silently called with an int and gives wrong results. What is happening?
Show answer
Overload operator+ and operator<< for a Complex number class.
Show answer
Implement a thread-safe singleton in modern C++.
Show answer
Tell me about a time you designed the class structure for a module and how you handled review feedback.
Show answer
What is the diamond problem and how does virtual inheritance solve it?
Show answer
You call a virtual function from a base class constructor but the derived override never runs. Why?
Show answer
You inherit a 3000-line class that handles parsing, validation, database and logging. How would you refactor it?
Show answer
A hot loop calls a virtual function millions of times and profiling shows the indirect call cost. What can you do?
Show answer
Implement a simple String class that follows the rule of five.
Show answer
Implement a basic Observer pattern for an event publisher.
Show answer
Show the copy-and-swap idiom and explain why it gives strong exception safety.
Show answer
Describe a situation where you disagreed with a senior engineer about a design decision.
Show answer
What are templates in C++? Explain function and class templates.
Show answer
What are non-type template parameters?
Show answer
Write a generic function template that returns the larger of two values.
Show answer
Implement a generic Stack class template backed by std::vector.
Show answer
Compute a factorial at compile time.
Show answer
How do you keep your C++ knowledge up to date with new standards?
Show answer
Why are templates usually defined in header files?
Show answer
What is the difference between full and partial template specialization?
Show answer
What are C++20 concepts?
Show answer
When do you need the typename keyword inside a template?
Show answer
What are type traits and how does if constexpr use them?
Show answer
What is template argument deduction and class template argument deduction?
Show answer
A teammate complains that a template function produces hundreds of lines of compiler errors when misused. How do you improve this?
Show answer
A generic average function must accept only numeric types. How would you enforce that?
Show answer
You need a container that holds ints, strings and doubles together. Which approach would you choose?
Show answer
A generic maxOf template gives wrong results when called with two C-style string literals. Why, and how do you fix it?
Show answer
Write a variadic function that sums any number of arguments and one that prints them.
Show answer
Write a C++20 concept and use it to constrain a function.
Show answer
Tell me about a time you wrote generic, reusable code that saved effort for your team.
Show answer
What are variadic templates and fold expressions?
Show answer
What is SFINAE?
Show answer
The release binary has grown very large and analysis shows many template instantiations. What can you do?
Show answer
Build times have become very slow because heavy templates are included everywhere. How do you speed them up?
Show answer
How do you decide between templates and virtual functions for polymorphism?
Show answer
Implement your own is_pointer type trait.
Show answer
What are the main components of the Standard Template Library?
Show answer
Compare std::vector, std::list and std::deque.
Show answer
What is the difference between std::map and std::unordered_map?
Show answer
Sort a vector of pairs by the second value in descending order, breaking ties by the first value.
Show answer
Solve two sum: return indices of two numbers that add up to a target.
Show answer
Remove duplicates from a vector of integers.
Show answer
Describe a time you had to learn a new library or API quickly.
Show answer
When are std::vector iterators invalidated?
Show answer
How does std::vector grow, and what is the difference between size, capacity and reserve?
Show answer
What is the difference between push_back and emplace_back?
Show answer
What are the iterator categories in C++?
Show answer
When would you use std::set versus std::priority_queue?
Show answer
What is the erase-remove idiom and what replaced it in C++20?
Show answer
A program does millions of lookups in a std::map with string keys and is slow. How would you speed it up?
Show answer
Using a custom struct as the key of an unordered_map gives a compile error. Why, and how do you fix it?
Show answer
Code that erases elements from a vector inside a range-based for loop crashes. Why, and what is the correct way?
Show answer
Sorting a vector of large objects is slow. What can you do?
Show answer
Write a custom hash so a struct can be used as a key in std::unordered_map.
Show answer
Group a list of words into anagram groups.
Show answer
Tell me about a time when choosing a better data structure improved performance.
Show answer
You need to build an LRU cache with O(1) get and put. Which containers would you use?
Show answer
You must find the top 100 most frequent items in a stream of 10 million events. How would you do it?
Show answer
Code that takes bool& from a std::vector<bool> fails to compile. Why?
Show answer
Implement an LRU cache with get and put in O(1).
Show answer
Tell me about a code review where you pointed out an important issue in a colleague's code.
Show answer
What do auto and decltype do?
Show answer
What are lambdas and how do capture modes work?
Show answer
Why use nullptr instead of NULL or 0?
Show answer
What are structured bindings and how do they work with range-based for loops?
Show answer
Use lambdas with standard algorithms to sort strings by length and count those longer than a limit.
Show answer
Write a lookup function that uses std::optional instead of returning -1 or a null pointer.
Show answer
Show how std::format and std::print format output in modern C++.
Show answer
Explain move semantics and what std::move actually does.
Show answer
What are std::optional, std::variant and std::any?
Show answer
What does the C++20 ranges library add?
Show answer
What is std::string_view and what are its pitfalls?
Show answer
Is it expensive to return a large std::vector from a function by value?
Show answer
A function returns a string_view and callers sometimes print garbage. What is the likely bug?
Show answer
How would you choose between exceptions, error codes and std::expected for error handling?
Show answer
After calling std::move on a string, a teammate prints it and sees an empty value. Is this a bug?
Show answer
Write a move constructor and move assignment operator for a class that owns a buffer.
Show answer
Use std::variant and std::visit to handle different message types.
Show answer
Use a C++20 ranges pipeline to get squares of the first three even numbers.
Show answer
Tell me about a time you convinced your team to adopt a new C++ feature or practice.
Show answer
How do you handle a tight deadline when the C++ code you need to change is complex?
Show answer
What is perfect forwarding?
Show answer
What does noexcept mean and why should move constructors be noexcept?
Show answer
You are asked to modernise a large C++98 codebase to C++17. How would you plan it?
Show answer
An asynchronous callback crashes intermittently. It is a lambda that captures local variables by reference. Explain the problem and fix.
Show answer
Tell me about a mistake you made in production and what you learned from it.
Show answer
What is the difference between a process and a thread?
Show answer
How do you create a thread in C++ and what is the difference between join and detach?
Show answer
What are race conditions and data races?
Show answer
Four threads each increment a shared int one million times, but the final total is less than four million. Why?
Show answer
Write a thread-safe counter using std::atomic.
Show answer
Describe how you handled an urgent production issue while on call.
Show answer
Tell me about a time you worked with QA to fix a flaky test.
Show answer
Compare std::mutex, lock_guard, unique_lock and scoped_lock.
Show answer
What is a deadlock and how do you prevent it?
Show answer
How does std::condition_variable work and what are spurious wakeups?
Show answer
Explain std::async, std::future and std::promise.
Show answer
What does C++20 std::jthread add over std::thread?
Show answer
A service occasionally freezes and thread dumps show two threads each waiting on a mutex held by the other. How do you fix it?
Show answer
Design a producer-consumer pipeline where one thread reads log lines and several workers parse them.
Show answer
A program crashes with terminate called without an active exception when a function returns. What is the cause?
Show answer
Write a program where two threads print odd and even numbers alternately up to 10.
Show answer
Compute the sum of a large vector in parallel using std::async.
Show answer
Write a deadlock-free money transfer between two bank accounts.
Show answer
What is std::atomic and what do memory orderings mean?
Show answer
You parallelised a loop across eight threads but it became slower than the single-threaded version. What could be wrong?
Show answer
Many threads read a shared configuration object constantly, but it changes only once an hour. How do you synchronise it efficiently?
Show answer
A low-latency system needs a queue between two threads with no locks. How would you approach it?
Show answer
Implement a thread-safe blocking queue.
Show answer
Implement a simple fixed-size thread pool.
Show answer
Tell me about a concurrency bug you investigated and how you solved it.
Show answer
What is CMake and why is it used for C++ projects?
Show answer
What is the difference between a static library and a dynamic library?
Show answer
Which unit testing frameworks are common in C++?
Show answer
What are header guards and #pragma once?
Show answer
Write a minimal modern CMakeLists.txt for an app with a library and tests.
Show answer
Measure how long a function takes to run in C++.
Show answer
Show the compiler commands to build with strict warnings and sanitizers enabled.
Show answer
What do compiler optimisation levels like -O0, -O2, -O3 and -Os mean?
Show answer
What is cache locality and why does it matter for performance?
Show answer
Which profiling tools do you use for C++ and what does each show?
Show answer
What are AddressSanitizer, UndefinedBehaviorSanitizer and ThreadSanitizer?
Show answer
A C++ server crashes with a segmentation fault in production. How do you debug it?
Show answer
A full build of your C++ project takes 40 minutes. How would you reduce it?
Show answer
Your application is slow and you are asked to optimise it. Where do you start?
Show answer
A binary runs on your laptop but fails on the server with error while loading shared libraries. What is wrong?
Show answer
List the gdb commands you would use to debug a crash.
Show answer
Write a GoogleTest unit test for a function, including an edge case.
Show answer
How do you prioritise when a critical bug and a feature deadline arrive at the same time?
Show answer
Why should we hire you for this C++ developer role?
Show answer
A low-latency service shows occasional latency spikes of several milliseconds. How would you investigate?
Show answer
You are building a particle simulation with millions of particles. Would you store them as an array of structs or a struct of arrays?
Show answer
Use perf to find the hotspots in a C++ program.
Show answer
Write a micro-benchmark with Google Benchmark comparing two approaches.
Show answer
Tell me about a performance improvement you delivered and how you measured it.
Show answer
Frequently asked questions
What jobs can I get after learning C++?
C++ opens roles such as C++ developer, software engineer at product companies, embedded software engineer, automotive software developer, game developer, systems programmer and low-latency developer at trading firms. Many companies also use C++ for coding rounds, so strong C++ skills help in general software engineering interviews. Semiconductor, telecom and automotive companies in Bengaluru, Hyderabad, Pune and Noida hire C++ engineers every year.
Who is eligible to learn C++ and do I need prior coding experience?
Anyone who is comfortable with basic logic and mathematics can learn C++. Students of B.Tech, BCA, MCA, B.Sc and diploma programmes take it most often, and working professionals use it to move into systems or product roles. No prior coding experience is strictly required because the course starts from basics, although some familiarity with C or any programming language makes the first two weeks easier.
How long does it take to become job-ready in C++?
With regular practice of two to three hours a day, most learners grasp the fundamentals in six to eight weeks and become interview-ready in three to four months. Becoming job-ready also needs data structures and algorithms practice and two or three solid projects. Advanced areas such as templates, concurrency and low-latency optimisation keep improving with real work experience over the following years.
Is C++ still in demand in 2026?
Yes. C++ remains essential wherever performance and hardware control matter: operating systems, browsers, databases, game engines, AI inference runtimes, automotive and embedded systems, telecom equipment and financial trading. Modern standards such as C++20 and C++23 keep the language current. Demand is especially strong for engineers who combine C++ with multithreading, Linux and system design skills, and competition at the junior level rewards strong fundamentals.
Should I learn C++ or Java or Python for placements?
For coding rounds any of the three works, but C++ is popular because the STL is fast and concise and it is widely used in competitive programming. If you target embedded, gaming, semiconductor or trading roles, C++ is the natural choice. For web backends or enterprise roles Java is common, and Python is preferred for data and AI work. Many engineers learn C++ first for fundamentals and add another language later.
Will I get a certificate and does it help in getting a C++ job?
You receive a course completion certificate that shows you have covered the full syllabus and projects. Recruiters value it as proof of structured learning, but hiring decisions for C++ roles depend mainly on your coding round performance, understanding of memory, OOP and STL, and the quality of your projects. Keep your projects on GitHub with clear READMEs and tests so interviewers can review your actual code.