Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: A hash table keyed by short strings uses Java's String.hashCode. Why do some keys collide?
Problem: A hash table keyed by short strings uses Java's String.hashCode. Why do some keys collide?
// String.hashCode: h = 31*h + c over the characters, in 32-bit int arithmetic
System.out.println("Aa".hashCode() + " " + "BB".hashCode() + " " + ("Aa".hashCode() == "BB".hashCode()));What is printed?- A. 2112 2113 false
- B. 2080 2112 false
- C. 2112 2112 true
- D. 4290 4290 true
Correct answer: C. 2112 2112 true
'A' = 65, 'a' = 97: 31·65 + 97 = 2112; 'B' = 66: 31·66 + 66 = 2112. Different strings, same hash – so hash maps must compare keys with equals() after matching hashes, and adversarial inputs built from such blocks can force many collisions (Java 8+ turns long buckets into trees, O(log n)).
More Coding Interview Problems questions
- Problem: Binary search on large indices in C++ (int is 32-bit). cpp int lo = 2000000000, hi = 2100000000; int bad = (lo + hi) / 2; //…
- Problem: Find the celebrity (known by all, knows nobody) with O(n) calls to knows(a, b). python M = [[0, 1, 1, 0], [0, 0, 1, 0], [0, 0, 0…
- Problem: Delete exactly one element from a binary array and return the longest run of 1s that can remain. python def afterdelete(a): l =…
- Problem: Find the minimum number of railway platforms with a sweep line over arrival and departure events (a train arriving when another…
- Problem: Find how many numbers in 1..n have all distinct digits — the digit-DP-free shortcut for n = 10ᵏ uses counting. python def…
- Problem: Find the minimum window length in which every one of k distinct product IDs appears at least once (k = number of distinct values…