Disk block writes with limited write cycles
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests heap, design and simulation.
A storage device has n blocks with ids 0 to n - 1. Every block can be written at most max_cycles times; after that it is worn out and can never be written again.
Implement a DiskManager class:
-
DiskManager(n: int, max_cycles: int)creates the disk. Every block starts withmax_cyclesremaining write cycles. -
write() -> intperforms one write and returns the id of the block it used. Choose the block with the fewest remaining cycles among blocks that still have at least one. If several blocks tie, choose the smallest id. The chosen block loses one cycle. If every block is worn out, return-1. -
replace(block_id: int) -> Noneswaps in a new physical block atblock_id. Its remaining cycles reset tomax_cycles. -
remaining(block_id: int) -> intreturns the block's remaining cycles.
Constraints
-
0 <= n <= 10^5,0 <= max_cycles <= 10^9 - Up to
10^5calls in total. - Every operation should run in better than
O(n)time.
Example 1
disk = DiskManager(3, 2)
write() -> 0, write() -> 0, write() -> 1, write() -> 1,
write() -> 2, write() -> 2, write() -> -1
Once block 0 has been written it has fewer cycles than the untouched blocks, so it is chosen again until it is worn out.
Example 2
disk = DiskManager(3, 3)
write() -> 0, write() -> 0, write() -> 0, write() -> 1 # block 1 has 2 left
replace(0) # block 0 back to 3
write() -> 1, write() -> 1 # 2 and then 1 are fewer than 3
write() -> 0 # blocks 0 and 2 both have 3; 0 is smaller
Clarifying questions
- Can
replacebe called on a worn-out block, making it writable again? (Yes.) - What should
replaceandremainingdo with an id outside0ton - 1: raise? - With
n = 0ormax_cycles = 0, does everywritereturn-1?
What a strong answer covers
- A min-heap of
(remaining, id)over blocks with at least one cycle left.writepops the top, takes one cycle, and pushes it back while it still has cycles. O(log n). -
replacesets the block back tomax_cyclesand pushes a new entry; a current-value array letswriteskip stale heap entries (lazy deletion). Or a sorted set with real removal, and the trade-off. - Never simulates cycles one by one, since
max_cyclescan be 10^9; builds the initial heap in O(n). - Handles writes once everything is worn out, and replacing a block that is still in the heap with another value.
- Tests both examples, a replace on a worn-out block, and
n = 0.
Follow-ups
- Wear levelling: pick the block with the most cycles left instead, and why a real SSD does that.
- Write
kblocks in one call. - List the blocks close to wearing out, below a threshold.
- Persist the state so a restart does not reset it.
Where it's asked
What it tests
heap · design · simulation
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
