Design high-throughput hashing for kernels
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates understanding of cache-efficient, high-throughput hash lookup design and related competencies such as hashing strategies, probe behavior, memory layout, vectorization, bitwise optimizations, prefetching, alignment, and microarchitectural performance trade-offs.
Constraints
- 0 <= len(pairs), len(queries) <= 2 * 10^5
- Keys are 32-bit signed integers
- Values are integers in the range [0, 10^9]
- Capacity is the smallest power of two >= ceil(10 * len(pairs) / 7), with minimum 1
Examples
Input: ([(1, 10), (9, 20), (17, 30)], [1, 17, 2])
Expected Output: (8, [10, 30, -1], 6, 7)
Explanation: Capacity is 8. Keys 1, 9, and 17 all start at slot 1 and are placed at slots 1, 2, and 3. Insert probes = 1 + 2 + 3 = 6. Lookups use the same probe sequence, giving results [10, 30, -1] and lookup probes 1 + 3 + 3 = 7.
Input: ([(5, 1), (13, 2), (5, 7)], [5, 13, 21])
Expected Output: (8, [7, 2, -1], 4, 6)
Explanation: Capacity is 8. Key 5 is inserted, 13 collides and moves to the next slot, then key 5 appears again and overwrites its existing value. Insert probes = 1 + 2 + 1 = 4. Query results are [7, 2, -1] with lookup probes 1 + 2 + 3 = 6.
Hints
- Because the capacity is a power of two, you can wrap around with & (capacity - 1) instead of using modulo.
- With open addressing, a lookup can stop as soon as it reaches an empty slot, because the key could not appear later in that probe sequence.