Parse 32-bit sensor packets and skip the ones that fail a nibble XOR parity check

Quick Overview

Decode 32-bit unsigned sensor packets into sensor type, distance, signal strength and sequence number, and drop any packet whose stored parity nibble does not equal the XOR of its other seven nibbles. Tests bit shifting, masking, nibble-level XOR and careful handling of unsigned values.

Parse 32-bit sensor packets and skip the ones that fail a nibble XOR parity check

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of raw sensor data packets. Each packet is a 32-bit unsigned integer that encodes the following fields in fixed bit ranges (bit 31 is the most significant bit, bit 0 the least significant): - Bits 31-28 (4 bits): sensor type (`0` = LiDAR, `1` = Radar, `2` = Ultrasonic, any other value = Unknown) - Bits 27-16 (12 bits): distance measurement, unsigned, in centimeters - Bits 15-8 (8 bits): signal strength, unsigned, 0 to 255 - Bits 7-4 (4 bits): parity nibble - Bits 3-0 (4 bits): sequence number, 0 to 15 A packet is malformed if its parity nibble does not match the expected value. The expected parity is the XOR of all non-parity nibbles: the sensor type nibble, the three nibbles of the distance field, the two nibbles of the signal strength field, and the sequence number nibble. For each valid (non-malformed) packet, produce a list `[sensor_type_string, distance, signal_strength, sequence_number]`. Skip malformed packets. Return the parsed results for all valid packets in the order they appear. ### Function Signature ```python def parse_sensor_packets(packets: list[int]) -> list[list]: ``` ### Rules - A nibble is a group of 4 bits. The seven nibbles in the parity check sit at bits 31-28, 27-24, 23-20, 19-16, 15-12, 11-8 and 3-0. The parity nibble itself (bits 7-4) is not part of the XOR. - Each output entry is `[name, distance, signal_strength, sequence_number]`, where `name` is one of the strings `"LiDAR"`, `"Radar"`, `"Ultrasonic"`, `"Unknown"` and the other three are integers. - Sensor type values 3 through 15 map to `"Unknown"`. Such a packet is still valid when its parity matches. - Keep the input order. If no packet is valid, return an empty list. ### Constraints - `1 <= len(packets) <= 10^5` - `0 <= packets[i] <= 2^32 - 1` ### Examples ```text Input: packets = [32819411, 285168898, 285168946] Output: [["LiDAR", 500, 200, 3], ["Radar", 255, 85, 2]] ``` In hex the packets are `0x01F4C8D3`, `0x10FF5502` and `0x10FF5532`; the first decodes to LiDAR, distance `0x1F4` = 500, signal `0xC8` = 200, sequence 3, and its seven nibbles 0, 1, F, 4, C, 8, 3 XOR to D, which equals its stored parity. The other two share the fields Radar, 255, 85, 2, whose nibbles XOR to 3, so the second (stored parity 0) is skipped and the third (stored parity 3) is kept. ```text Input: packets = [0, 4294967295, 539656121] Output: [["LiDAR", 0, 0, 0], ["Unknown", 4095, 255, 15], ["Ultrasonic", 42, 127, 9]] ``` Packet 0 has every field and its parity equal to 0, and `0xFFFFFFFF` has seven F nibbles whose XOR is F, matching its parity F, with type 15 mapping to Unknown. `539656121` is `0x202A7FB9`: type 2, distance `0x02A` = 42, signal `0x7F` = 127, sequence 9, and `2 ^ 0 ^ 2 ^ A ^ 7 ^ F ^ 9 = B` matches its stored parity B. ```text Input: packets = [268699648] Output: [] ``` `268699648` is `0x10040800`: type 1, distance 4, signal 8, stored parity 0, sequence 0. Its nibbles 1, 0, 0, 4, 0, 8, 0 XOR to D, which does not match 0, so the only packet is malformed and the result is empty.

Overview: Decode 32-bit unsigned sensor packets into sensor type, distance, signal strength and sequence number, and drop any packet whose stored parity nibble does not equal the XOR of its other seven nibbles. Tests bit shifting, masking, nibble-level XOR and careful handling of unsigned values.

Waymo logo
Waymo
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of raw sensor data packets. Each packet is a 32-bit unsigned integer that encodes the following fields in fixed bit ranges (bit 31 is the most significant bit, bit 0 the least significant):

  • Bits 31-28 (4 bits): sensor type ( 0 = LiDAR, 1 = Radar, 2 = Ultrasonic, any other value = Unknown)
  • Bits 27-16 (12 bits): distance measurement, unsigned, in centimeters
  • Bits 15-8 (8 bits): signal strength, unsigned, 0 to 255
  • Bits 7-4 (4 bits): parity nibble
  • Bits 3-0 (4 bits): sequence number, 0 to 15

A packet is malformed if its parity nibble does not match the expected value. The expected parity is the XOR of all non-parity nibbles: the sensor type nibble, the three nibbles of the distance field, the two nibbles of the signal strength field, and the sequence number nibble.

For each valid (non-malformed) packet, produce a list [sensor_type_string, distance, signal_strength, sequence_number]. Skip malformed packets. Return the parsed results for all valid packets in the order they appear.

Function Signature

def parse_sensor_packets(packets: list[int]) -> list[list]:

Rules

  • A nibble is a group of 4 bits. The seven nibbles in the parity check sit at bits 31-28, 27-24, 23-20, 19-16, 15-12, 11-8 and 3-0. The parity nibble itself (bits 7-4) is not part of the XOR.
  • Each output entry is [name, distance, signal_strength, sequence_number] , where name is one of the strings "LiDAR" , "Radar" , "Ultrasonic" , "Unknown" and the other three are integers.
  • Sensor type values 3 through 15 map to "Unknown" . Such a packet is still valid when its parity matches.
  • Keep the input order. If no packet is valid, return an empty list.

Constraints

  • 1 <= len(packets) <= 10^5
  • 0 <= packets[i] <= 2^32 - 1

Examples

Input: packets = [32819411, 285168898, 285168946]
Output: [["LiDAR", 500, 200, 3], ["Radar", 255, 85, 2]]

In hex the packets are 0x01F4C8D3, 0x10FF5502 and 0x10FF5532; the first decodes to LiDAR, distance 0x1F4 = 500, signal 0xC8 = 200, sequence 3, and its seven nibbles 0, 1, F, 4, C, 8, 3 XOR to D, which equals its stored parity. The other two share the fields Radar, 255, 85, 2, whose nibbles XOR to 3, so the second (stored parity 0) is skipped and the third (stored parity 3) is kept.

Input: packets = [0, 4294967295, 539656121]
Output: [["LiDAR", 0, 0, 0], ["Unknown", 4095, 255, 15], ["Ultrasonic", 42, 127, 9]]

Packet 0 has every field and its parity equal to 0, and 0xFFFFFFFF has seven F nibbles whose XOR is F, matching its parity F, with type 15 mapping to Unknown. 539656121 is 0x202A7FB9: type 2, distance 0x02A = 42, signal 0x7F = 127, sequence 9, and 2 ^ 0 ^ 2 ^ A ^ 7 ^ F ^ 9 = B matches its stored parity B.

Input: packets = [268699648]
Output: []

268699648 is 0x10040800: type 1, distance 4, signal 8, stored parity 0, sequence 0. Its nibbles 1, 0, 0, 4, 0, 8, 0 XOR to D, which does not match 0, so the only packet is malformed and the result is empty.