
The objective is to design a phone directory system where initially there are maxNumbers empty slots that can store phone numbers. Each slot can only store one phone number at a time.
The PhoneDirectory class should be able to:
For instance:
PhoneDirectory(int maxNumbers) sets up the directory with maxNumbers slots. int get() returns the next available number, or -1 if no numbers are available.bool check(int number) checks if a particular number is available (returns true) or not (returns false).void release(int number) releases a previously assigned number, making it available again.These functions ensure efficient management of available and assigned phone numbers within specified limits.
Input
Output
Explanation
1 <= maxNumbers <= 1040 <= number < maxNumbers2 * 104 calls will be made to get, check, and release.0 to maxNumbers-1.get() is called.check(int number).release(int number).Initialization:
Get Operations:
get(): Returns 0, the first available number. Now, the available numbers are 1 and 2.get() again: Returns 1. Now, the only available number is 2.Check Operation:
check(2): Returns true since 2 is still available.Get Operation:
get(): Returns 2. Now, no numbers are left. All numbers (0, 1, and 2) are assigned.Check Operation:
check(2): Returns false since 2 is now assigned and not available.Release Operation:
release(2): This makes number 2 available again.Check Operation:
check(2): Returns true now since 2 is available again after release.get, check, and release happen in reasonable time, especially given the constraints that up to 2 * 104 calls might be made.Implementing the above methods with attention to performance while effectively managing available and assigned slots will provide a robust phone directory system.
The provided C++ program defines a class DirectoryService that simulates a phone directory service where numbers can be acquired, checked for availability, and released. Here's how the class operations work:
Constructor (DirectoryService(int maxNumbers)): Initializes the directory with a set of phone numbers ranging from 0 to maxNumbers - 1. Each number is inserted into an unordered set availableNumbers, making these numbers available for acquisition.
Acquire Method (int acquire()): Provides the first available phone number from the set. If no numbers are available (i.e., the set is empty), it returns -1 to indicate that no numbers can be acquired. It removes the acquired number from the availableNumbers set to ensure it is not issued again until it's released.
IsAvailable Method (bool isAvailable(int number)): Checks if a specific number is available for acquisition by searching for the number in availableNumbers. Returns true if the number exists in the set (is available), otherwise returns false.
Free Method (void free(int number)): Reinserts a previously acquired number back into the availableNumbers set, making it available for future acquisitions. This method effectively allows a number to be reused after it's released.
This design offers a straightforward mechanism for phone number management within a defined range, using set operations for efficiently handling the availability of numbers.
0 Comments
Be the first to comment and share your perspective with the community.