Code RoomSingle-bit error correction
MediumPrep Room Coding #4858

Single-bit error correction

CodingAlgorithms & data structuresMid–Senior~22 min

A memory controller protects each stored word by position parity. Number the bit positions 1, 2, 3 and upward starting at the least significant bit. A word is intact when the XOR of the positions of all its set bits is zero, and the controller only ever writes words that satisfy that. A stray particle flips at most one bit of a word while it sits in storage. words holds the values read back and word_bits is the width of each word. Return the repaired values in the same order. Leave a word alone when its position XOR is zero, otherwise read that XOR as the position that flipped and flip it back. When the position it names is greater than word_bits the word is beyond repair, so leave that one alone too.

Implement
repair_hamming_words(words: list[int], word_bits: int) → list[int]
Examples
in[[0],8]out[0]
in[[23],8]out[7]
in[[18],5]out[18]
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 22 min
InputExpectedGot
[[0],8][0]not run yetsample
[[23],8][7]not run yetsample
[[18],5][18]not run yetsample