You are given n cars moving over time. Each car has known initial state at time t=0:
-
1D case (straight road):
-
initial position
xi
(meters)
-
initial velocity
vi
(m/s)
-
constant acceleration
ai
(m/s²)
-
physical size represented as a radius
ri
(meters). (Cars collide when their intervals overlap, i.e., when center distance
≤ri+rj
.)
-
2D case (flat plane):
-
initial position
(xi,yi)
-
initial velocity
(vxi,vyi)
-
constant acceleration
(axi,ayi)
-
radius
ri
A car’s position evolves as:
-
1D:
xi(t)=xi+vit+21ait2
-
2D:
pi(t)=pi+vit+21ait2
Tasks
-
1D:
Determine whether
any collision occurs
for
t≥0
. If yes, return the
earliest collision time
and the pair of cars involved.
-
2D:
Same as (1), but in 2D (cars collide when Euclidean distance between centers
≤ri+rj
).
Requirements
-
Provide an algorithm and implementable approach.
-
Discuss time complexity and how you would
optimize
beyond the naive
O(n2)
pairwise checking.
-
Handle edge cases such as:
-
collisions at
t=0
-
cars with identical trajectories (overlapping for an interval)
-
numerical precision when solving for collision times
You may assume n can be large (e.g., up to 105), so a brute-force all-pairs solution may be too slow.