← Questions

Memory allocator: leftmost free block and free by owner

0
OpenAICodingLast asked Sep 2026

A memory has n units, numbered 0 to n - 1, all free at the start. Process a list of operations in order, each producing one recorded value:

  1. Allocate size units for an owner: find the leftmost run of size consecutive free units, give all of them to the owner and record the run's first index, or record -1 if no run is long enough.
  2. Free by owner: release every unit the owner holds and record how many units were freed.

Return the recorded values. Start with a direct simulation, then discuss how to make allocation faster when the memory is large.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding