KatInterview

Best-Fit Block Allocator with Coalescing and Log-Time Operations

Data Structure DesignAuto-graded · 18 test cases
HardAuto-gradedData Structure Designdesignordered-settreap

Last updated

Simulate a best-fit extent allocator over a huge block range, where reserve and release must merge adjacent free runs and run in logarithmic time.

A storage service manages a volume of blocks blocks, numbered 0 to blocks - 1. At the start, every block is free. Files occupy extents, which are contiguous runs of blocks. You are given a batch of commands to run in order against one volume, and you must return what each command produces.

Commands

Command i has the name operations[i] and the integer list arguments[i].

"reserve" with arguments[i] = [n]

  • Find a free run of at least n blocks using the best-fit policy: choose the free run with the smallest length that is still >= n. If several free runs share that smallest length, choose the one with the lowest start address.
  • The extent is carved from the front of the chosen run. It occupies blocks start .. start + n - 1. Any leftover blocks stay free as a shorter run beginning at start + n.
  • The result is the start address of the new extent, or -1 if no free run is large enough. A failed reservation changes nothing.
  • If n <= 0, raise ValueError.

"release" with arguments[i] = [start]

  • Return the live extent that begins at start to the free pool. The result is None.
  • Coalescing: after the release, the freed run must merge with a free run that ends exactly at start and with a free run that begins exactly where the extent ended. A later large request can then use the merged space.
  • If start is not the start address of a currently live extent, raise KeyError. This covers an address that was never handed out and an extent that was already released.

Any other command name raises ValueError.

Task

Implement the function so that it returns a list with one entry per command: the start address or -1 for "reserve", and None for "release". An exception raised while processing a command simply propagates out of the function.

Both reserve and release should run in O(log F) time, where F is the current number of free runs. Use only the standard library.

Constraints

  • 0 <= blocks <= 10^12, so you cannot keep a per-block array.
  • Up to 10^6 commands.
  • reserve arguments and release arguments are integers. Reservation sizes may exceed blocks, in which case the answer is -1.
  • A volume with blocks = 0 has no free runs.
  • Free runs are always kept fully merged: no two free runs are ever adjacent.

Examples

Example 1

Input: simulate_extent_allocator(100, ['reserve', 'reserve', 'reserve', 'reserve', 'release', 'reserve', 'release', 'release', 'reserve'], [[30], [20], [40], [20], [30], [15], [0], [30], [50]])

Output: [0, 30, 50, -1, None, 30, None, None, 0]

Example 2

Input: simulate_extent_allocator(100, ['reserve', 'reserve', 'reserve', 'reserve', 'release', 'release', 'reserve'], [[30], [10], [10], [50], [0], [40], [10]])

Output: [0, 30, 40, 50, None, None, 40]

Hints

Hint 1

Think of the free space as a set of runs, each described by (start, length). Which orderings do you need: one by length for choosing a run, and some way to find runs adjacent to a given address?

Hint 2

Best fit with the lowest-address tie-break is exactly a ceiling query on pairs (length, start): find the smallest pair that is >= (n, -infinity). Python has no built-in ordered set, so build a balanced or randomized BST, such as a treap.

Hint 3

Keep two dictionaries for free runs, one keyed by start address and one keyed by end address. Then on release you can check both neighbours in O(1). Route every change through one add helper and one remove helper so the structures cannot disagree.