379. Design Phone Directory
Design Phone Directory (LeetCode 379) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
How to solve Design Phone Directory
Design Phone Directory is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
Work towards the solution in these steps:
- Consider how you can efficiently track which phone numbers are currently available and which are assigned at any given time.
- Think about using a data structure that allows quick retrieval of an available number and supports efficient addition when numbers are released.
- A queue or a set could be useful to store available numbers, enabling O(1) or near O(1) operations for assignment and release.
The reference solution runs in O(1) time and O(N) space.
The Design Phone Directory solution and editorial walks through the full approach with reference code.
Problem statement
You are given an integer maxNumbers representing the total number of phone numbers in a directory, numbered from 0 to maxNumbers - 1. Design a system that manages the assignment and recycling of these phone numbers. Implement a class PhoneDirectory with the following methods:
- PhoneDirectory(int maxNumbers): Initializes the directory with maxNumbers available phone numbers. - int get(): Provides an available phone number and marks it as assigned. Returns the number if available; otherwise, returns -1 if none are available. - bool check(int number): Returns true if the given number is available; otherwise, returns false. - void release(int number): Recycles or releases the given number, making it available for assignment again if it was previously assigned.
You must ensure that each number is assigned to at most one user at a time, and that released numbers can be reassigned. Implement the class to efficiently support up to 2 10^4 calls to the above methods.
Examples
Example 1
Input: {"commands": ["PhoneDirectory", "get", "get", "get", "get", "check"], "arguments": [[2], [], [], [], [], [2]]}
Output: [null, 0, 1, -1, -1, false]
Explanation: Initializes directory with 3 numbers, gets two numbers, checks if 2 is available, releases 2, checks again, and gets the last number.
Example 2
Input: {"commands": ["PhoneDirectory", "get", "get", "release", "get", "check", "release", "get", "check", "release", "get", "get"], "arguments": [[3], [], [], [2], [], [2], [0], [], [0], [1], [], []]}
Output: [null, 0, 1, null, 2, false, null, 0, false, null, 1, -1]
Explanation: The directory hands out 0, 1 and then 2, reissues 0 and 1 after each is released, and has none left for the final get(), which returns -1.
Topics and companies
Topics: Array, Hash Table, Linked List, Design, Queue.
Reported in interviews at Google.
How to practise Design Phone Directory on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) time and O(N) space.
Open Design Phone Directory in the code editor, or read the Design Phone Directory editorial for a worked solution with its approach and complexity analysis.
Where Design Phone Directory sits in the DSA pattern sheet
Problems related to Design Phone Directory
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern
Design Phone Directory FAQ
What is the LeetCode number of Design Phone Directory?
Design Phone Directory is LeetCode problem 379, rated Medium.
What pattern is used to solve Design Phone Directory?
Design Phone Directory is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
What is the time complexity of Design Phone Directory?
The reference solution runs in O(1) time and O(N) space.
Which companies ask Design Phone Directory?
Design Phone Directory has been reported in interviews at Google.