Code RoomFlash memory write erasures
MediumPrep Room Coding #4856

Flash memory write erasures

CodingAlgorithms & data structuresMid–Senior~20 min

A NOR flash page holds one word, and programming that word can only clear bits: a bit that already reads 0 can never be driven back to 1 without erasing the page, and an erase drives every bit of the word back to 1. The page starts erased. writes lists the values the firmware wants stored, in order, and word_bits gives the width of the word. Before each write the controller erases the page if and only if the wanted value cannot be reached from the stored value by clearing bits alone. Rewriting the value already stored costs nothing. Return how many erases the whole sequence costs. Every value is non-negative and fits inside word_bits bits.

Implement
flash_erase_cycles(writes: list[int], word_bits: int) → int
Examples
in[[12,8,15],4]out1
in[[7,7,7],4]out0
in[[0,1],4]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 20 min
InputExpectedGot
[[12,8,15],4]1not run yetsample
[[7,7,7],4]0not run yetsample
[[0,1],4]1not run yetsample