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.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Software Engineering Fundamentals
Hard

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
buddy allocator
memory fragmentation
free lists
bit manipulation
operating systems
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 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 Problems
Sample Answer
Requirements
  • BuddyAllocator(total_order, min_order): arena of 2**total_order bytes, smallest block 2**min_order.
  • alloc(size) returns an offset or None if 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 ...


Submit Your Answer
Markdown supported