KatInterview

Earliest Disc Overlap Among Unicycle-Driven Agents

Math & Bit ManipulationAuto-graded · 17 test cases
MediumAuto-gradedMath & Bit Manipulationsimulationgeometryfloating-point

Last updated

Simulate several disc-shaped agents with forward Euler unicycle motion and report the first time step at which any pair overlaps, with all pairs at that step.

Overlapping Robots on a Plane

A fleet of circular robots moves on a flat 2D surface. Each robot is described by an id, a center position (x, y), a heading theta in radians (counter-clockwise from the positive x-axis), and a positive radius.

Every robot follows a unicycle motion model. At each time step k (for k = 0, 1, ..., steps - 1), robot i is given a command (v, omega): a forward speed v and a turn rate omega. The state is advanced by one forward Euler step of length dt:

x_next     = x     + v * cos(theta) * dt
y_next     = y     + v * sin(theta) * dt
theta_next = theta + omega * dt

The position update uses the heading from before the update. Robots do not influence each other's motion.

Overlap rule

Two different robots overlap at a given moment if the distance between their centers is less than or equal to the sum of their radii. Touching exactly counts as an overlap.

The state is inspected at the initial moment (step 0) and again after each update (steps 1, 2, ..., steps).

Task

Write a function that simulates the fleet and finds the earliest step at which at least one pair of robots overlaps.

Return a two-element list [first_step, pairs] where:

  • first_step is that earliest step index.
  • pairs is a list of all pairs of robot ids that overlap at that step, each pair given as [smaller_id, larger_id], with the list sorted in ascending lexicographic order.

Pairs that only start overlapping at a later step are not included. Return None if no overlap occurs at any step from 0 to steps inclusive.

Notes:

  • Ids are not necessarily in increasing order in the input list, so order each pair by id value, not by list position.
  • If the robots already overlap at step 0, return step 0 without applying any commands.
  • If steps = 0, only the initial state is checked.
  • controls[i][k] holds the (v, omega) command for the i-th robot in agents at step k.
  • To match the expected results exactly, compare squared distance to squared radius sum, and compute the updates in the order given above.

Constraints

  • There are at least 2 robots.
  • steps >= 0, and each controls[i] has length exactly steps.
  • dt > 0.
  • Every radius is strictly positive.
  • Coordinates, speeds and turn rates are finite floats (they may be negative).
  • Robot ids are distinct integers.

Examples

Example 1

Input: find_first_collision([{'id': 0, 'x': 0, 'y': 0, 'theta': 0, 'radius': 1}, {'id': 1, 'x': 5, 'y': 0, 'theta': 3.141592653589793, 'radius': 1}], [[[2, 0], [2, 0]], [[2, 0], [2, 0]]], 1, 2)

Output: [1, [[0, 1]]]

Example 2

Input: find_first_collision([{'id': 10, 'x': 0, 'y': 0, 'theta': 0, 'radius': 0.5}, {'id': 20, 'x': 10, 'y': 10, 'theta': 1.57, 'radius': 0.5}], [[[1, 0]], [[1, 0]]], 0.5, 1)

Output: None

Hints

Hint 1

Process steps in increasing order and stop at the first step where any pair overlaps; no earlier step can be wrong then.

Hint 2

Check for overlaps before applying the update for that step. This covers step 0 and the final state without ever reading a command at index steps.

Hint 3

Compare dx*dx + dy*dy against (r_i + r_j)**2 using <= to avoid sqrt and to count exact contact.

Hint 4

Build each pair as (min(id_a, id_b), max(id_a, id_b)) using ids rather than list indices, then sort the collected pairs before returning.