KatInterview

Best-Fit Block Manager with Splitting and Merging

Data Structure DesignAuto-graded · 21 test cases
HardAuto-gradedData Structure Designsimulationbinary-searchmemory-allocator

Last updated

Simulate a best-fit memory manager on a fixed address range, handling splits, exact-start frees, invalid frees, and merging of neighboring free blocks using plain lists.

Best-Fit Block Manager with Splitting and Merging

You are managing a linear region of memory whose addresses run from 0 up to, but not including, capacity. At the start, the whole region is a single free block beginning at address 0.

You receive a list of requests and must process them in order. Each request is a pair (kind, value):

  • ("alloc", size) asks for a block of size units.
    • Look at all free blocks whose length is at least size, and pick the one with the smallest length.
    • If several qualify with the same smallest length, pick the one with the lowest starting address.
    • Hand out the first size units of that block. Any leftover units at the end remain as a new, smaller free block.
    • The result of the request is the starting address of the handed-out block.
    • If no free block is long enough, the result is -1 and nothing changes.
  • ("free", pointer) gives back a previously handed-out block.
    • It succeeds only if some block that is currently handed out begins at exactly pointer. In that case the block becomes free, it is merged with any free block that directly touches it on the left and/or on the right (so no two free blocks are ever adjacent), and the result is 1.
    • Otherwise the result is 0 and nothing changes. This covers an address that was never the start of a block, an address in the middle of a block, an address that is the start of a free block, and releasing the same block twice.

Return a list with one integer result per request, in the same order as the requests.

Function

Implement run_allocator(capacity, operations):

  • capacity: total number of address units.
  • operations: list of (kind, value) pairs, where kind is "alloc" or "free".
  • Returns a list of integers, one per request.

Constraints

  • 1 <= capacity <= 10_000
  • 1 <= len(operations) <= 10_000
  • For "alloc", size is an integer in [1, capacity].
  • For "free", pointer is an integer in [0, capacity - 1].
  • Keep your state in plain Python lists and scalar variables. Do not use trees, heaps, hash maps/dicts/sets, ordered-map libraries, or bisect.
  • If you keep a list ordered by size, write the binary search and insertion yourself.
  • Block boundaries and merging must be derived from the state your code maintains, not from a precomputed answer.

Examples

Example 1

Input: run_allocator(8, [['alloc', 3], ['alloc', 2], ['free', 0], ['alloc', 4]])

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

Hints

Hint 1

Think about which questions you need to answer quickly: which free block is the best fit, who are a freed block's neighbors by address, and is a given pointer the start of a live block. Different orderings of the data suit different questions.

Hint 2

If you keep free blocks sorted by the pair (length, start), then the first entry with length >= size is exactly the best fit with the correct tie-break. A hand-written binary search finds it.

Hint 3

On free, a pointer that is not an exact start in the allocated list means result 0. Otherwise insert the block into the address-ordered free list and check only its immediate left and right neighbors for touching; remember to remove absorbed neighbors from every list that holds them.