Skip to main content

Design Phone Directory

Medium
Data Structures and Algorithms
Editorial
Topics:
Array
Hash Table
Linked List

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.

Need help or want more practice?

Practise in an interviewGet AI Coach help