Detect earliest collision among moving cars
Company: Meta
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: Evaluates continuous kinematics and collision-detection concepts—solving quadratic and linear motion intersections, numerical robustness, and algorithmic time-complexity trade-offs—within the Coding & Algorithms domain for a Data Scientist role at a medium-to-advanced abstraction level.
Part 1: Earliest Collision Time on a Line with Constant Acceleration
Constraints
- 0 <= n <= 2000
- len(x0) == len(v0) == len(a)
- -1e6 <= x0[i], v0[i], a[i] <= 1e6
- Continuous time with t >= 0
- Use a floating-point tolerance of about 1e-9
Examples
Input: ([0.0, 10.0], [2.0, -3.0], [0.0, 0.0])
Expected Output: (2.0, (0, 1))
Explanation: The positions are x0(t) = 2t and x1(t) = 10 - 3t. They meet at t = 2.
Input: ([0.0, 4.0, 100.0], [0.0, 0.0, 0.0], [2.0, 0.0, 0.0])
Expected Output: (2.0, (0, 1))
Explanation: Vehicle 0 follows x(t) = t^2, so it reaches x = 4 at t = 2 and collides with vehicle 1 first.
Input: ([5.0, 5.0, 7.0], [1.0, -2.0, 0.0], [0.0, 1.0, 0.0])
Expected Output: (0.0, (0, 1))
Explanation: Vehicles 0 and 1 already occupy the same position at t = 0.
Input: ([0.0, 5.0], [1.0, 2.0], [0.0, 0.0])
Expected Output: None
Explanation: The second vehicle starts ahead and is also faster, so the gap never closes.
Input: ([3.0], [1.0], [0.0])
Expected Output: None
Explanation: With fewer than two vehicles, no collision pair exists.
Hints
- For a fixed pair (i, j), subtract the two motion equations. You get at most a quadratic equation in t.
- Be careful with degenerate cases: the quadratic term may disappear, and sometimes two vehicles share the exact same trajectory, which means the earliest collision is t = 0.
Part 2: Earliest Intersection Time for Moving Disks in 2D
Constraints
- 0 <= n <= 2000
- len(p0) == len(v) == len(r)
- -1e6 <= coordinates and velocity components <= 1e6
- 0 <= r[i] <= 1e6
- Continuous time with t >= 0
- Use a floating-point tolerance of about 1e-9
Examples
Input: ([(0.0, 0.0), (10.0, 0.0)], [(1.0, 0.0), (-1.0, 0.0)], [1.0, 1.0])
Expected Output: (4.0, (0, 1))
Explanation: The centers start 10 units apart and close at relative speed 2. They first touch when the center distance becomes 2, which happens at t = 4.
Input: ([(0.0, 0.0), (1.0, 0.0), (10.0, 0.0)], [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0)], [1.0, 1.0, 1.0])
Expected Output: (0.0, (0, 1))
Explanation: Disks 0 and 1 already overlap at time 0 because their centers are 1 unit apart and their radii sum to 2.
Input: ([(0.0, 0.0), (0.0, 5.0)], [(1.0, 0.0), (1.0, 0.0)], [1.0, 1.0])
Expected Output: None
Explanation: The relative velocity is zero, so the distance between the disks stays 5, which is greater than 2.
Input: ([(0.0, 0.0), (3.0, 0.0)], [(1.0, 0.0), (0.0, 0.0)], [0.0, 0.0])
Expected Output: (3.0, (0, 1))
Explanation: These are moving points. The first point reaches x = 3 at t = 3 and meets the second point.
Input: ([(2.0, 2.0)], [(0.0, -1.0)], [0.5])
Expected Output: None
Explanation: With only one disk, there is no pair to test.
Hints
- For one pair of disks, switch to relative motion: treat one disk as stationary and let the other move with the relative velocity.
- After squaring the distance condition, you get a quadratic inequality A*t^2 + B*t + C <= 0. Already-overlapping disks correspond to C <= 0 at t = 0.