Magicsheet logo

Design Phone Directory

Medium
25%
Updated 8/1/2025

Design Phone Directory

What is this problem about?

The Design Phone Directory interview question asks you to manage a pool of available phone numbers from 00 to maxNumbers - 1. You need to support three operations: get (returns an unassigned number and marks it as used), check (checks if a number is available), and release (makes a previously used number available again).

Why is this asked in interviews?

Google asks this to evaluate your understanding of set-based operations and your ability to choose the right data structure for specific constraints. It’s a classic resource allocation problem. It tests whether you can optimize for both O(1)O(1) lookup and O(1)O(1) retrieval of an available resource.

Algorithmic pattern used

This problem uses a combination of a Queue (or Stack) and a Hash Set (or Boolean Array).

  1. A Queue stores all currently available numbers. This allows get to run in O(1)O(1).
  2. A Hash Set (or boolean[]) tracks which numbers are currently in use. This allows check and release to run in O(1)O(1).

Example explanation

Directory for 3 numbers (0, 1, 2).

  1. get(): Returns 0. Set {0} is used. Queue has [1, 2].
  2. check(0): Returns false (0 is in the used set).
  3. get(): Returns 1. Set {0, 1} is used. Queue has [2].
  4. release(0): Removes 0 from the used set and adds it back to the Queue. Queue: [2, 0].
  5. check(0): Returns true.

Common mistakes candidates make

  • Inefficient Search: Iterating through an array to find the first "false" value for every get call, leading to O(N)O(N) time.
  • Double Release: Not checking if a number is actually in use before adding it back to the available pool, which can lead to duplicate numbers in the directory.
  • Memory Usage: Using a full Set<Integer> when a BitSet or boolean[] would be much more memory-efficient for a fixed range of numbers.

Interview preparation tip

When managing a pool of unique items, always consider using two structures: one for the "available" items (for fast retrieval) and one for the "used" items (for fast membership checks). This "Dual Structure" approach is a staple in system design.

Similar Questions