Design Phone Directory - Problem

Design Phone Directory

You're tasked with designing a phone directory system that manages available phone number slots efficiently. The directory starts with a fixed number of empty slots that can store phone numbers.

Your system needs to support three key operations:

  • get() - Assign an available phone number slot to a user. Returns the number, or -1 if no slots are available
  • check(number) - Verify if a specific slot number is currently available
  • release(number) - Free up a previously assigned slot so it can be reused

Example: If you initialize the directory with 3 slots, numbers 0, 1, and 2 are initially available. After calling get(), one number becomes assigned. You can later release() that number to make it available again.

This problem tests your ability to design efficient data structures for resource management - a common requirement in system design interviews.

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["PhoneDirectory", "get", "check", "get", "check", "release", "check"] [[3], [], [2], [], [2], [2], [2]]
โ€บ Output: [null, 0, true, 1, false, null, true]
๐Ÿ’ก Note: Initialize with 3 slots (0,1,2). get() returns 0 and marks it unavailable. check(2) returns true as it's still available. get() returns 1. check(2) returns false since 2 is assigned. release(2) frees slot 2. check(2) returns true again.
example_2.py โ€” All Slots Exhausted
$ Input: ["PhoneDirectory", "get", "get", "get", "get"] [[2], [], [], [], []]
โ€บ Output: [null, 0, 1, -1, -1]
๐Ÿ’ก Note: Initialize with 2 slots. First get() returns 0, second returns 1. Third and fourth get() return -1 since all slots are exhausted.
example_3.py โ€” Release and Reuse
$ Input: ["PhoneDirectory", "get", "get", "release", "get", "release", "get"] [[1], [], [], [0], [], [0], []]
โ€บ Output: [null, 0, -1, null, 0, null, -1]
๐Ÿ’ก Note: Initialize with 1 slot. get() returns 0. Second get() returns -1. release(0) frees slot 0. get() can now return 0 again. After another release(0) and get(), we're back to no available slots.

Visualization

Tap to expand
Phone Directory SystemAvailable Pool2345Assigned Numbers01get()release()Operations:โ€ข get(): O(1) - Remove from available poolโ€ข check(): O(1) - Lookup in availability setโ€ข release(): O(1) - Return to available pool๐Ÿ’ก Key InsightUse Queue for fast assignment + Set for instant availability checking
Understanding the Visualization
1
Initialize
All phone numbers 0 to maxNumbers-1 are available
2
Assignment
get() removes and returns the next available number
3
Availability Check
check() verifies if a specific number is available
4
Release
release() returns a number to the available pool
Key Takeaway
๐ŸŽฏ Key Insight: Combining queue for O(1) assignment with set for O(1) lookup creates an optimal resource management system

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Queue operations and set lookups are all O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Queue and set each store up to n numbers

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค maxNumbers โ‰ค 104
  • 0 โ‰ค number < maxNumbers
  • At most 2 ร— 104 calls will be made to get, check, and release
Asked in
Google 35 Amazon 28 Microsoft 22 Apple 15
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen