~/problems / Binary search / Sorted containers (bisect)

OA: Memory allocator

hard 3 levels ~60 min OpenAI

Level 1 First-fit allocate and free

Build MemoryAllocator(total_capacity, strategy="first"). It hands out byte ranges of the address space [0, total_capacity), which starts out as a single free gap. You'll need strategy in level 2.

  • The constructor raises ValueError if total_capacity <= 0.
  • allocate(size) -> int: find the lowest-address free gap that holds at least size bytes (this is first fit). Hand out the front of it, so the gap shrinks from the left, or disappears on an exact fit. Return the block's start address. Raise ValueError if size <= 0 or if no single gap is big enough, even when the total free memory would be.
  • free(address, size) -> None: give back a block that allocate handed out, passing the same size. The freed range merges with a free gap that ends right where it starts and with one that starts right where it ends. So it can join the left gap, the right gap, both, or neither. Free gaps are never left touching each other.
    • Raise ValueError and change nothing if address isn't the start of a live allocation (never allocated, or already freed), if size doesn't match what was allocated there, or if the range falls outside [0, total_capacity).
  • free_blocks() -> list[tuple[int, int]]: every free gap as (start, size), sorted by start.
  • get_free_memory() -> int is the total free bytes. get_largest_free_block() -> int is the size of the largest gap (0 if memory is full).
m = MemoryAllocator(100)
m.allocate(20)      # 0
m.allocate(30)      # 20
m.allocate(40)      # 50      free: [(90, 10)]
m.free(20, 30)      #         free: [(20, 30), (90, 10)]
m.allocate(25)      # 20      free: [(45, 5), (90, 10)]
m.free(0, 20)       #         free: [(0, 20), (45, 5), (90, 10)]
m.free(20, 25)      #         free: [(0, 50), (90, 10)]   merged on both sides
m.allocate(60)      # raises ValueError: 60 bytes are free, but not in one piece

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

Topic: Sorted containers (bisect). Ordered-set operations in Python: bisect.insort, floor/ceiling lookups.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc