← Questions

Automatic emergency braking logic in Python

0
TeslaCodingLast asked Sep 2025

A coding interview question asked in Tesla's technical phone screen, last asked Sep 2025. It tests control, python and simulation.

Write a simple automatic emergency braking function in Python. A car moves at speed v (m/s) toward an obstacle d metres ahead. Once braking starts, it slows at a constant a_max (m/s²), so it needs v² / (2·a_max) metres to stop. If it does not brake now, it keeps going at v for a reaction delay of t_r seconds before braking begins. Decide whether the car must brake now.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • brake_decision(v: float, d: float, a_max: float, t_r: float) -> tuple[str, float, float] — returns (decision, start_time, stopping_distance):
    • decision is "BRAKE_NOW" if waiting t_r seconds would leave too little room to stop before the obstacle, else "WAIT".
    • start_time is 0.0 when braking now, else t_r.
    • stopping_distance is the total distance travelled from now until the car stops under that decision.
    • If the car cannot stop in time even when braking now, still return "BRAKE_NOW": it is the safest action.

Example

brake_decision(20, 100, 5, 1)  ->  ("WAIT", 1.0, 60.0)        # 20 m during the delay + 40 m braking = 60 <= 100
brake_decision(20, 50, 5, 1)   ->  ("BRAKE_NOW", 0.0, 40.0)   # waiting needs 60 > 50
brake_decision(30, 50, 5, 1)   ->  ("BRAKE_NOW", 0.0, 90.0)   # cannot stop in time at all; brake anyway

Constraints

  • 0 <= v <= 10^4, 0 <= d <= 10^7, a_max > 0, 0 <= t_r <= 10^3.
  • Stopping exactly at the obstacle (distance == d) counts as stopping in time.

Clarifying questions

  • Does stopping exactly at the obstacle count as safe, or is a safety margin required?
  • Is the obstacle still, or moving?
  • Should the function also return the speed at impact when the car cannot stop?
  • What should happen with invalid input (a_max <= 0, negative values)?

What a strong answer covers

  • The kinematics: braking distance v² / (2·a_max), delay distance v·t_r, and the comparison with d.
  • Clear handling of the boundary case and of the "cannot stop anyway" case.
  • Floating-point care near the boundary (compare with a small tolerance or document the rule).
  • Input validation.
  • Unit tests for each branch, v = 0, t_r = 0 and the exact boundary.

Follow-ups

  • The obstacle is a car ahead moving at its own speed and braking. Use relative motion.
  • Return the minimum deceleration needed instead of a yes/no decision.
  • Run it every 50 ms with noisy distance readings. How do you avoid flickering between WAIT and BRAKE_NOW?

Where it's asked

  • Tesla · Technical phone screen · Last asked Sep 202545 min · 10 to 20 minutes on your background, then one compact coding, debugging or design task in CoderPad that must run

What it tests

control · python · simulation

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding