Loading…
Hash map questions test the core interview trade: spend memory to remove a nested loop, turning a quadratic scan into a single linear pass.
Hash maps are the most reliable optimisation in the coding interview, because so many brute-force solutions are quadratic purely from re-scanning data that could have been remembered. The recognisable trigger is a nested loop asking "have I seen this before?" — that question is a hash lookup, and replacing it collapses the complexity in one move.
Interviewers listen for you to name the space cost when you reach for a map, not to skip past it. Strong answers also handle the details: what the key actually is when the natural key is a composite, why average-case constant lookup is not worst-case constant, and when an ordered structure is required because the question needs sorted traversal. Being asked to solve it again in constant space is a common follow-up, so know which of your hash-map solutions have a two-pointer equivalent.
1 system design · 10 coding · 5/4/2 easy/medium/hard
Whenever the brute force contains a nested loop whose inner pass answers a membership or counting question. That inner loop is almost always replaceable by a hash lookup, taking the solution from quadratic to linear.
To solve it again without the extra space. Many hash-map solutions have a sorted-input or two-pointer counterpart that trades time for memory, and knowing that alternative is what turns a good answer into a strong one.