> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Design Phone Directory Python Solution

> Tested Python solution for LeetCode 379 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 379, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Queue](/catalog/topics/queue), [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/design-phone-directory/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 379   # by problem number
lcpy gen -s design_phone_directory   # by problem name
```

## Problem

Design a phone directory that initially has `maxNumbers` empty slots that can store numbers. The directory should store numbers, check if a certain slot is empty or not, and empty 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`.

### Examples

```
Input
["PhoneDirectory", "get", "get", "check", "get", "check", "release", "check"]
[[3], [], [], [2], [], [2], [2], [2]]
Output
[null, 0, 1, true, 2, false, null, true]

Explanation
PhoneDirectory phoneDirectory = new PhoneDirectory(3);
phoneDirectory.get();      // It can return any available phone number. Here we assume it returns 0.
phoneDirectory.get();      // Assume it returns 1.
phoneDirectory.check(2);   // The number 2 is available, so return true.
phoneDirectory.get();      // It returns 2, the only number that is left.
phoneDirectory.check(2);   // The number 2 is no longer available, so return false.
phoneDirectory.release(2); // Release number 2 back to the pool.
phoneDirectory.check(2);   // Number 2 is available again, return true.
```

### Constraints

* `1 <= maxNumbers <= 10^4`
* `0 <= number < maxNumbers`
* At most `2 * 10^4` calls will be made to `get`, `check`, and `release`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_phone_directory/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_phone_directory/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque


class PhoneDirectory:
    # Time: O(1) per operation
    # Space: O(max_numbers)
    def __init__(self, max_numbers: int) -> None:
        self.free = deque(range(max_numbers))
        self.free_set = set(range(max_numbers))

    def get(self) -> int:
        if not self.free:
            return -1
        number = self.free.popleft()
        self.free_set.remove(number)
        return number

    def check(self, number: int) -> bool:
        return number in self.free_set

    def release(self, number: int) -> None:
        if number not in self.free_set:
            self.free_set.add(number)
            self.free.append(number)
```

## Complexity

| Time | Space |
| - | - |
| O(1) per operation | O(max\_numbers) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.