Basic Hash Map Questions
Seven warm-up exercises to build hash-map muscle memory before the interview problems. Each one is short, but each one drills a different reflex: when to use a set vs a map, when to use a counter, how a tiny hash map is implemented under the hood, and the canonical “build a signature” trick.
1. Compute a simple hash by hand
Given the function h(s) = (sum of ASCII codes) mod 7, what bucket does each of these strings land in?
"cat", "dog", "act", "tac", "god"2. Pick the right structure: set vs map vs counter
For each task, which structure fits best?
a) “Have I seen this number before?” b) “How many times does each word appear?” c) “Which words are unique to this file but not in another?” d) “Given a username, look up that user’s score.”
3. First duplicate in a list, fast
Given [2, 7, 1, 8, 7, 2, 9], find the first number that appears more than once (the first one whose duplicate we encounter when walking left-to-right).
4. Anagram check in two lines
Decide whether "listen" and "silent" are anagrams. Then "rat" and "car".
5. The “signature → bucket” pattern
You’re given a list of strings. Group all the anagrams together.
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["eat","tea","ate"], ["tan","nat"], ["bat"]]What’s the right hash-map key?
6. Build your own tiny hash map
Implement a hash map with chained collisions, supporting put(key, value), get(key), and remove(key). Use 8 buckets and hash(key) % 8 for indexing.
7. Two-sum check by hand
Given nums = [3, 5, 8, 11, 4] and target = 13, which two numbers add to target? Trace the hash-map approach.
Quick check
Ready for the interview classics? Head to Practice Questions.