Design an ID Allocator That Always Returns the Smallest Free ID
Quick Overview
Design an ID allocator over the range 0 to maxId - 1 that always hands out the smallest free ID, supports releasing IDs and checking whether one is taken, and returns -1 when the pool is empty. Tests data structure choice, keeping two structures consistent and per-operation cost.
Design an ID Allocator That Always Returns the Smallest Free ID
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a class that manages the allocation and release of unique IDs from a pool of IDs ranging from `0` to `maxId - 1`.
The class always allocates the smallest available ID, and allocation returns `-1` when no ID is available. A previously allocated ID can be released back into the pool so a later call can allocate it again. Releasing an ID that is not currently allocated has no effect. The class also reports whether a given ID is currently allocated.
Implement the `IDAllocator` class:
- `IDAllocator(maxId: int)` creates a pool holding the IDs `0` to `maxId - 1`, all of them free.
- `allocate() -> int` allocates and returns the smallest available ID, or returns `-1` if no ID is available.
- `release(id: int) -> None` returns a previously allocated ID to the pool. If `id` is not currently allocated, nothing happens.
- `check(id: int) -> bool` returns `True` if `id` is currently allocated and `False` otherwise.
Example 1:
```text
alloc = IDAllocator(5) # pool: {0, 1, 2, 3, 4}
alloc.allocate() # 0
alloc.allocate() # 1
alloc.allocate() # 2
alloc.check(1) # True
alloc.release(1) # ID 1 goes back to the pool
alloc.check(1) # False
alloc.allocate() # 1, the smallest free ID again
alloc.allocate() # 3
```
Example 2:
```text
alloc = IDAllocator(3) # pool: {0, 1, 2}
alloc.allocate() # 0
alloc.allocate() # 1
alloc.release(0) # ID 0 goes back to the pool
alloc.release(2) # no effect, ID 2 was never allocated
alloc.allocate() # 0
alloc.check(0) # True
alloc.allocate() # 2
alloc.allocate() # -1, no IDs left
alloc.allocate() # -1
```
```hint Two different questions
`allocate` asks for the smallest free ID, and `check` asks whether one particular ID is taken. Each question is cheap to answer with a different data structure.
```
```hint Watch the release path
Consider what happens to your pool if `release` is called on an ID that is already free.
```
### Constraints & Assumptions
- `1 <= maxId <= 10^5`
- `0 <= id < maxId` for every `release` and `check` call.
- At most `2 * 10^5` calls are made in total to `allocate`, `release` and `check`.
### Clarifying Questions to Ask
- Will the allocator be called from several threads at once?
- Could `maxId` grow far beyond `10^5`, to the point where storing every free ID up front is too expensive?
- Should an out-of-range `id` passed to `release` or `check` raise an error, or be treated like any other unallocated ID?
### What a Strong Answer Covers
- A way to find the smallest free ID quickly and a way to answer membership queries, and how the two stay consistent.
- Releasing a free or never-allocated ID without corrupting the pool or creating duplicate IDs.
- The time cost of each operation and of the constructor, plus the memory used.
- Behavior when the pool is exhausted, and small cases such as `maxId = 1`.
### Follow-up Questions
- How would you make the allocator safe to call from many threads at once?
- How would you support a `maxId` of about four billion while keeping memory proportional to the number of IDs actually handed out?
- How would you add `claim(id)`, which allocates a specific ID if it is free?
- If `allocate` could return any free ID instead of the smallest, what simpler design would you choose?
Overview: Design an ID allocator over the range 0 to maxId - 1 that always hands out the smallest free ID, supports releasing IDs and checking whether one is taken, and returns -1 when the pool is empty. Tests data structure choice, keeping two structures consistent and per-operation cost.
Design a class that manages the allocation and release of unique IDs from a pool of IDs ranging from 0 to maxId - 1.
The class always allocates the smallest available ID, and allocation returns -1 when no ID is available. A previously allocated ID can be released back into the pool so a later call can allocate it again. Releasing an ID that is not currently allocated has no effect. The class also reports whether a given ID is currently allocated.
Implement the IDAllocator class:
IDAllocator(maxId: int)
creates a pool holding the IDs
0
to
maxId - 1
, all of them free.
allocate() -> int
allocates and returns the smallest available ID, or returns
-1
if no ID is available.
release(id: int) -> None
returns a previously allocated ID to the pool. If
id
is not currently allocated, nothing happens.
check(id: int) -> bool
returns
True
if
id
is currently allocated and
False
otherwise.
Example 1:
alloc = IDAllocator(5) # pool: {0, 1, 2, 3, 4}
alloc.allocate() # 0
alloc.allocate() # 1
alloc.allocate() # 2
alloc.check(1) # True
alloc.release(1) # ID 1 goes back to the pool
alloc.check(1) # False
alloc.allocate() # 1, the smallest free ID again
alloc.allocate() # 3
Example 2:
alloc = IDAllocator(3) # pool: {0, 1, 2}
alloc.allocate() # 0
alloc.allocate() # 1
alloc.release(0) # ID 0 goes back to the pool
alloc.release(2) # no effect, ID 2 was never allocated
alloc.allocate() # 0
alloc.check(0) # True
alloc.allocate() # 2
alloc.allocate() # -1, no IDs left
alloc.allocate() # -1
Constraints & Assumptions
1 <= maxId <= 10^5
0 <= id < maxId
for every
release
and
check
call.
At most
2 * 10^5
calls are made in total to
allocate
,
release
and
check
.
Clarifying Questions to Ask Guidance
Will the allocator be called from several threads at once?
Could
maxId
grow far beyond
10^5
, to the point where storing every free ID up front is too expensive?
Should an out-of-range
id
passed to
release
or
check
raise an error, or be treated like any other unallocated ID?
What a Strong Answer Covers Guidance
A way to find the smallest free ID quickly and a way to answer membership queries, and how the two stay consistent.
Releasing a free or never-allocated ID without corrupting the pool or creating duplicate IDs.
The time cost of each operation and of the constructor, plus the memory used.
Behavior when the pool is exhausted, and small cases such as
maxId = 1
.
Follow-up Questions Guidance
How would you make the allocator safe to call from many threads at once?
How would you support a
maxId
of about four billion while keeping memory proportional to the number of IDs actually handed out?
How would you add
claim(id)
, which allocates a specific ID if it is free?
If
allocate
could return any free ID instead of the smallest, what simpler design would you choose?