KatInterview

Arrays vs Hash Maps: Trade-offs for Sparse Active-Record Stores

Data Structure DesignModel answer
HardModel answerData Structure Designhash-maparraymemory

Last updated

Compare arrays and hash maps across time complexity, memory, and cache behavior, then choose the right structure for a sparse integer-keyed active set.

Background

You are designing a core data store for a real-time order-tracking system. At any moment the system maintains metadata for a subset of orders drawn from a large integer ID space. You must choose the right underlying data structure before writing a single line of business logic, because that choice affects latency, memory footprint, and correctness under load.

Your Task

Answer the four questions below. Support every claim with concrete reasoning; numbers and examples are expected.

1. Time Complexity

Build a comparison table for search, insert, and delete across:

  • an unsorted dynamic array
  • a sorted array
  • a hash map (average case and worst case)

Call out two subtleties that matter in production: when is the hash map worst case actually reachable, and what does "amortized O(1)" hide?

2. Memory Overhead

A hash map consistently uses more memory per element than a plain array. Explain the three structural reasons for this, and give rough byte counts to make the gap concrete.

3. Ordering and Data Locality

Describe what happens at the CPU cache level when you iterate sequentially through an array versus a hash map. How does memory layout influence which structure wins in a latency-sensitive loop, even when big-O complexity is identical?

4. Design Decision

Your system tracks orders whose IDs are unique integers drawn from the range 0 to 1,000,000. At any given moment exactly 1,000 of those IDs are active. Each active order carries a small metadata record.

  • Evaluate a direct-address array sized to the full ID range.
  • Evaluate a hash map keyed by order ID.
  • Describe a hybrid approach that gives O(1) lookup and cache-friendly iteration over only the live records. Implement it in Python.

Constraints and Expected Output

  • Inputs: a stream of (order_id: int, price: float) pairs; IDs may appear multiple times (update semantics) or be cancelled (delete).
  • Outputs: fast point lookup by ID, fast full iteration over active orders only.
  • Assume IDs could later grow beyond 32 bits; your design should remain valid.
  • For the Python implementation: include upsert(order_id, price), get(order_id) -> Optional[float], remove(order_id) -> bool, and __iter__.

Hints

Hint 1

For the complexity table, think carefully about what triggers a hash map's O(n) worst case and whether a good hash function alone is sufficient protection.

Hint 2

Memory overhead in a hash map comes from three independent sources: what is stored per entry, the mandatory fraction of empty slots, and any per-node allocation cost depending on the collision strategy.

Hint 3

The hybrid structure keeps two things in sync: a dense packed array for fast iteration, and a mapping from ID to position in that array for fast lookup. Think about what swap-remove does to that mapping when you delete.