← Questions

Disk block writes with limited write cycles

0
AmazonCoding

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 with max_cycles remaining write cycles.
  • write() -> int performs 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) -> None swaps in a new physical block at block_id. Its remaining cycles reset to max_cycles.
  • remaining(block_id: int) -> int returns the block's remaining cycles.

Constraints

  • 0 <= n <= 10^5, 0 <= max_cycles <= 10^9
  • Up to 10^5 calls 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 replace be called on a worn-out block, making it writable again? (Yes.)
  • What should replace and remaining do with an id outside 0 to n - 1: raise?
  • With n = 0 or max_cycles = 0, does every write return -1?

What a strong answer covers

  • A min-heap of (remaining, id) over blocks with at least one cycle left. write pops the top, takes one cycle, and pushes it back while it still has cycles. O(log n).
  • replace sets the block back to max_cycles and pushes a new entry; a current-value array lets write skip stale heap entries (lazy deletion). Or a sorted set with real removal, and the trade-off.
  • Never simulates cycles one by one, since max_cycles can 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 k blocks 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding