Hash Maps & Sets
The hash map is the most useful data structure in programming: look anything up by key in O(1). Python's dict, JavaScript's object and Map, Java's HashMap — same idea everywhere, and the #1 tool for making slow code fast.
A hash map stores key–value pairs and finds any value by key in constant time. The trick: a hash function converts the key into a number, and that number decides which slot of an internal array holds the value. Lookup doesn't search — it recomputes the hash and jumps straight to the slot, the same way array indexing jumps. A set is a hash map that only keeps keys; 'have I seen this before?' in O(1).
Why does it work? The hash function spreads keys evenly across slots. Occasionally two keys land in the same slot — a collision — and the map handles it by chaining a small list in that slot or probing nearby ones. With a good hash function collisions stay rare, so lookups average O(1). This is also why keys must be immutable (hashable): if a key could change after insertion, its hash would change, and the map would look in the wrong slot forever.
Rule of thumb for interviews and real code alike: if your solution is O(n²) because of a lookup inside a loop, a hash map or set almost always makes it O(n). It trades memory for speed — nearly always a great trade.