Design a contiguous segment allocator
Problem Description
Design an in-memory contiguous segment allocator that operates over an array of n cells, indexed from 0 to n-1. All cells are initially free.
The allocator must support the following two operations:
allocate(len, tag):- Find the lowest index
isuch that the contiguous block of cells[i, i+len-1]are all currently free. - Mark these
lencells with the providedtagidentifier. - Return the starting index
i. - If no such contiguous block of
lenfree cells exists, return-1.
- Find the lowest index
release(tag):- Free every cell that is currently marked with the given
tag. - Return the total number of cells that were released.
- Free every cell that is currently marked with the given
Performance Requirements
The goal is to achieve efficient performance for large n, specifically aiming for O(log n) time complexity per operation.
Edge Cases and Considerations
The implementation should robustly handle various edge cases, including:
- Repeated
tagidentifiers. - Attempting to release a
tagthat does not currently mark any cells. - Memory fragmentation resulting from arbitrary sequences of
allocateandreleaseoperations. - Invalid
lenvalues (e.g.,len <= 0orlen > n).
Deliverables
- Specify and implement the underlying data structures.
- Provide a complexity analysis for both
allocateandreleaseoperations. - Include brief tests demonstrating the correctness of the implementation.
AI interview prep
Core dilemma
Achieving O(log n) performance for both finding the *lowest index first-fit* contiguous free block during allocation, and for releasing potentially multiple non-contiguous segments associated with a single tag, all while managing fragmentation efficiently.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design a contiguous segment allocator
Problem Description
Design an in-memory contiguous segment allocator that operates over an array of n cells, indexed from 0 to n-1. All cells are initially free.
The allocator must support the following two operations:
allocate(len, tag):- Find the lowest index
isuch that the contiguous block of cells[i, i+len-1]are all currently free. - Mark these
lencells with the providedtagidentifier. - Return the starting index
i. - If no such contiguous block of
lenfree cells exists, return-1.
- Find the lowest index
release(tag):- Free every cell that is currently marked with the given
tag. - Return the total number of cells that were released.
- Free every cell that is currently marked with the given
Performance Requirements
The goal is to achieve efficient performance for large n, specifically aiming for O(log n) time complexity per operation.
Edge Cases and Considerations
The implementation should robustly handle various edge cases, including:
- Repeated
tagidentifiers. - Attempting to release a
tagthat does not currently mark any cells. - Memory fragmentation resulting from arbitrary sequences of
allocateandreleaseoperations. - Invalid
lenvalues (e.g.,len <= 0orlen > n).
Deliverables
- Specify and implement the underlying data structures.
- Provide a complexity analysis for both
allocateandreleaseoperations. - Include brief tests demonstrating the correctness of the implementation.