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.
Question Bank
Easy
JavaScript
3 questions
hash-table
data-structures
quiz
fundamentals
484 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.const seen = new Set();
const counts = new Map();
for (const ch of 'banana') {
seen.add(ch);
counts.set(ch, (counts.get(ch) ?? 0) + 1);
}
console.log(seen.size);
console.log(counts.get('a'));
console.log(counts.has('z'));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.function firstUnique(arr) {
// TODO: O(n) with a hash structure
}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?
