Course outline · 0% complete

0/27 lessons0%

Course overview →

Interview Idiom Drills

lesson 10-2 · ~14 min · 27/27

Your C++ interview toolkit

Everything below is a one-liner combination of things you built in this course:

TaskIdiomFrom lesson
count anythingstd::unordered_map<T,int> f; f[x]++;9-1
have I seen x?std::unordered_set<T> s; s.count(x)9-2
sortstd::sort(v.begin(), v.end());9-2
unique + sortedput everything in a std::set9-2
grow a resultstd::vector<T> out; out.push_back(...)8-1
walk a stringfor (char c : s)8-2, 8-3
big sumslong long10-1

The drill below is the most famous interview problem of all, Two Sum: find two numbers in a list that add to a target. The hash-map insight: for each value x, ask "have I already seen target − x?" One pass, O(1) average per lookup, O(n) total (lesson 8-4) — against the O(n²) of checking every pair.

Code exercise · cpp

Run this complete Two Sum. Input is n, then n numbers, then the target. It prints the positions (0-based) of the pair. Study the seen-map pattern until it feels obvious.

Problem

The Two Sum program above visits each of the n elements once and does one average-O(1) map lookup and one insert per element. In big-O notation, what is its overall time complexity?

Quiz

In the Two Sum code, why is the answer pair guaranteed to be two DIFFERENT positions?

Where to go from here

You now hold the working core of C++: the compile model (unit 1), types and control flow (units 2-3), functions, references, and recursion (unit 4), pointers and memory (units 5-6), classes (unit 7), and the STL with its big-O cost model (units 8-9), capped by the contest habits of unit 10.

Next steps:

  1. Practice on the DSA platform. Pick easy problems, choose C++ as your language, and reuse this unit's template. Fluency comes from repetition, not rereading.
  2. Deepen the memory story when you are ready: copy vs move semantics, the rule of three/five, shared_ptr, and templates are the natural next layer.
  3. Read real C++. Standard library documentation and well-written open source will keep stretching you.

The language rewards exactly what interviews reward: knowing what your code costs, down to the byte and the pass.

Code exercise · cpp

Final drill, on your own. Read one word and print its first NON-repeating character, or `none` if every character repeats. Plan: count all characters in a map (lesson 9-1), then walk the word a second time and print the first char whose count is 1. Input is `swiss`.