Implement a Buddy Allocator Over a Fixed Memory Arena
Last updated: November 29, 2026
Quick Overview
Write a buddy allocator for a 2^k byte arena that serves power-of-two blocks, splits larger blocks on allocation, and merges a freed block with its buddy when both are free.
0
0
0 solved
Write a buddy allocator for a 2^k byte arena that serves power-of-two blocks, splits larger blocks on allocation, and merges a freed block with its buddy when both are free.
Systems teams at Google (gVisor, Fuchsia, TPU runtime memory) care how allocators trade fragmentation for speed. In an onsite this tests bit tricks for buddy addresses, careful bookkeeping, and a clear explanation of internal versus external fragmentation.
What the Interviewer Expects
- Round requests up to a power of two with a minimum block size
- Keep one free list per order
- Split from the smallest available larger block
- Find the buddy with an XOR on the offset
- Coalesce recursively on free and reject invalid frees
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 much internal fragmentation can this cause in the worst case?
- How would you make free O(1) to locate the block order without a side table?
- How does a slab allocator layered on top help small objects?
- How would you make this safe for many threads?
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
BuddyAllocator(total_order, min_order): arena of2**total_orderbytes, smallest block2**min_order.alloc(size)returns an offset orNoneif no block fits.free(offset)releases a bl...
High-Level Design
Free lists are indexed by order; each holds the offsets of free blocks of size 2**order. To allocate order o, find the smallest j >= o with a free block, pop it, and split repeatedly: the block ...