Build a First-Fit Heap Allocator That Coalesces Free Blocks
Last updated: November 29, 2026
Quick Overview
Implement malloc and free over a fixed-size memory region using a free list with first-fit placement, 8-byte alignment, block splitting, and coalescing of adjacent free blocks. Explain fragmentation and how real allocators reduce it.
Microsoft
November 29, 20260
0
0 solved
Implement malloc and free over a fixed-size memory region using a free list with first-fit placement, 8-byte alignment, block splitting, and coalescing of adjacent free blocks. Explain fragmentation and how real allocators reduce it.
Systems teams at Microsoft, such as Windows kernel, Hyper-V and the C++ runtime, still ask candidates to reason about memory management directly. The interviewer checks correct splitting and coalescing logic, alignment, invalid free detection, and a solid discussion of fragmentation and allocator design trade-offs.
What the Interviewer Expects
- Maintains a free list sorted by address
- Rounds sizes up to the alignment boundary
- Splits larger blocks and removes exact fits
- Coalesces with both the previous and next neighbor on free
- Detects double free and invalid pointers
- Discusses external fragmentation and size-class allocators
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you store block headers inside the heap itself instead of a side dictionary?
- Compare first-fit, best-fit, and segregated free lists.
- How do allocators like the Windows Low Fragmentation Heap or mimalloc reduce contention across threads?
- How would you detect buffer overruns between blocks?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
- Heap of
sizebytes.malloc(n)returns an offset orNone;free_block(off)releases it. - Sizes are rounded up to multiples of 8.
- Freed neighbors must merge so a large request can succeed af...
High-Level Design
Keep a free list sorted by offset of (offset, length) and a map of allocated blocks. malloc scans for the first block big enough (first-fit), takes the front part, and leaves the remainder i...