Determine earliest collision among moving cars
Company: Waymo
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in kinematics, computational geometry, numerical root-finding, and scalable algorithm design for collision detection among moving objects, including attention to numerical precision and overlapping trajectories.
Part 1: Earliest collision on a straight road
Constraints
- 1 <= n <= 200000
- -10^6 <= x, v, a <= 10^6
- 0 <= r <= 10^6
- Inputs may be integers or floats
- Cars move independently; you only need the first collision time, not post-collision dynamics
Examples
Input: [(0, 10, 0, 1), (10, 0, 0, 1)]
Expected Output: (0.8, (0, 1))
Explanation: The center distance is 10 - 10t. Collision starts when it becomes 2, so t = 0.8.
Input: [(0, 0, 0, 2), (3, 0, 0, 2), (10, 0, 0, 1)]
Expected Output: (0.0, (0, 1))
Explanation: Cars 0 and 1 already overlap at t = 0 because their center distance is 3 and their radii sum is 4.
Input: [(0, 0, 2, 1), (10, 0, -2, 1)]
Expected Output: (2.0, (0, 1))
Explanation: Their center distance is 10 - 2t^2. Collision starts when it becomes 2, so t = 2.
Input: [(0, 0, 0, 1), (10, 0, 1, 1)]
Expected Output: None
Explanation: The second car accelerates away, so the gap never closes.
Input: [(5, 1, 0, 1)]
Expected Output: None
Explanation: A single car cannot collide with anything.
Hints
- If there is no overlap at t = 0, the left-to-right order of car centers cannot change before the first collision. That means only initially adjacent cars can create the first collision.
- For two adjacent cars, write the gap between their occupied intervals as a quadratic in t and solve for the earliest time when the gap becomes <= 0.
Part 2: Earliest collision on a plane
Constraints
- 1 <= n <= 250
- -10^4 <= x, y, vx, vy, ax, ay <= 10^4
- 0 <= r <= 10^4
- Inputs may be integers or floats
- An O(n^2) pair check is acceptable for this version
Examples
Input: [(0, 0, 1, 0, 0, 0, 1), (5, 0, 0, 0, 0, 0, 1)]
Expected Output: (3.0, (0, 1))
Explanation: The moving car reaches center distance 2 from the stationary car at t = 3.
Input: [(0, 0, 0, 0, 0, 0, 2), (3, 0, 0, 0, 0, 0, 2)]
Expected Output: (0.0, (0, 1))
Explanation: The cars already overlap at t = 0 because the distance is 3 and the radii sum is 4.
Input: [(0, 0, 0, 0, 1, 0, 1), (10, 0, 0, 0, -1, 0, 1)]
Expected Output: (2.828427, (0, 1))
Explanation: The x-distance is 10 - t^2, so collision starts when it becomes 2, giving t = sqrt(8).
Input: [(0, 0, 0, 0, 0, 0, 1), (4, 2, -2, 0, 0, 0, 1)]
Expected Output: (2.0, (0, 1))
Explanation: This is a tangential collision: the moving car just touches the stationary car at t = 2.
Input: [(0, 0, 1, 0, 0, 0, 1), (0, 10, 1, 0, 0, 0, 1)]
Expected Output: None
Explanation: Their relative position stays constant at vertical distance 10, so they never meet.
Hints
- For one pair of cars, the squared distance minus (r1 + r2)^2 is a quartic function of time. You do not need a direct quartic formula if you can reason about monotone intervals.
- Find the real critical points of the distance function by solving its derivative cubic, then binary-search the earliest root on the first interval where the function can cross zero.