Back to C++ for DSA

Drill it before
they ask it.

The paid companion to the free book. For the week before a C++ interview.

Reading an answer feels like learning. Saying it out loud, under time, is learning.

Every question here is one an interviewer can ask in under a minute and grade in ten seconds — the unsigned size(), the subscript that inserts, what a rehash invalidates, which pointer owns the tree. Each one comes with the trap it tests, the full answer with code, and the single line that earns the mark, so you can grade yourself honestly.

Then eight mock sets of ten questions in twenty-five minutes, a seven-day plan, and a recall sheet for the morning itself.

Three questions, as they appear.

This is the whole format — nothing is held back behind the price except the other 117.

Q4.1 — The subscript that inserts

This counts words. What does it leave behind, and why does the second line matter?

++counts[word];
if (counts["ghost"] == 0) { /* ... */ }

Tests the best-known trap in the standard library.

Answer operator[] default-constructs a missing value and inserts it, so the counting line is correct and idiomatic. The second line, though, inserts "ghost" with the value 0 just by asking about it, growing the map and changing later iteration. Read with find or contains.

Point Say “it inserts”, that the counting use is fine, and give the read-only alternative.

Q2.7 — The reference that dies

int& first = v[0];
v.push_back(42);
first = 7;          // ?

Answer If the push_back reallocated, first refers to freed memory and the assignment is undefined behaviour — a use-after-free that usually does not crash, which is worse. Take the index instead, or reserve before you take the reference.

Point Name the use-after-free and give the index or the reserve.

Q8.12 — Will it pass?

The limits say n ≤ 100,000 and the time limit is 2 seconds. Is an O(n²) solution viable? What about n ≤ 2,000?

Answer Take roughly 10⁸ to 10⁹ simple operations a second for compiled C++. At n = 100,000, n² is 10¹⁰ — one to two orders of magnitude too slow, so the problem wants O(n log n) or O(n). At n = 2,000, n² is 4 × 10⁶, which is comfortable. Do this out loud before writing: the limits are the interviewer telling you the intended complexity.

Point The rule of thumb, both multiplications, and reading the limits as a hint.

The eight blocks.

Fifteen questions each, with a target score, so you know which block to spend a day on.

  1. Types, sizes and overflow

    Unsigned sizes, the midpoint that overflows, signed UB, promotion, negative modulo.

  2. Vectors, arrays and iterator invalidation

    What push_back invalidates, erasing in a loop, swap-and-pop, grids and reuse.

  3. Strings, bytes and characters

    substr cost, dangling views, UTF-8 against size(), the isalpha cast, splitting without copies.

  4. Maps, sets and hashing

    The subscript that inserts, rehash invalidation, hashing a pair, comparators that crash.

  5. Ownership, moves and lifetime

    unique_ptr against shared_ptr, what std::move does, rule of zero, the lambda that dangles.

  6. Recursion, trees and linked lists

    The BST check everyone gets wrong, Floyd, dummy heads, level order, flattening in place.

  7. Sorting, searching and the standard algorithms

    Stability, lower_bound, nth_element, heap direction, binary search on the answer.

  8. Complexity, cost and judgment

    Amortised against average, will it pass, sliding windows, "can you do better?"

Checked, not trusted.

The same bar as the books: if the pack says code compiles, a machine said so first.

  1. Every snippet compiles.

    All 90 code blocks are compiled with clang++ -std=c++20 -Wall -Wextra -pedantic. The two that are meant to be rejected are checked for being rejected.

  2. Every algorithm runs.

    The BST check, Floyd, the merge, the flatten, the rotated search and the rest are built and run against 51 assertions, including the empty and even-length cases.

  3. Every number is measured.

    The quoted values — the wrapped size(), the signed char, the six bytes of “héllo” — come from running the program, not from memory.