Code RoomUnpack signed converter samples
MediumPrep Room Coding #4892

Unpack signed converter samples

CodingAlgorithms & data structuresMid–Senior~22 min

A vibration logger packs signed converter samples end to end into 16 bit halfwords. Sample 0 starts at bit 0 of halfwords[0], counting the least significant bit of a halfword as bit 0. Each later sample starts where the one before it ended, and a sample that runs past bit 15 carries on in the low bits of the next halfword. Every sample is width bits wide and is held in two's complement, so a sample whose top bit is set reads as a negative value. width is between 2 and 12, and each halfword holds a value from 0 to 65535. Return the first count samples as signed values, in the order they were logged. Return an empty list when count is 0, and ignore any halfwords left over past the last sample asked for.

Implement
decode_sample_stream(halfwords: list[int], width: int, count: int) → list[int]
Examples
in[[34801],4,4]out[1,-1,7,-8]
in[[16259,24],5,5]out[3,-4,15,-16,1]
in[[2047,128,0],12,3]out[2047,-2048,0]
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
[[34801],4,4][1,-1,7,-8]not run yetsample
[[16259,24],5,5][3,-4,15,-16,1]not run yetsample
[[2047,128,0],12,3][2047,-2048,0]not run yetsample