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?"