Design Phone Directory - Problem

Design a phone directory that initially has maxNumbers empty slots that can store numbers. The directory should support storing numbers, checking if a certain slot is empty, and emptying a given slot.

Implement the PhoneDirectory class:

  • PhoneDirectory(int maxNumbers) - Initializes the phone directory with the number of available slots maxNumbers
  • int get() - Provides a number that is not assigned to anyone. Returns -1 if no number is available
  • bool check(int number) - Returns true if the slot number is available and false otherwise
  • void release(int number) - Recycles or releases the slot number

All operations should be performed efficiently to handle multiple queries.

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["PhoneDirectory", "get", "check", "get", "check", "release", "check"], parameters = [[3], [], [2], [], [2], [2], [2]]
Output: [null, 0, true, 1, true, null, true]
💡 Note: Initialize directory with 3 slots. get() returns 0 (first available). check(2) returns true (slot 2 available). get() returns 1. check(2) returns true (slot 2 still available). release(2) has no effect since slot 2 was never used. check(2) returns true.
Example 2 — Full Directory
$ Input: operations = ["PhoneDirectory", "get", "get", "get"], parameters = [[2], [], [], []]
Output: [null, 0, 1, -1]
💡 Note: Directory with 2 slots. First get() returns 0, second returns 1, third returns -1 (no slots available).
Example 3 — Release and Reuse
$ Input: operations = ["PhoneDirectory", "get", "get", "release", "get"], parameters = [[2], [], [], [0], []]
Output: [null, 0, 1, null, 0]
💡 Note: Get slots 0 and 1, release slot 0, then get() returns the released slot 0.

Constraints

  • 1 ≤ maxNumbers ≤ 104
  • 0 ≤ number < maxNumbers
  • At most 2 × 104 calls will be made to get, check, and release

Visualization

Tap to expand
Design Phone Directory INPUT Phone Directory (maxNumbers=3) Slot 0 Slot 1 Slot 2 Available Queue: 0 1 2 front Operations: 1. PhoneDirectory(3) 2. get() 3. check(2) 4. get() 5. check(2) 6. release(2) 7. check(2) ALGORITHM STEPS 1 Initialize Queue with [0,1,2] Set tracks used slots 2 get() Method Dequeue from front Mark as used in Set 3 check() Method Return true if NOT in used Set 4 release() Method Remove from used Set Enqueue back to queue State After Operations: After get(): Q=[1,2] used={0} check(2): 2 not in used After get(): Q=[2] used={0,1} release(2): Q=[2] used={0,1} check(2): 2 not in used FINAL RESULT Operation Results: PhoneDirectory(3) null get() 0 check(2) true get() 1 check(2) false release(2) null check(2) true Output Array: [null, 0, true, 1, false, null, true] OK - All operations O(1) Key Insight: Using a Queue + HashSet combination enables O(1) time for all operations. The queue stores available numbers for fast get(), while the HashSet tracks used numbers for instant check() lookups. Release() adds back to queue and removes from set - both O(1). Space complexity is O(n). TutorialsPoint - Design Phone Directory | Queue-Based Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
18.5K Views
Medium Frequency
~25 min Avg. Time
245 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