Implement a Buddy Allocator With Split and Coalesce
Last updated: November 29, 2026
Quick Overview
Implement a buddy memory allocator over a power-of-two region with a minimum block size: alloc(size) returns the offset of the smallest fitting power-of-two block (splitting larger blocks as needed), and free(offset) returns it and repeatedly merges it with its free buddy.
Intel
November 29, 20260
0
0 solved
Implement a buddy memory allocator over a power-of-two region with a minimum block size: alloc(size) returns the offset of the smallest fitting power-of-two block (splitting larger blocks as needed), and free(offset) returns it and repeatedly merges it with its free buddy.
Intel driver, firmware and Linux kernel engineers work with buddy allocators for physical pages and GPU memory. The onsite version checks the XOR trick for locating a buddy, correct splitting and merging, and error handling for double frees.
What the Interviewer Expects
- Rounds requests up to a power of two and the minimum order
- Finds the smallest non-empty free list and splits down, pushing upper halves onto free lists
- Locates the buddy with offset XOR block size
- Coalesces repeatedly until the buddy is not free or the top order is reached
- Detects double free and invalid offsets
- Discusses internal fragmentation
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you make alloc O(1) per order instead of scanning sets for the lowest address?
- How does the Linux kernel keep per-CPU page lists in front of the buddy allocator, and why?
- How would you support allocations that need a specific alignment larger than their size?
- How would you measure and reduce external fragmentation over time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Region size and minimum block are powers of two. alloc(size) returns an offset or None when no block fits. free(offset) frees a block previously returned and raises on double free. Track free by...
High-Level Design
Keep free[order], a set of offsets of free blocks of size 2**order, and allocated[offset] = order. Alloc: compute the target order, find the smallest order at or above it with a free block, then...