|
| 1 | +========================== |
| 2 | +C/C++ Interview Cheatsheet |
| 3 | +========================== |
| 4 | + |
| 5 | +.. meta:: |
| 6 | + :description lang=en: Curated C and C++ interview questions, each linking directly to the exact cheatsheet section that answers it. Covers RAII, smart pointers, move semantics, templates, concepts, the STL, modern C++ features, concurrency, and debugging tools. |
| 7 | + :keywords: C++ interview questions, C interview questions, RAII, smart pointers, unique_ptr, shared_ptr, move semantics, Rule of Five, templates, SFINAE, concepts, STL, vector, unordered_map, iterator invalidation, lambda, coroutine, modules, virtual function, undefined behavior, memory leak, Valgrind, AddressSanitizer, gdb, CMake |
| 8 | + |
| 9 | +This page is a curated, question-indexed map into the rest of the cheatsheet. |
| 10 | +Each entry below is a question you are likely to see in a C or C++ interview, |
| 11 | +followed by a link that jumps directly to the section of the notes that |
| 12 | +answers it. It is intentionally a navigation layer — the actual explanations, |
| 13 | +code, and caveats live in the linked sections. |
| 14 | + |
| 15 | +Use it two ways: |
| 16 | + |
| 17 | +* **Drilling a topic:** pick a group (e.g. *Templates & Generics*) and walk |
| 18 | + every question in it. |
| 19 | +* **Quick review before an interview:** read the questions, and for any you |
| 20 | + cannot confidently answer in one or two sentences, click through. |
| 21 | + |
| 22 | +.. contents:: Topics |
| 23 | + :local: |
| 24 | + :depth: 1 |
| 25 | + |
| 26 | +Memory & Resource Management |
| 27 | +============================ |
| 28 | + |
| 29 | +* What is RAII, and why is it the cornerstone of modern C++ resource |
| 30 | + management? :ref:`→ cpp/cpp_raii: RAII Wrapper <notes/cpp/cpp_raii:RAII Wrapper>` |
| 31 | +* How does RAII interact with exception safety (basic / strong / nothrow |
| 32 | + guarantees)? :ref:`→ cpp/cpp_raii: Exception Safety Guarantees <notes/cpp/cpp_raii:Exception Safety Guarantees>` |
| 33 | +* What happens when a constructor throws after partial resource acquisition? |
| 34 | + :ref:`→ cpp/cpp_raii: Constructor Failure and Exception Safety <notes/cpp/cpp_raii:Constructor Failure and Exception Safety>` |
| 35 | +* Compare ``std::lock_guard``, ``std::unique_lock``, and ``std::scoped_lock``. |
| 36 | + :ref:`→ cpp/cpp_raii: Lock-Based RAII <notes/cpp/cpp_raii:Lock-Based RAII>` |
| 37 | +* When does ``std::unique_ptr`` make sense vs. ``std::shared_ptr``? |
| 38 | + :ref:`→ cpp/cpp_smartpointers: std::unique_ptr: Exclusive Ownership <notes/cpp/cpp_smartpointers:std::unique_ptr\: Exclusive Ownership>` |
| 39 | +* What problem does ``std::weak_ptr`` solve? |
| 40 | + :ref:`→ cpp/cpp_smartpointers: std::weak_ptr: Non-Owning Observer <notes/cpp/cpp_smartpointers:std::weak_ptr\: Non-Owning Observer>` |
| 41 | +* Why prefer ``std::make_unique`` / ``std::make_shared`` over raw ``new``? |
| 42 | + :ref:`→ cpp/cpp_smartpointers: std::make_unique and std::make_shared <notes/cpp/cpp_smartpointers:std::make_unique and std::make_shared>` |
| 43 | +* What are common smart-pointer pitfalls (e.g. cycles, double ownership)? |
| 44 | + :ref:`→ cpp/cpp_smartpointers: Common Pitfalls <notes/cpp/cpp_smartpointers:Common Pitfalls>` |
| 45 | +* Why does memory alignment matter, and how is struct padding computed? |
| 46 | + :ref:`→ c/c_memory: Structure Alignment and Padding <notes/c/c_memory:Structure Alignment and Padding>` |
| 47 | + |
| 48 | +Move Semantics & Value Categories |
| 49 | +================================= |
| 50 | + |
| 51 | +* What are the special member functions, and when are they implicitly defined |
| 52 | + or deleted? :ref:`→ cpp/cpp_move: Special Member Functions <notes/cpp/cpp_move:Special Member Functions>` |
| 53 | +* Explain the Rule of Zero, Three, and Five. |
| 54 | + :ref:`→ cpp/cpp_move: Rule of Zero <notes/cpp/cpp_move:Rule of Zero>` · |
| 55 | + :ref:`Three <notes/cpp/cpp_move:Rule of Three>` · |
| 56 | + :ref:`Five <notes/cpp/cpp_move:Rule of Five>` |
| 57 | +* What are lvalues, rvalues, xvalues, prvalues? |
| 58 | + :ref:`→ cpp/cpp_move: Value Categories <notes/cpp/cpp_move:Value Categories>` |
| 59 | +* How does ``std::forward`` achieve perfect forwarding, and why is it needed? |
| 60 | + :ref:`→ cpp/cpp_move: Perfect Forwarding <notes/cpp/cpp_move:Perfect Forwarding>` |
| 61 | +* What state is a moved-from object left in, and what can you safely do with |
| 62 | + it? :ref:`→ cpp/cpp_move: Moved-From State <notes/cpp/cpp_move:Moved-From State>` |
| 63 | +* Why should move constructors typically be ``noexcept``? |
| 64 | + :ref:`→ cpp/cpp_move: Conditional noexcept <notes/cpp/cpp_move:Conditional noexcept>` |
| 65 | +* ``emplace_back`` vs. ``push_back`` — when is there a real difference? |
| 66 | + :ref:`→ cpp/cpp_move: Emplace vs Insert <notes/cpp/cpp_move:Emplace vs Insert>` |
| 67 | + |
| 68 | +Templates & Generics |
| 69 | +==================== |
| 70 | + |
| 71 | +* How does template specialization differ from overloading? |
| 72 | + :ref:`→ cpp/cpp_template: Template Specialization <notes/cpp/cpp_template:Template Specialization>` |
| 73 | +* What are variadic templates and fold expressions? |
| 74 | + :ref:`→ cpp/cpp_template: Variadic Templates and Parameter Packs <notes/cpp/cpp_template:Variadic Templates and Parameter Packs>` · |
| 75 | + :ref:`Fold Expressions <notes/cpp/cpp_template:Fold Expressions (C++17)>` |
| 76 | +* What is SFINAE, and how do C++20 concepts replace most of it? |
| 77 | + :ref:`→ cpp/cpp_template: SFINAE and Type Constraints <notes/cpp/cpp_template:SFINAE and Type Constraints>` · |
| 78 | + :ref:`Defining Concepts <notes/cpp/cpp_concepts:Defining Concepts>` |
| 79 | +* What is CRTP and when would you use it? |
| 80 | + :ref:`→ cpp/cpp_template: CRTP <notes/cpp/cpp_template:CRTP (Curiously Recurring Template Pattern)>` |
| 81 | +* How does a ``requires`` clause constrain a template, and how does it compose |
| 82 | + with ``&&`` / ``||``? |
| 83 | + :ref:`→ cpp/cpp_requires: Basic Requires Clause <notes/cpp/cpp_requires:Basic Requires Clause>` · |
| 84 | + :ref:`Conjunction <notes/cpp/cpp_requires:Conjunction (&&)>` · |
| 85 | + :ref:`Disjunction <notes/cpp/cpp_requires:Disjunction (\|\|)>` |
| 86 | +* ``requires`` vs. ``if constexpr`` — when to reach for which? |
| 87 | + :ref:`→ cpp/cpp_requires: Requires vs if constexpr <notes/cpp/cpp_requires:Requires vs if constexpr>` |
| 88 | +* What are reference collapsing rules, and why do they matter for forwarding |
| 89 | + references? :ref:`→ cpp/cpp_basic: Reference Collapsing Rules <notes/cpp/cpp_basic:Reference Collapsing Rules>` |
| 90 | +* How does template type deduction differ between by-value, by-ref, and |
| 91 | + forwarding parameters? |
| 92 | + :ref:`→ cpp/cpp_basic: Template Type Deduction <notes/cpp/cpp_basic:Template Type Deduction>` |
| 93 | + |
| 94 | +Casting & Type Conversions |
| 95 | +========================== |
| 96 | + |
| 97 | +* Why prefer ``static_cast``, ``dynamic_cast``, ``const_cast``, and |
| 98 | + ``reinterpret_cast`` over C-style casts? |
| 99 | + :ref:`→ cpp/cpp_casting: C-Style Cast (Legacy Casting) <notes/cpp/cpp_casting:C-Style Cast (Legacy Casting)>` |
| 100 | +* When is ``dynamic_cast`` safe, and when does it return null or throw? |
| 101 | + :ref:`→ cpp/cpp_casting: dynamic_cast <notes/cpp/cpp_casting:dynamic_cast\: Runtime Type-Safe Downcasting>` |
| 102 | +* What does ``const_cast`` actually guarantee, and when is it undefined? |
| 103 | + :ref:`→ cpp/cpp_casting: const_cast <notes/cpp/cpp_casting:const_cast\: Removing or Adding const>` |
| 104 | + |
| 105 | +Compile-Time Programming |
| 106 | +======================== |
| 107 | + |
| 108 | +* ``const`` vs. ``constexpr`` vs. ``consteval`` vs. ``constinit`` — pick the |
| 109 | + right one. |
| 110 | + :ref:`→ cpp/cpp_constexpr: constexpr Functions <notes/cpp/cpp_constexpr:constexpr Functions>` · |
| 111 | + :ref:`consteval <notes/cpp/cpp_constexpr:consteval\: Immediate Functions (C++20)>` · |
| 112 | + :ref:`constinit <notes/cpp/cpp_constexpr:constinit\: Constant Initialization (C++20)>` |
| 113 | +* How does ``if constexpr`` eliminate branches at compile time? |
| 114 | + :ref:`→ cpp/cpp_constexpr: constexpr if (C++17) <notes/cpp/cpp_constexpr:constexpr if (C++17)>` |
| 115 | +* How has ``constexpr`` expanded across C++14, 17, 20, and 23? |
| 116 | + :ref:`→ cpp/cpp_constexpr: constexpr Evolution by Standard <notes/cpp/cpp_constexpr:constexpr Evolution by Standard>` |
| 117 | + |
| 118 | +STL Containers & Iterators |
| 119 | +========================== |
| 120 | + |
| 121 | +* Compare ``std::vector``, ``std::deque``, and ``std::list`` — memory layout, |
| 122 | + iterator invalidation, complexity. |
| 123 | + :ref:`→ cpp/cpp_container: std::vector <notes/cpp/cpp_container:std\:\:vector>` · |
| 124 | + :ref:`std::deque <notes/cpp/cpp_container:std\:\:deque>` · |
| 125 | + :ref:`std::list <notes/cpp/cpp_container:std\:\:list>` |
| 126 | +* ``std::map`` vs. ``std::unordered_map`` — trade-offs and when to use each. |
| 127 | + :ref:`→ cpp/cpp_container: Ordered vs Unordered Containers <notes/cpp/cpp_container:Ordered vs Unordered Containers>` |
| 128 | +* What are the iterator invalidation rules for the common containers? |
| 129 | + :ref:`→ cpp/cpp_iterator: Iterator Invalidation <notes/cpp/cpp_iterator:Iterator Invalidation>` |
| 130 | +* What are iterator categories, and why do algorithms care? |
| 131 | + :ref:`→ cpp/cpp_iterator: Iterator Categories <notes/cpp/cpp_iterator:Iterator Categories>` |
| 132 | +* Walk through ``std::sort``, ``std::stable_sort``, ``std::partial_sort``, and |
| 133 | + ``std::nth_element`` — when would you pick each? |
| 134 | + :ref:`→ cpp/cpp_algorithm: Sorting <notes/cpp/cpp_algorithm:Sorting>` |
| 135 | +* Difference between ``std::find`` and ``std::binary_search``? |
| 136 | + :ref:`→ cpp/cpp_algorithm: Searching <notes/cpp/cpp_algorithm:Searching>` |
| 137 | +* What are C++20 ranges and views? Are views lazy? |
| 138 | + :ref:`→ cpp/cpp_iterator: C++20 Ranges Overview <notes/cpp/cpp_iterator:C++20 Ranges Overview>` |
| 139 | + |
| 140 | +Strings |
| 141 | +======= |
| 142 | + |
| 143 | +* ``std::string`` vs. ``std::string_view`` — when is ``string_view`` dangerous? |
| 144 | + :ref:`→ cpp/cpp_string: std::string_view <notes/cpp/cpp_string:std\:\:string_view>` |
| 145 | +* Why is naïve ``+=`` concatenation in a loop a performance trap? |
| 146 | + :ref:`→ cpp/cpp_string: String Concatenation Performance <notes/cpp/cpp_string:String Concatenation Performance>` |
| 147 | + |
| 148 | +Modern C++ Features |
| 149 | +=================== |
| 150 | + |
| 151 | +* What does each lambda capture mode mean: ``[]``, ``[=]``, ``[&]``, |
| 152 | + ``[this]``, init captures? |
| 153 | + :ref:`→ cpp/cpp_lambda: Lambda Syntax Overview <notes/cpp/cpp_lambda:Lambda Syntax Overview>` · |
| 154 | + :ref:`Init Capture <notes/cpp/cpp_lambda:Init Capture (C++14)>` |
| 155 | +* Why is a captureless lambda convertible to a function pointer? |
| 156 | + :ref:`→ cpp/cpp_lambda: Captureless Lambdas and Function Pointers <notes/cpp/cpp_lambda:Captureless Lambdas and Function Pointers>` |
| 157 | +* What is a generic / template lambda? |
| 158 | + :ref:`→ cpp/cpp_lambda: Generic Lambdas (C++14) <notes/cpp/cpp_lambda:Generic Lambdas (C++14)>` · |
| 159 | + :ref:`Template Lambdas <notes/cpp/cpp_lambda:Template Lambdas (C++20)>` |
| 160 | +* What are C++20 coroutines, and what does ``co_await`` actually do? |
| 161 | + :ref:`→ cpp/cpp_coroutine: Coroutine Basics <notes/cpp/cpp_coroutine:Coroutine Basics>` · |
| 162 | + :ref:`Promise Type <notes/cpp/cpp_coroutine:Promise Type>` · |
| 163 | + :ref:`Awaiter <notes/cpp/cpp_coroutine:Awaiter>` |
| 164 | +* Why would you write a generator with coroutines instead of a class? |
| 165 | + :ref:`→ cpp/cpp_coroutine: Generator <notes/cpp/cpp_coroutine:Generator>` |
| 166 | +* What are C++20 modules, and what problem do they solve? |
| 167 | + :ref:`→ cpp/cpp_modules: Module Basics <notes/cpp/cpp_modules:Module Basics>` |
| 168 | +* ``auto`` vs. ``decltype`` vs. ``decltype(auto)``? |
| 169 | + :ref:`→ cpp/cpp_basic: Auto Type Deduction <notes/cpp/cpp_basic:Auto Type Deduction>` · |
| 170 | + :ref:`decltype(auto) <notes/cpp/cpp_basic:decltype(auto) Type Deduction>` |
| 171 | +* What does uniform / brace initialization change about narrowing conversions? |
| 172 | + :ref:`→ cpp/cpp_basic: Uniform Initialization <notes/cpp/cpp_basic:Uniform Initialization (Brace Initialization)>` |
| 173 | + |
| 174 | +C Language Essentials |
| 175 | +===================== |
| 176 | + |
| 177 | +* When does an array *not* decay to a pointer, and what are the signed/unsigned |
| 178 | + gotchas around pointer arithmetic? |
| 179 | + :ref:`→ cpp/cpp_basic: Pointer Arithmetic and Negative Indices <notes/cpp/cpp_basic:Pointer Arithmetic and Negative Indices>` |
| 180 | +* What does ``restrict`` promise to the compiler? |
| 181 | + :ref:`→ c/c_basic: Restrict Qualifier <notes/c/c_basic:Restrict Qualifier>` |
| 182 | +* Designated initializers and compound literals — how are they different? |
| 183 | + :ref:`→ c/c_basic: Designated Initializers <notes/c/c_basic:Designated Initializers>` · |
| 184 | + :ref:`Compound Literals <notes/c/c_basic:Compound Literals>` |
| 185 | +* What are flexible array members? |
| 186 | + :ref:`→ c/c_basic: Flexible Array Members <notes/c/c_basic:Flexible Array Members>` |
| 187 | +* Why is ``_Generic`` useful, and how is it different from C++ overloading? |
| 188 | + :ref:`→ c/c_basic: _Generic Type Selection (C11) <notes/c/c_basic:\`\`_Generic\`\` Type Selection (C11)>` |
| 189 | +* What new things did C23 bring (``nullptr``, ``constexpr``, ``typeof``, |
| 190 | + attributes, bit-precise ints)? |
| 191 | + :ref:`→ c/c_basic: typeof and auto (C23) <notes/c/c_basic:\`\`typeof\`\` and \`\`auto\`\` (C23)>` · |
| 192 | + :ref:`nullptr <notes/c/c_basic:\`\`nullptr\`\` (C23)>` · |
| 193 | + :ref:`constexpr <notes/c/c_basic:\`\`constexpr\`\` (C23)>` |
| 194 | +* Why are macros dangerous, and what safer alternatives exist? |
| 195 | + :ref:`→ c/c_macro: Variadic Macros <notes/c/c_macro:Variadic Macros>` |
| 196 | + |
| 197 | +Concurrency & OS |
| 198 | +================ |
| 199 | + |
| 200 | +* How do you create threads in modern C++? |
| 201 | + :ref:`→ os/os_thread: Creating Threads <notes/os/os_thread:Creating Threads>` |
| 202 | +* Mutex, condition variable, and read-write lock — when is each appropriate? |
| 203 | + :ref:`→ os/os_thread: Mutex Synchronization <notes/os/os_thread:Mutex Synchronization>` · |
| 204 | + :ref:`Condition Variables <notes/os/os_thread:Condition Variables>` · |
| 205 | + :ref:`Read-Write Locks <notes/os/os_thread:Read-Write Locks>` |
| 206 | +* ``std::future`` / ``std::promise`` / ``std::async`` — what are the launch |
| 207 | + policies? |
| 208 | + :ref:`→ os/os_thread: C++ Futures and Async <notes/os/os_thread:C++ Futures and Async>` · |
| 209 | + :ref:`Launch Policies <notes/os/os_thread:Launch Policies>` |
| 210 | +* What is thread-local storage? |
| 211 | + :ref:`→ os/os_thread: Thread-Local Storage <notes/os/os_thread:Thread-Local Storage>` |
| 212 | +* Process vs. thread — what is shared and what is not? |
| 213 | + :ref:`→ os/os_process: Fork and Wait <notes/os/os_process:Fork and Wait>` · |
| 214 | + :ref:`Fork and Exec <notes/os/os_process:Fork and Exec>` |
| 215 | + |
| 216 | +Debugging & Tools |
| 217 | +================= |
| 218 | + |
| 219 | +* How do you find memory leaks and invalid accesses with Valgrind? |
| 220 | + :ref:`→ debug/valgrind: Memcheck: Memory Errors <notes/debug/valgrind:Memcheck\: Memory Errors>` · |
| 221 | + :ref:`Leak Types <notes/debug/valgrind:Leak Types>` |
| 222 | +* What do AddressSanitizer, ThreadSanitizer, and UBSan each catch? |
| 223 | + :ref:`→ debug/sanitizers: AddressSanitizer (ASan) <notes/debug/sanitizers:AddressSanitizer (ASan)>` · |
| 224 | + :ref:`ThreadSanitizer <notes/debug/sanitizers:ThreadSanitizer (TSan)>` · |
| 225 | + :ref:`UndefinedBehaviorSanitizer <notes/debug/sanitizers:UndefinedBehaviorSanitizer (UBSan)>` |
| 226 | +* How do you inspect a core dump with gdb after a segfault? |
| 227 | + :ref:`→ debug/gdb: Debugging Crashes <notes/debug/gdb:Debugging Crashes>` |
| 228 | +* What are breakpoints vs. watchpoints in gdb? |
| 229 | + :ref:`→ debug/gdb: Breakpoints <notes/debug/gdb:Breakpoints>` · |
| 230 | + :ref:`Watchpoints <notes/debug/gdb:Watchpoints>` |
| 231 | +* Minimal modern CMake project — what are the must-have commands? |
| 232 | + :ref:`→ cpp/cpp_cmake: Minimal Project <notes/cpp/cpp_cmake:Minimal Project>` · |
| 233 | + :ref:`Modern C++ Standard Setting <notes/cpp/cpp_cmake:Modern C++ Standard Setting>` |
| 234 | + |
| 235 | +See Also |
| 236 | +======== |
| 237 | + |
| 238 | +If a question above is not covered, the top-level indices are the best next |
| 239 | +stop: |
| 240 | + |
| 241 | +* :doc:`../c/index` — C language reference |
| 242 | +* :doc:`../cpp/index` — Modern C++ reference |
| 243 | +* :doc:`../os/index` — OS and systems programming |
| 244 | +* :doc:`../debug/index` — Debugging and profiling tools |
0 commit comments