Arrays, strings & hashing
Hashmap/hashset idioms, frequency counting, collision handling, load factor and resize. The workhorse of every screening round.
The mental model
A hash table is a coat check. You hand over a coat (a value), the attendant gives you a numbered ticket derived from something about the coat (the hash of the key), and hangs it on the peg with that number. To retrieve it, you present the key, the attendant recomputes the number, walks straight to that peg, and hands the coat back — no searching the whole cloakroom. That single idea, turn a key into an address, is why lookups are O(1) instead of O(n).
The array underneath is even simpler and is the reason the coat check is fast: an array is a **row of numbered mailboxes in one contiguous strip of memory**. Because element i lives at base_address + i * element_size, the computer reaches any mailbox with one multiplication and one memory fetch — that is what O(1) random access means physically. A hash table is an array where the index is computed from the key instead of given directly.
Why do these exist as the workhorse of interviews? Because an enormous fraction of problems reduce to one question: "have I seen this before, and where?" Counting frequencies, detecting duplicates, grouping anagrams, finding a complement that sums to a target, deduplicating — all of them are "look something up by content, fast". The brute force is a nested loop (O(n²)); the hash table turns the inner loop into an O(1) lookup, collapsing it to O(n). Learning to hear "have I seen this?" inside a problem statement and reach for a hash map is the single most reused move in screening rounds.
The catch the mental model must include: the ticket numbers can collide. Two different coats can hash to the same peg. Everything hard about hash tables — chaining, load factor, resizing, why worst case is O(n) — is about handling collisions gracefully. And strings are where hashing gets subtle, because a string key must be turned into a number by combining all its characters, and doing that badly (or maliciously) is where both bugs and attacks live.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision