Memory allocator
Design Memory Allocator, LeetCode 2502, is on OpenAI's LeetCode company tag lists, the all-time, three-month and six-month ones (3 sources). Three OpenAI candidates report it under the name malloc, one as the only question, with some tests provided (3 reports). It starts like this:
Build
Allocator(size)for an arena ofsizeunits, at addresses 0 tosize - 1. A block is a run of units in a row, known by its start address. Asizebelow 1 raisesValueError.
alloc(n)hands out a block ofnunits and returns its start: the lowest address wherenfree units in a row begin. When no such run exists, it returnsNoneand changes nothing, even if enough units are free in total. Annbelow 1 raisesValueError.free(addr)frees the live block that starts ataddrand returns the number of units it held. Any other address raisesValueErrorand changes nothing: one inside a block, a free one, one outside the arena, and the start of a block that was freed already.
Call on Allocator(10)Returns alloc(3)0alloc(4)3alloc(2)7alloc(2)None, because only unit 9 is freefree(3)4, the size of the block at 3free(3)raises ValueError: that block is free alreadyalloc(2)3free(8)raises ValueError: 8 is inside the block at 7
Level 2 gives each block an owner, adds release(owner) and free_runs(), and makes the arena 80 GiB, too big for one slot per unit. Level 3 adds aligned blocks and resize, which grows a block in place or reports that it can't.
You don't write it yet. The lesson builds the pieces first: a free list of runs, first fit, the merge on free, and the dicts that track blocks and their owners. At the end you solve the whole problem, level by level, in an interview simulation with a 45-minute timer.
This lesson builds on intervals: the free memory is a sorted list of runs, and freeing a block merges it with its neighbors the way the intervals lesson merges ranges.