KatInterview

Leftmost-Fit Block Reservation with Owner Release

Data Structure DesignAuto-graded · 24 test cases
HardAuto-gradedData Structure Designsegment-treesimulationinterval-merging

Last updated

Simulate a storage pool that reserves the leftmost run of contiguous free cells per request and releases everything an owner holds, using efficient free-run tracking.

A storage pool has total_units cells, numbered 0 to total_units - 1. Every cell is free at the start. You are given a list of requests to handle in order. Each request yields exactly one output number.

There are two request types:

  • Reserve [1, length, owner]: look for the leftmost stretch of length contiguous free cells.
    • If such a stretch exists, mark all of its cells as held by owner and output the index of its first cell.
    • Otherwise leave the pool unchanged and output -1.
  • Release [2, owner]: free every cell currently held by owner and output how many cells were freed. Output 0 if the owner holds no cells.

Return the list of outputs, one per request, in order.

Rules

  • "Leftmost" means the stretch whose first cell has the smallest index among all stretches of length contiguous free cells. A stretch may begin in the middle of a longer free area. Only its starting index matters.
  • An owner may hold several disjoint stretches at once, acquired through separate reserve requests. A single release frees all of them.
  • Reserving for an owner that already holds cells is allowed and leaves its existing cells untouched.
  • After a release, the same owner ID may reserve again.

Constraints

  • 1 <= total_units <= 100000
  • 1 <= len(requests) <= 100000
  • Reserve requests have the form [1, length, owner] with 1 <= length <= total_units and 1 <= owner <= 100000.
  • Release requests have the form [2, owner] with 1 <= owner <= 100000.
  • Every output is -1 or an integer in [0, total_units].
  • The input sizes are large enough that rescanning the entire pool on every request will be too slow. Aim for roughly logarithmic time per request (amortized over the stretches released).

Examples

Example 1

Input: simulate_leftmost_fit(8, [[1, 3, 1], [1, 2, 2], [1, 2, 3], [2, 2], [1, 3, 4], [1, 2, 5], [2, 1], [1, 4, 6]])

Output: [0, 3, 5, 2, -1, 3, 3, -1]

Example 2

Input: simulate_leftmost_fit(5, [[1, 2, 7], [1, 1, 9], [1, 2, 7], [2, 7], [1, 5, 1]])

Output: [0, 2, 3, 4, -1]

Example 3

Input: simulate_leftmost_fit(1, [[2, 3], [1, 1, 3], [1, 1, 4], [2, 3], [1, 1, 4]])

Output: [0, 0, -1, 1, 0]

Hints

Hint 1

The leftmost stretch of `length` free cells always begins at the start of some maximal free run, specifically the leftmost run whose size is at least `length`. So you only need to track maximal free runs, not individual cells.

Hint 2

Store for each position the size of the maximal free run that starts there (0 if none starts there) in a max segment tree. To find the leftmost run of size at least `length`, descend from the root, going left whenever the left child's maximum is large enough.

Hint 3

On release, return each of the owner's stored stretches to the pool and merge with neighbors. The run to the right begins at `end + 1`, so you can read it from the tree. For the run to the left, keep a dictionary from a run's last index to its first index.