Reddit Question · Apr 2026

Hash maps are the answer to more problems than you think

82 upvotes 17 replies

Question Details

Took me a while to realize this but a huge chunk of medium problems basically have the same answer. Store something in a hash map and look it up in O(1) instead of scanning the array again. The hard p

Full Details

Took me a while to realize this but a huge chunk of medium problems basically have the same answer. Store something in a hash map and look it up in O(1) instead of scanning the array again. The hard part isn't knowing what a hash map is. Everyone knows what a hash map is. The hard part is recognizing fast enough that you need one before you go down a nested loop path that'll cost you the interview. The signal I look for now is this. If I'm about to write a second loop to find something I already saw in the first loop, that's the moment to stop and ask if a hash map would just solve this. Nine times out of ten it does. Two sum is the obvious example but once you see it there you start seeing it everywhere. Subarray sum equals k. Longest substring without repeating characters. Group anagrams. All of them are just "remember what you've seen so you don't have to look again." Also worth knowing is what to store. Sometimes it's the value, sometimes the index, sometimes the frequency. Getting that wrong is where people mess up even when they correctly identify that a hash map is the right move. I started tagging every problem where a hash map was part of the solution. The list got long fast. Made it obvious this was worth drilling specifically. What data structure do you think is the most underrated in DSA prep?

Free preview. Unlock all questions →

Topics

Arrays Strings Hash Table