Next-Fit Contiguous Block Allocator Over a Free Bitmap
Last updated: November 29, 2026
Quick Overview
Implement an allocator over N flash blocks tracked by a bitmap with alloc(k), which returns the start of k contiguous free blocks using next-fit from the last allocation (wrapping once), and free(start, k), which rejects double frees.
Pure Storage
November 29, 20260
0
0 solved
Implement an allocator over N flash blocks tracked by a bitmap with alloc(k), which returns the start of k contiguous free blocks using next-fit from the last allocation (wrapping once), and free(start, k), which rejects double frees.
Because DirectFlash modules expose raw NAND, Purity manages space allocation itself, and bitmap allocators are a natural first coding question for data path teams. The interviewer watches for careful loop bounds, wrap-around handling and defensive checks that a storage engineer would add by reflex.
What the Interviewer Expects
- Scans with a running count of consecutive free bits and resets on a used bit
- Implements next-fit with a single wrap and correct stopping point
- Returns -1 for impossible or failed requests instead of raising
- Detects double frees and explains why that matters for metadata corruption
- States the O(N) worst case and how to speed it up
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 faster than a linear scan for large N?
- Why might next-fit reduce fragmentation or wear compared to first-fit on flash?
- How would you make this safe for concurrent allocators on multiple cores?
- How would you persist the bitmap so a crash cannot leak or double-allocate blocks?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
alloc(k) returns the first index s such that blocks s..s+k-1 are free, searching from a hint (just after the last allocation) to the end and then from 0, and marks them used. It returns -1 if no run...
High-Level Design
Keep a bytearray bitmap and a hint. Pass one scans [hint, N). Pass two scans from 0 up to hint + k - 1, which is enough to find any run that starts before the hint (a run starting at hint-1 en...