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.
Easy
Contains Duplicate
LC 217
13 min
Easy
Valid Anagram
LC 242
12 min
Easy
Two Sum
LC 1
13 min
Medium
Group Anagrams
LC 49
14 min
Medium
Top K Frequent Elements
LC 347
16 min
Medium
Encode and Decode Strings
LC 271
14 min
Medium
Product of Array Except Self
LC 238
15 min
Medium
Valid Sudoku
LC 36
17 min
Medium
Longest Consecutive Sequence
LC 128
16 min
Prerequisites from Part 1
Read these before starting:
01
Complexity & Problem Triage
you need to read constraints and name a target complexity02
Java Collections Toolkit
§1
HashMap, §4 HashSet — especially the compute family (merge, computeIfAbsent, getOrDefault)05
equals, hashCode & Keys
questions 4 and 8 fail outright if you use an int[] as a map key06
Arrays & Hashing
the pattern summary this section instantiatesHow to work this section
For each question:
- Read §1 and §2 only, then close the file and attempt the problem cold with a timer (20 minutes for Easy, 35 for Medium).
- 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.
- 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.
- 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:
- "What would I need to have already recorded, at this element, to answer in one step?" — the key-design question.
- Why
HashMap.getisO(1), and when it isn't. - When a fixed-size
int[26]beats aHashMap(and why it'sO(1)space, notO(k)). - The three ways to get "top k" and their trade-offs: sort, heap, bucket.
- Why an
int[]cannot be aHashMapkey in Java, and the three standard fixes.