Question Bank
Hash Table Basics Quiz
Difficulty: Easy
Short prompts on hash table lookup cost, collision handling, and when to reach for a Set vs a Map. Builds the mental model before harder hashing problems.
Hash Table Basics Quiz
Short prompts on hash table lookup cost, collision handling, and when to reach for a Set vs a Map. Builds the mental model before harder hashing problems.
483 views
13
Trace this snippet and explain what each line prints. Which of the two structures is right for counting frequencies, and which for membership testing?
Examples
Example 1:
Input: iterate 'banana' building seen (Set) and counts (Map); then print seen.size, counts.get('a'), counts.has('z')
Output: 3, then 3, then false
Explanation: Sets dedupe so seen ends as {b, a, n} (size 3). Map stores per-key totals, so counts.get('a') is 3. 'z' was never inserted, so counts.has('z') is false. Use Set for membership, Map for per-key payload.Implement firstUnique(arr) returning the first element that appears exactly once in arr, or null if none exists. Aim for O(n) time using a hash structure.
Examples
Example 1:
Input: arr = ['a', 'b', 'c', 'a', 'b']
Output: 'c'
Explanation: Counts are {a: 2, b: 2, c: 1}. Walking the original array, 'a' and 'b' have count 2 and are skipped; 'c' has count 1 and is returned.Example 2:
Input: arr = ['x', 'x', 'y', 'y']
Output: null
Explanation: Every element repeats, so no key has count 1 and the function returns null.Two distinct keys hash to the same bucket index. What happens in a hash table that uses separate chaining versus one that uses open addressing (linear probing)? Which is more cache-friendly?
