379. Design Phone Directory
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The core task is to design a system for managing a pool of phone numbers, which are integers from 0 to maxNumbers - 1. This system needs to support three main operations efficiently:
Solution
Design Phone Directory in Python
class PhoneDirectory:
def __init__(self, maxNumbers: int):
"""
Initialize your data structure here.
:param maxNumbers: The maximum numbers that can be stored in the phone directory.
"""
# A set to store all available phone numbers.
# Initialize it with all numbers from 0 to maxNumbers-1.
self.available = set(range(maxNumbers))
def get(self) -> int:
"""
Provide a number which is not assigned to anyone.
:return: Return an available number. Return -1 if none is available.
"""
# If the set is empty, no numbers are available.
if not self.available:
return -1
# pop() removes and returns an arbitrary element from the set in O(1) time.
return self.available.pop()
def check(self, number: int) -> bool:
"""
Check if a number is available or not.
"""
# A number is available if it is present in our 'available' set.
return number in self.available
def release(self, number: int) -> None:
"""
Recycle or release a number.
"""
# Add the number back to the set of available numbers.
# `add` will do nothing if the number is already present, which correctly
# handles the case of releasing a number that is already available.
self.available.add(number)
# Your PhoneDirectory object will be instantiated and called as such:
# obj = PhoneDirectory(maxNumbers)
# param_1 = obj.get()
# param_2 = obj.check(number)
# obj.release(number)
Design Phone Directory in Java
class PhoneDirectory {
// A hash set to store all available phone numbers.
private Set<Integer> available;
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
public PhoneDirectory(int maxNumbers) {
available = new HashSet<>();
// Populate the set with all numbers from 0 to maxNumbers-1.
for (int i = 0; i < maxNumbers; i++) {
available.add(i);
}
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
public int get() {
// If the set is empty, no numbers are available.
if (available.isEmpty()) {
return -1;
}
// Get an arbitrary element from the set using an iterator.
Iterator<Integer> it = available.iterator();
int number = it.next();
// Remove the element from the set to mark it as used.
it.remove(); // Using iterator's remove is efficient.
return number;
}
/** Check if a number is available or not. */
public boolean check(int number) {
// A number is available if it is present in our 'available' set.
return available.contains(number);
}
/** Recycle or release a number. */
public void release(int number) {
// Add the number back to the set of available numbers.
// `add` will return false and do nothing if the number is already present,
// which correctly handles the case of releasing an already available number.
available.add(number);
}
}
/**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj.get();
* boolean param_2 = obj.check(number);
* obj.release(number);
*/
Design Phone Directory in C++
class PhoneDirectory {
private:
// A hash set to store all available phone numbers.
std::unordered_set<int> available;
public:
/** Initialize your data structure here
@param maxNumbers - The maximum numbers that can be stored in the phone directory. */
PhoneDirectory(int maxNumbers) {
// Populate the set with all numbers from 0 to maxNumbers-1.
for (int i = 0; i < maxNumbers; ++i) {
available.insert(i);
}
}
/** Provide a number which is not assigned to anyone.
@return - Return an available number. Return -1 if none is available. */
int get() {
// If the set is empty, no numbers are available.
if (available.empty()) {
return -1;
}
// Get an arbitrary element from the set. `*available.begin()` is an easy way.
int num = *available.begin();
// Remove the element from the set to mark it as used.
available.erase(available.begin());
return num;
}
/** Check if a number is available or not. */
bool check(int number) {
// A number is available if it is present in our 'available' set.
// `count` returns 1 if the element is present, 0 otherwise.
return available.count(number) > 0;
}
/** Recycle or release a number. */
void release(int number) {
// Add the number back to the set of available numbers.
// `insert` will do nothing if the number is already present, which correctly
// handles the case of releasing a number that is already available.
available.insert(number);
}
};
/**
* Your PhoneDirectory object will be instantiated and called as such:
* PhoneDirectory* obj = new PhoneDirectory(maxNumbers);
* int param_1 = obj->get();
* bool param_2 = obj->check(number);
* obj->release(number);
*/
Design Phone Directory in TypeScript
class PhoneDirectory {
// A set to store all available phone numbers.
private available: Set<number>;
constructor(maxNumbers: number) {
this.available = new Set<number>();
// Populate the set with all numbers from 0 to maxNumbers-1.
for (let i = 0; i < maxNumbers; i++) {
this.available.add(i);
}
}
get(): number {
// If the set is empty, no numbers are available.
if (this.available.size === 0) {
return -1;
}
// Get an arbitrary element from the set using an iterator.
const iterator = this.available.values();
const number = iterator.next().value;
// Remove the element from the set to mark it as used.
this.available.delete(number);
return number;
}
check(number: number): boolean {
// A number is available if it is present in our 'available' set.
return this.available.has(number);
}
release(number: number): void {
// Add the number back to the set of available numbers.
// `add` will do nothing if the number is already present, which correctly
// handles the case of releasing a number that is already available.
this.available.add(number);
}
}
/**
* Your PhoneDirectory object will be instantiated and called as such:
* var obj = new PhoneDirectory(maxNumbers)
* var param_1 = obj.get()
* var param_2 = obj.check(number)
* obj.release(number)
*/
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.