The Design Phone Directory interview question asks you to manage a pool of available phone numbers from 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).
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 lookup and retrieval of an available resource.
This problem uses a combination of a Queue (or Stack) and a Hash Set (or Boolean Array).
get to run in .boolean[]) tracks which numbers are currently in use. This allows check and release to run in .Directory for 3 numbers (0, 1, 2).
{0} is used. Queue has [1, 2].false (0 is in the used set).{0, 1} is used. Queue has [2].[2, 0].true.get call, leading to time.Set<Integer> when a BitSet or boolean[] would be much more memory-efficient for a fixed range of numbers.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.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Circular Deque | Medium | Solve | |
| Design Circular Queue | Medium | Solve | |
| Design Snake Game | Medium | Solve | |
| Design Front Middle Back Queue | Medium | Solve | |
| First Unique Number | Medium | Solve |