Disk Space Manager
Storage Block Allocator
Design the part of a file system that hands out fixed-size disk blocks, takes them back, reports fragmentation, and packs files together again.
Overview
A disk is a long row of fixed-size blocks. A file system has to remember which blocks are free, hand some out when a file is written, take them back when it is deleted, and keep track of which blocks belong to which file. This problem asks you to design that allocator: the bitmap that records free and used blocks, the policy that picks blocks for a new file, and a defragmenter that moves files so each one sits in a single run.
The problem is a memory allocator with two differences. Blocks are all the same size, so there is no size field to manage. And position matters: a spinning disk reads a run of adjacent blocks much faster than the same blocks scattered across the platter (on an SSD the gap is smaller but still there), so the allocator cares not just whether space exists but whether it is together. That gap between 'enough free blocks' and 'a long enough free run' is external fragmentation, and it is the idea the interviewer is testing.
A strong answer names the bitmap in the first minute, gives its size (one bit per block), shows two allocation policies behind one interface, and writes a defragmenter that walks the disk once and provably keeps every file's data intact.
Scoping the problem
Agree on six things before you design. Each question below comes with why it matters and the answer used in the rest of this chapter.
- Fixed block size? Same-size blocks mean a bitmap is enough; variable sizes need a size-aware allocator. The answer is fixed, and the demo uses tiny blocks so the layout fits on one line.
- Scattered or contiguous allocation, or both? Contiguous reads faster but can fail when free space is split up. The answer is both, as interchangeable policies.
- Do we store the data or only the map? Storing bytes per block lets a test prove that defragmentation moved data correctly. So yes, a list of bytes per block.
- Can a file grow after it is written? Growing a contiguous file needs free blocks right after it. The answer is no; a rewrite frees the old blocks first.
- Many threads? Two writers must never get the same block. Yes, with one lock around every operation.
- When to defragment? That needs a metric. It runs on demand, and the metric is the share of files in more than one extent.
Functional requirements
- write(name, data) allocates ceil(len / block_size) blocks under the current policy and stores the data; rewriting a name frees its old blocks first
- read(name) returns the bytes in file order, trimmed to the stored size
- delete(name) frees the blocks and returns False for an unknown name
- Two policies: Scattered takes the lowest free blocks; Contiguous takes the first run long enough or refuses
- defragment() packs every file at the front of the disk in one extent each and returns how many blocks changed position
- fragmentation() is the share of files stored in more than one extent; stats() reports free blocks and the largest free run
- An allocation that cannot be met raises OutOfSpace and leaves the disk unchanged
Non-functional requirements
- The bitmap costs one bit per block: 32 KiB for a 1 GiB disk of 4 KiB blocks
- Block status and the free count are O(1); allocation scans are O(n) in the number of blocks
- Defragmentation walks the disk once and needs no spare blocks
- Data integrity: after any sequence of operations every file reads back exactly as written
- One lock makes write, read, delete and defragment safe from many threads; no block is ever handed to two files
Interview questions
- Why a bitmap rather than a free list?
- What is external fragmentation, and how do extents help?
- Walk me through the defragmenter. Why does swapping work, and what does it cost?
- How would you support directories and file metadata on top of this?
- Is one lock enough, and how would you do better?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio