Calculate Emergency Braking Distance and Decision
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Tests understanding of kinematic equations, motion physics, and numerical reasoning to determine whether a vehicle can stop in time given speed, obstacle distance, deceleration, and reaction delay.
You are building a collision avoidance system for a vehicle. Your task is to determine whether the vehicle can safely stop before reaching an obstacle.
Write a function that takes the following parameters:
v: current velocity in meters per second (m/s)d: distance to the obstacle in meters (m)a_max: maximum deceleration rate in meters per second squared (m/s²)t_r: reaction time in seconds (s) — the delay before brakes engage
During the reaction time, the vehicle continues at constant velocity v. After the reaction time elapses, the vehicle decelerates uniformly at rate a_max until it comes to a complete stop.
Your function must return a dictionary with three keys:
"should_brake": a boolean indicating whether the vehicle can stop before reaching the obstacle (stopping distance ≤ obstacle distance)"braking_start_time": the reaction timet_rif braking is possible, otherwiseNone"stopping_distance": the total distance traveled from now until the vehicle stops (includes both reaction distance and braking distance)
The stopping distance consists of two components:
- Reaction distance: distance traveled at constant velocity during reaction time =
v × t_r - Braking distance: distance traveled while decelerating to a stop =
v² / (2 × a_max)
Total stopping distance = reaction distance + braking distance
Constraints
0 ≤ v ≤ 100(velocity in m/s)0 ≤ d ≤ 1000(distance in meters)0 < a_max ≤ 20(deceleration in m/s²)0 ≤ t_r ≤ 5(reaction time in seconds)- All inputs are valid numbers
- Return
float('inf')for stopping distance ifa_maxis 0 or negative
Examples
Example 1
Input: calculate_braking(20, 100, 8, 1)
Output: {'should_brake': True, 'braking_start_time': 1, 'stopping_distance': 45}
Example 2
Input: calculate_braking(20, 40, 8, 1)
Output: {'should_brake': False, 'braking_start_time': None, 'stopping_distance': 45}
Example 3
Input: calculate_braking(10, 30, 5, 0.5)
Output: {'should_brake': True, 'braking_start_time': 0.5, 'stopping_distance': 15}
Hints
Hint 1
The vehicle travels in two phases: constant velocity during reaction time, then uniform deceleration until stopped.
Hint 2
Use the kinematic equation for constant acceleration: the distance to stop from velocity v with deceleration a is v²/(2a).
Hint 3
Compare the total stopping distance (reaction distance plus braking distance) against the obstacle distance to decide if braking will succeed.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires modeling vehicle motion in two distinct phases:
-
Reaction phase: During the reaction time
t_r, the vehicle continues moving at its current velocityv. The distance covered is simplyv × t_r. -
Braking phase: After the reaction time, the vehicle decelerates uniformly at rate
a_maxuntil it stops. Using kinematic equations for constant acceleration, when final velocity is zero, the braking distance isv² / (2 × a_max).
The total stopping distance is the sum of these two components. We compare this against the obstacle distance d to determine if the vehicle can stop safely.
If the stopping distance is less than or equal to the obstacle distance, braking will succeed and we return t_r as the braking start time. Otherwise, braking cannot prevent a collision, so we return None for the braking start time.
Why This Works
The kinematic equation v_final² = v_initial² - 2 × a × distance gives us the relationship between velocity, acceleration, and distance. When the final velocity is zero (vehicle stopped), we can solve for distance: 0 = v² - 2 × a_max × distance, which gives distance = v² / (2 × a_max).
This formula assumes constant deceleration, which matches the problem's specification. The reaction distance is straightforward: distance equals velocity times time when velocity is constant.
Complexity
- Time complexity: O(1) — the function performs a constant number of arithmetic operations regardless of input values.
- Space complexity: O(1) — only a fixed-size dictionary is allocated.
Implementation
from typing import Dict
def calculate_braking(v: float, d: float, a_max: float, t_r: float) -> Dict[str, object]:
stopping_distance = v * t_r + (v * v) / (2 * a_max) if a_max > 0 else float('inf')
should_brake = stopping_distance <= d
return {
"should_brake": should_brake,
"braking_start_time": t_r if should_brake else None,
"stopping_distance": stopping_distance,
}