← Questions

Resize a hash map without long pauses

0
AppleCodingLast asked Oct 2025

A coding interview question asked in Apple's technical phone screen, last asked Oct 2025. It tests hash tables, data structures and latency.

Explain how a hash map works inside: how a key becomes a bucket, how collisions are handled, and when the table grows. Then the follow-up: growing usually means allocating a bigger array and moving every entry at once, which causes a latency spike on a large map. Design a way to cap or smooth that pause while keeping expected O(1) lookups and inserts.

Part 1: how it works

Cover hashing and reducing the hash to a bucket index (modulo or masking with a power-of-two size), chaining versus open addressing, the load factor that triggers a resize, and why resizing doubles the size.

Part 2: resizing without a long pause

Unless you agree on a different interface with the interviewer, assume the map supports get(key), put(key, value) and remove(key), and that any single call should do a bounded amount of work.

Example

capacity 8, load factor limit 0.75
put #7 makes 7/8 > 0.75  ->  allocate a table of 16 and start migrating
the next calls each also move 2 old buckets to the new table
get(key) during the migration checks the new table, then the old one
after 4 more calls the old table is empty and is freed

What a strong answer covers

  • Why doubling gives amortized O(1) inserts, and why amortized is not good enough when every request has a latency budget.
  • Incremental rehashing: keep the old and new tables, move a few buckets on each operation (or in a background task), track a migration cursor.
  • Where lookups, inserts and deletes go during the migration, and why inserts go only to the new table.
  • Memory cost: both tables exist at once.
  • Alternatives: linear hashing or extendible hashing that split one bucket at a time, consistent hashing for sharded caches, and pre-sizing when the final size is known.

Follow-ups

  • How do you make sure the migration finishes if traffic stops?
  • How would this work in a concurrent map?
  • When should the map shrink, and how do you avoid growing and shrinking back and forth?

Where it's asked

  • Apple · Technical phone screen · Last asked Oct 2025

What it tests

hash tables · data structures · latency

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding