KatInterview

Validated Config-Driven LiDAR Scan Processor

Arrays & HashingAuto-graded · 14 test cases
MediumAuto-gradedArrays & Hashingsimulationvalidationmatrix-transform

Last updated

Validate a declarative list of crop and rigid-transform steps up front, then apply them in order to a point list while tracking metadata, without mutating the inputs.

Validated Config-Driven LiDAR Scan Processor

A perception stack cleans up raw LiDAR scans with an ordered list of steps described as plain data. Implement a function that checks the whole description first, then runs it on a scan.

Input

  • points: a list of points. Each point is a list of numbers [x, y, z, ...] with at least three values. Values after the first three are extra channels (intensity, ring ID, and so on). All points must have the same length.
  • metadata: a dictionary describing the scan. It may contain keys such as "frame_id" and "applied_transforms" (a list of earlier step records), plus any other keys.
  • config: a list of step descriptions. Each one is a dictionary with exactly the keys "name" and "params".

Supported steps

CropBoundingBox, with params containing exactly min_bound and max_bound.

  • Each bound is a list of exactly 3 finite numbers (not booleans).
  • For every axis, min_bound[i] <= max_bound[i] must hold (equality is allowed).
  • Keep a point only if min_bound[i] <= point[i] <= max_bound[i] for i = 0, 1, 2 (inclusive on both ends). Keep the surviving points in their original order, with all their channels.
  • Append {"name": "CropBoundingBox", "remaining_points": <number of points kept>} to the applied_transforms list.

RigidTransform, with params containing exactly target_frame and matrix.

  • target_frame is a non-empty string.
  • matrix is a list of 4 rows, each a list of 4 finite numbers (not booleans). It is a 4x4 homogeneous matrix M. Do not check that it is a true rigid motion.
  • For each point with coordinates (x, y, z), the new coordinates are the first three entries of M · [x, y, z, 1]ᵀ. That is, new coordinate i is M[i][0]*x + M[i][1]*y + M[i][2]*z + M[i][3]*1.0. The fourth row of M is ignored. Channels after the first three stay unchanged.
  • Set metadata["frame_id"] to target_frame, even if the key did not exist before.
  • Append {"name": "RigidTransform"} to the applied_transforms list.

Validation (all failures raise ValueError)

Validate everything before running any step, so a bad config never produces partial output.

  • points must be a list, metadata a dict and config a list.
  • Every point must be a list with at least 3 entries. All points must have the same length. Every entry must be a finite int or float (booleans, NaN and infinities are rejected). An empty points list is valid.
  • Every config item must be a dict whose keys are exactly {"name", "params"}. name must be a string and params a dict.
  • An unknown step name is an error.
  • A step's params must have exactly the keys listed for it above, with the value rules given above.

Output

Return a dictionary {"points": ..., "metadata": ...}:

  • points is the processed list of points.
  • metadata has all the keys of the input metadata (with frame_id possibly replaced), and applied_transforms is the original list (or an empty list if absent) followed by the records from this run, in order.
  • The inputs must not be modified.

Constraints

  • 0 <= len(points); each point has length D >= 3.
  • An empty scan runs cleanly through every step. Crop records remaining_points: 0, and the rigid transform still updates frame_id.
  • An empty config list is valid. The output is then a copy of the input scan, with applied_transforms copied (or [] if it was absent).

Examples

Example 1

Input: process_lidar_scan([[0, 0, 0, 0.8], [20, 0, 0, 0.5], [-5, 5, 1, 0.2]], {'frame_id': 'lidar_top'}, [{'name': 'CropBoundingBox', 'params': {'min_bound': [-10, -10, -2], 'max_bound': [10, 10, 2]}}, {'name': 'RigidTransform', 'params': {'target_frame': 'vehicle', 'matrix': [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]}}])

Output: {'points': [[0, 0, 0, 0.8], [-5, 5, 1, 0.2]], 'metadata': {'frame_id': 'vehicle', 'applied_transforms': [{'name': 'CropBoundingBox', 'remaining_points': 2}, {'name': 'RigidTransform'}]}}

Example 2

Input: process_lidar_scan([], {'frame_id': 'lidar_top'}, [{'name': 'CropBoundingBox', 'params': {'min_bound': [-1, -1, -1], 'max_bound': [1, 1, 1]}}, {'name': 'RigidTransform', 'params': {'target_frame': 'vehicle', 'matrix': [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]}}])

Output: {'points': [], 'metadata': {'frame_id': 'vehicle', 'applied_transforms': [{'name': 'CropBoundingBox', 'remaining_points': 0}, {'name': 'RigidTransform'}]}}

Hints

Hint 1

Do all validation in a first pass that builds a list of parsed steps, and only then touch the points. This guarantees an invalid config raises before any work is done.

Hint 2

Copy the points rows and the metadata (including a fresh applied_transforms list) before processing so the caller's data is never mutated.

Hint 3

For the rigid step, only rows 0 to 2 of the 4x4 matrix matter. Compute each new coordinate as a dot product with (x, y, z, 1.0) and assign it back to the first three slots of the row, leaving extra channels alone.