Design a sensor data aggregator that buffers readings into synchronized frames
Company: Waymo
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a sensor data aggregator for a vehicle equipped with multiple sensors such as LiDAR, radar and cameras. The system collects timestamped data readings from each registered sensor and buffers them until all sensors have reported data for a given timestamp.
When all registered sensors have provided data for the same timestamp, that timestamp's data is a synchronized frame and is ready to be consumed by a perception module. The system supports registering sensors, adding data, querying synchronized frames, flushing consumed frames, and inspecting pending (unsynchronized) data.
Sensor types must be one of `'lidar'`, `'radar'` or `'camera'`. Once a timestamp becomes synchronized, no further data can be added to it. All outputs that involve multiple sensors or timestamps must be returned in deterministic sorted order.
Implement the following methods:
- `register_sensor(sensor_id: str, sensor_type: str) -> bool`: register a sensor with the given ID and type (`'lidar'`, `'radar'` or `'camera'`). Return `True` if it is newly registered, `False` if the sensor ID already exists or the type is invalid.
- `add_data(sensor_id: str, timestamp: int, data: str) -> bool`: add a timestamped reading from a registered sensor. Return `True` if it is accepted, `False` if the sensor is unregistered or the timestamp is already synchronized.
- `get_synchronized_frames() -> List[List[str]]`: return all synchronized frames sorted by timestamp. Each frame is a list of `'sensor_id:data'` strings sorted by sensor_id.
- `flush_synchronized() -> int`: remove all synchronized frames from the buffer and return the number of frames flushed.
- `get_pending_timestamps() -> List[int]`: return a sorted list of timestamps that have buffered data but are not yet synchronized.
- `get_sensor_count() -> int`: return the number of registered sensors.
- `get_buffer_size() -> int`: return the total number of unique timestamps currently in the buffer, synchronized or pending.
- `get_data_at(timestamp: int) -> List[str]`: return the data at a timestamp as a list of `'sensor_id:data'` strings sorted by sensor_id, or an empty list if there is no data for that timestamp.
Example 1 registers three sensors, completes one frame and leaves another pending:
```text
agg = SensorDataAggregator()
agg.register_sensor("lidar_1", "lidar") # -> True
agg.register_sensor("radar_1", "radar") # -> True
agg.register_sensor("cam_front", "camera") # -> True
agg.register_sensor("lidar_1", "camera") # -> False (ID already registered)
agg.register_sensor("sonar_1", "sonar") # -> False (invalid type)
agg.get_sensor_count() # -> 3
agg.add_data("lidar_1", 100, "pts_a") # -> True
agg.add_data("radar_1", 100, "obj_a") # -> True
agg.add_data("cam_front", 200, "img_b") # -> True
agg.get_pending_timestamps() # -> [100, 200]
agg.add_data("cam_front", 100, "img_a") # -> True, all 3 sensors reported: 100 is synchronized
agg.add_data("radar_1", 100, "obj_late") # -> False (100 is synchronized)
agg.add_data("gps_1", 200, "fix") # -> False (gps_1 is not registered)
agg.get_synchronized_frames() # -> [["cam_front:img_a", "lidar_1:pts_a", "radar_1:obj_a"]]
agg.get_pending_timestamps() # -> [200]
agg.get_buffer_size() # -> 2
agg.get_data_at(200) # -> ["cam_front:img_b"]
```
Example 2 completes frames out of timestamp order, then flushes them:
```text
agg = SensorDataAggregator()
agg.register_sensor("radar_b", "radar") # -> True
agg.register_sensor("radar_a", "radar") # -> True
agg.add_data("radar_a", 30, "r30a") # -> True
agg.add_data("radar_b", 10, "r10b") # -> True
agg.add_data("radar_a", 10, "r10a") # -> True, 10 is synchronized
agg.add_data("radar_b", 30, "r30b") # -> True, 30 is synchronized
agg.add_data("radar_a", 20, "r20a") # -> True
agg.get_synchronized_frames() # -> [["radar_a:r10a", "radar_b:r10b"], ["radar_a:r30a", "radar_b:r30b"]]
agg.get_buffer_size() # -> 3
agg.flush_synchronized() # -> 2
agg.get_buffer_size() # -> 1
agg.get_pending_timestamps() # -> [20]
agg.get_synchronized_frames() # -> []
agg.get_data_at(10) # -> [] (flushed)
agg.flush_synchronized() # -> 0
```
```hint Detecting a complete frame
Sensors are never removed, and only registered sensors can add data. Think about which two numbers you can compare to know a timestamp is complete without scanning the sensor list.
```
```hint Reads that write
If your buffer creates an entry on lookup (a `defaultdict`, for example), check what `get_data_at` on an unknown timestamp does to `get_buffer_size` and `get_pending_timestamps`.
```
### Constraints & Assumptions
- `1 <= number of sensors <= 100`
- Sensor types are one of `'lidar'`, `'radar'`, `'camera'`.
- Timestamps are non-negative integers (`0 <= timestamp <= 10⁶`).
- Sensor IDs and data values are non-empty strings.
- At most `10⁴` total operations.
### Clarifying Questions to Ask
- If a sensor sends a second reading for a timestamp that is still pending, should it replace the first one, be ignored, or be rejected?
- After `flush_synchronized`, may a late reading for a flushed timestamp open a new pending entry, or should that timestamp stay closed?
- If a new sensor registers while some timestamps are pending, must those timestamps wait for it too?
- Is there a limit on how long a timestamp can stay pending, or can a sensor that stops reporting keep data in the buffer forever?
### What a Strong Answer Covers
- The structures for sensors, the per-timestamp buffer and the synchronized timestamps, and the cost of each lookup.
- How `add_data` validates the sensor, rejects synchronized timestamps, and detects that a frame is complete.
- Deterministic output: timestamps in numeric order and entries ordered by sensor_id, which can differ from sorting the formatted strings.
- What `flush_synchronized` removes, and how buffer size, pending timestamps and synchronized frames stay consistent after it.
- The complexity of each method and explicit choices for the open cases (repeat readings, late sensors, data after a flush).
### Follow-up Questions
- A sensor fails and never reports again. How would you keep its missing readings from growing the buffer without bound?
- Real sensors do not share a perfect clock. How would you group readings whose timestamps differ by a few milliseconds into one frame?
- How would the design change if each sensor writes from its own thread?
- How would you push each frame to the perception module the moment it completes?
Overview: Design a class that buffers timestamped LiDAR, radar and camera readings and emits a synchronized frame once every registered sensor has reported. It tests hash map and set bookkeeping, deterministic sorted output, flush semantics, and the edge cases around late or repeated readings.