Design a Memory Allocator
🧠 What is a memory allocator? It is the code behind
mallocandfree. The operating system gives a program a few big chunks of memory, and the allocator cuts them into the small pieces the program asks for: 24 bytes for a string here, 4 KB for a buffer there. When the program gives a piece back, the allocator remembers that it is free so it can hand it out again.
This problem shows up in systems-focused interviews (and as the hands-on part of "OS fundamentals" questions). It looks like pure algorithms, but the interviewer is really grading design: how you represent free and used memory, where the placement rule lives, and whether you notice that freeing memory is the hard part, not allocating it. The core ideas are free lists, first fit versus best fit, splitting and coalescing, and we build all four.
Requirements
"Design a memory allocator. It manages one fixed block of memory. Programs ask it for a number of bytes and get an address back, and later they free that address."
That is the whole prompt. The questions below turn it into rules we can code and test.
Clarifying Questions
You: What does an allocation return, and what if there is no room?
Interviewer: The start address of the block, as an offset into the heap: the heap goes from 0 to capacity minus one. If no free piece is large enough, return -1. Don't throw.
You: If several free pieces are big enough, which one do we use?
Interviewer: That is up to the caller. Support first fit (the lowest address that fits) and best fit (the smallest piece that fits, lowest address on a tie), chosen when the allocator is created.
You: When a block is freed right next to another free block, should they become one?
Interviewer: Yes, always. Two free blocks should never touch.
You: Do we need realloc?
Interviewer: Yes. Shrinking or growing a block. Keep it at the same address when you can; otherwise move it. If it cannot be done, the old block must stay valid.
You: What about bad calls, like freeing an address twice, or the middle of a block?
Interviewer: Return false and change nothing. Real allocators often crash or corrupt memory here; ours should detect it.
You: Should sizes be rounded up for alignment, and where do we store each block's size?
Interviewer: No alignment for now: sizes are exact bytes. Keep the bookkeeping outside the heap. We can talk about headers later.
You: Do several threads call it at once?
Interviewer: One thread for now.
The fourth answer sets the hardest rule. "Free blocks never touch" means every free has to look at both neighbours and merge, and the data structure has to make those neighbours easy to find.
💡 Interview tip: Ask what happens on a double free early. It tells you whether the allocator must remember exactly which addresses are allocated (it must, here), and it shows the interviewer you know where real allocators break.
Final Requirements
- The allocator manages a heap of
capacitybytes at addresses0tocapacity - 1, all free at the start. allocate(size)reservessizecontiguous bytes and returns the start address, or-1ifsizeis not positive or no free block is large enough.- The placement policy is first fit or best fit, picked at construction: first fit uses the lowest-address free block that fits; best fit uses the smallest one that fits, lowest address on a tie.
- An allocation takes the front of the chosen free block; the rest stays free (splitting).
free(address)releases the block that starts ataddress. Any other address (never allocated, the middle of a block, already freed) returnsfalse. A freed block merges with the free blocks right before and right after it (coalescing).reallocate(address, new_size)shrinks in place, grows in place when the next block is free and big enough, and otherwise moves the block. If it cannot, it returns-1and the old block is untouched.- The allocator reports free bytes, the largest free block and the number of free blocks, so callers can see fragmentation.
Out of scope
- Copying the actual bytes. We track addresses and sizes; a real
reallocwould also copy the contents. - Alignment, block headers and asking the OS for more memory (all in Extensibility).
- Several threads (also in Extensibility).
- Garbage collection: here the program frees memory itself. See /low-level-design/garbage-collector for the other side.