Remove failed samples

Write codeEasy10 min

Your task

A fitness band streams non-negative sensor readings, but a failed sample is recorded as -1. Given the readings list, remove every failed sample while keeping the valid readings in their original order, and return the compacted list. Do it in a single pass using a write pointer (overwrite the array in place, then cut it to length) rather than building a filtered copy per element check. Example: [7, -1, 8, -1, -1, 9] becomes [7, 8, 9].

Examples

compact_readings(readings: list[int]) -> list[int]
Example 1
Inputcompact_readings([7, -1, 8, -1, -1, 9])
Output[7, 8, 9]