Learning/Arrays Hashing

Arrays & Hashing

9 questions. This is the foundation section: almost every later pattern reuses the moves learned here.

The one idea

Spend O(n) memory to eliminate a nested loop.

Nearly every question below has the same shape. The brute force contains an inner loop that is searching for something. Searching is exactly what a hash map makes instant — so you replace the inner loop with a lookup into something you built as you went.

The design work is never "use a HashMap". It's choosing what the key should be. That's what these nine questions drill.

Prerequisites from Part 1

Read these before starting:

How to work this section

For each question:

  1. Read §1 and §2 only, then close the file and attempt the problem cold with a timer (20 minutes for Easy, 35 for Medium).
  2. After your attempt, read §3 and §4 and diff against your own reasoning. The gap is your study material — not the parts you got right.
  3. Rehearse §6 aloud. Then use the counter-questions in §3 as a quiz — for each approach, cover the answers and try to defend it yourself.
  4. Read §7 last: those are the modified-constraint variants.

What to carry forward

By the end of this section you should be able to answer, instantly: