Decide whether a car must brake now from its speed, obstacle distance and maximum deceleration

Quick Overview

Decide whether a car's automatic braking system should start braking now, given the car's speed, the distance to an obstacle ahead and its maximum deceleration. Tests applying a basic kinematics formula, handling the boundary case where the car would stop exactly at the obstacle, and comparing values exactly.

Decide whether a car must brake now from its speed, obstacle distance and maximum deceleration

Company: Tesla

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A car's automatic braking system has three readings: the current `speed` of the car (in m/s), the `distance` to an obstacle ahead (in meters), and `max_deceleration`, the strongest deceleration the car can achieve (in m/s²). Determine whether the car should start braking immediately to avoid a collision. A car traveling at speed $v$ with maximum deceleration $a$ needs a minimum stopping distance of $\frac{v^2}{2a}$. If the stopping distance is greater than or equal to the distance to the obstacle, the car should brake. Return `True` if the car should brake and `False` otherwise. ### Function Signature ```python def should_brake(speed: int, distance: int, max_deceleration: int) -> bool: ``` ### Rules - The comparison is inclusive. A stopping distance exactly equal to `distance` means the car should brake. - The stopping distance is compared as an exact real number, with no tolerance. ### Constraints - `0 <= speed <= 200` - `1 <= distance <= 10000` - `1 <= max_deceleration <= 20` - `speed` is in m/s, `distance` is in meters, and `max_deceleration` is in m/s². ### Examples ```text Input: speed = 30, distance = 50, max_deceleration = 8 Output: True ``` The stopping distance is 900 / 16 = 56.25 meters. Since 56.25 is at least 50, the car should brake. ```text Input: speed = 10, distance = 100, max_deceleration = 5 Output: False ``` The stopping distance is 100 / 10 = 10 meters, which is less than 100, so the car does not need to brake yet. ```text Input: speed = 20, distance = 40, max_deceleration = 5 Output: True ``` The stopping distance is 400 / 10 = 40 meters, exactly the distance to the obstacle. Equality counts as a reason to brake.

Overview: Decide whether a car's automatic braking system should start braking now, given the car's speed, the distance to an obstacle ahead and its maximum deceleration. Tests applying a basic kinematics formula, handling the boundary case where the car would stop exactly at the obstacle, and comparing values exactly.

Tesla logo
Tesla
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A car's automatic braking system has three readings: the current speed of the car (in m/s), the distance to an obstacle ahead (in meters), and max_deceleration, the strongest deceleration the car can achieve (in m/s²). Determine whether the car should start braking immediately to avoid a collision.

A car traveling at speed vv with maximum deceleration aa needs a minimum stopping distance of v22a\frac{v^2}{2a}. If the stopping distance is greater than or equal to the distance to the obstacle, the car should brake.

Return True if the car should brake and False otherwise.

Function Signature

def should_brake(speed: int, distance: int, max_deceleration: int) -> bool:

Rules

  • The comparison is inclusive. A stopping distance exactly equal to distance means the car should brake.
  • The stopping distance is compared as an exact real number, with no tolerance.

Constraints

  • 0 <= speed <= 200
  • 1 <= distance <= 10000
  • 1 <= max_deceleration <= 20
  • speed is in m/s, distance is in meters, and max_deceleration is in m/s².

Examples

Input: speed = 30, distance = 50, max_deceleration = 8
Output: True

The stopping distance is 900 / 16 = 56.25 meters. Since 56.25 is at least 50, the car should brake.

Input: speed = 10, distance = 100, max_deceleration = 5
Output: False

The stopping distance is 100 / 10 = 10 meters, which is less than 100, so the car does not need to brake yet.

Input: speed = 20, distance = 40, max_deceleration = 5
Output: True

The stopping distance is 400 / 10 = 40 meters, exactly the distance to the obstacle. Equality counts as a reason to brake.