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
Software Engineering Fundamentals
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

0

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
memory allocation
fragmentation
free lists
coalescing
alignment
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. 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 Problems
Sample Answer
Requirements
  • Heap of size bytes. malloc(n) returns an offset or None; 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...


Submit Your Answer
Markdown supported