Katkat

Lane-Change Safety and Longitudinal Motion Simulation

ZooxSoftware Engineer · Technical Screen

Last updated

Implement a discrete-time kinematic simulator to evaluate whether an autonomous vehicle can safely execute a lane change given adjacent lead and lag vehicles.

Problem Statement

In autonomous driving, evaluating whether a lane change is safe requires projecting vehicle trajectories over a time horizon and ensuring required safety margins are never violated.

You are tasked with implementing a forward-simulation safety checker for a lane-change maneuver.

Each vehicle has a 1D longitudinal position s (front bumper position in meters), speed v (m/s, non-negative), constant acceleration a (m/s^2), and physical length length (meters).

A lane change is considered safe over a time horizon T with discrete step size dt if and only if, at every time step t = 0, dt, 2*dt, ..., T:

  1. If a target lead vehicle exists, the front bumper of ego must remain behind the rear bumper of the lead vehicle by at least the required clearance: gap_lead = s_lead - length_lead - s_ego >= d_min + v_ego * t_headway
  2. If a target lag vehicle exists, the front bumper of the lag vehicle must remain behind the rear bumper of ego by at least the required clearance: gap_lag = s_ego - length_ego - s_lag >= d_min + v_lag * t_headway

Kinematic Update Rule

For each time step of duration dt, update each vehicle's state as follows:

  • If v + a * dt <= 0 (vehicle comes to a complete stop during the step):
    • Distance traveled: ds = -v^2 / (2 * a) (when a < 0)
    • New position: s = s + ds
    • New speed: v = 0.0
    • New acceleration: a = 0.0
  • Otherwise:
    • New position: s = s + v * dt + 0.5 * a * dt^2
    • New speed: v = v + a * dt

Return True if the lane change satisfies safety criteria at all steps up to and including T, and False otherwise.

Function Signature

from typing import Optional
from dataclasses import dataclass

@dataclass
class Vehicle:
    s: float       # Front bumper position (m)
    v: float       # Velocity (m/s, >= 0)
    a: float       # Acceleration (m/s^2)
    length: float  # Length (m, > 0)

def is_lane_change_safe(
    ego: Vehicle,
    target_lead: Optional[Vehicle],
    target_lag: Optional[Vehicle],
    horizon: float,
    dt: float,
    d_min: float,
    t_headway: float
) -> bool:
    pass

Constraints

  • 0.0 = 0.0 meters
  • t_headway >= 0.0 seconds
  • Initial velocities v >= 0.0
  • Vehicle lengths length > 0.0

Examples

Example 1

ego = Vehicle(s=50.0, v=20.0, a=0.0, length=4.5)
lead = Vehicle(s=80.0, v=20.0, a=0.0, length=4.5)
lag = Vehicle(s=20.0, v=20.0, a=0.0, length=4.5)

# horizon = 2.0, dt = 1.0, d_min = 2.0, t_headway = 1.0
is_lane_change_safe(ego, lead, lag, 2.0, 1.0, 2.0, 1.0)

Output: False Explanation: At t = 0, required headway for ego behind lead is 2.0 + 20.0 * 1.0 = 22.0 m. The actual gap is 80.0 - 4.5 - 50.0 = 25.5 >= 22.0 (safe). However, required headway for lag behind ego is 2.0 + 20.0 * 1.0 = 22.0 m. The actual gap is 50.0 - 4.5 - 20.0 = 25.5 >= 22.0 (safe). Over the horizon, all vehicles move at the same speed, maintaining 25.5 >= 22.0. Thus, this returns True.

Example 2

ego = Vehicle(s=50.0, v=20.0, a=-5.0, length=5.0)
lead = None
lag = Vehicle(s=30.0, v=25.0, a=0.0, length=5.0)

# horizon = 2.0, dt = 0.5, d_min = 3.0, t_headway = 1.0
is_lane_change_safe(ego, lead, lag, 2.0, 0.5, 3.0, 1.0)

Output: False Explanation: Ego is decelerating while lag approaches at constant 25 m/s. The gap between ego's rear bumper and lag's front bumper narrows rapidly and falls below d_min + v_lag * t_headway before horizon ends.

Hints

Hint 1

Perform the safety check at the current state before advancing the simulation for the next time step.

Hint 2

Pay special attention to vehicle stopping logic: when velocity reaches zero mid-step, the vehicle must not move in reverse.

Hint 3

Be careful with floating-point loop increments: using an integer step counter `k` where `num_steps = round(horizon / dt)` avoids precision drift.

Follow-up questions

  • How would you extend this check if the lag vehicle reacts to ego's lane change with a delayed braking response (e.g., reaction time tau = 0.4s)?
  • What changes if ego's lane change has a non-instantaneous lateral duration, requiring safety checks across both source and target lanes simultaneously?
  • How can you determine the safety analytically without discrete time stepping when accelerations are constant?