PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates understanding of kinematics, motion modeling, and numerical reasoning for safety-critical control logic, focusing on stopping-distance computation and the impact of reaction time on braking decisions.

  • Medium
  • Tesla
  • Coding & Algorithms
  • Machine Learning Engineer

Implement automatic braking logic in Python

Company: Tesla

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Using Python, implement a simple automatic braking function. Given current speed v (m/s), distance to obstacle d (m), maximum deceleration a_max (m/s^ 2), and reaction time t_r (s), decide whether to brake immediately to stop before the obstacle; return the braking decision, braking start time, and expected stopping distance.

Quick Answer: This question evaluates understanding of kinematics, motion modeling, and numerical reasoning for safety-critical control logic, focusing on stopping-distance computation and the impact of reaction time on braking decisions.

You are building a simplified automatic emergency braking module. A car is moving at speed v meters per second, and an obstacle is d meters ahead. Once braking begins, the car can decelerate at a constant maximum rate a_max meters per second squared. If the car does not brake immediately, it continues traveling at speed v for t_r seconds of reaction delay before braking starts. Assume: - Speed stays constant during the reaction delay. - Deceleration is constant after braking starts. - The braking distance from speed v is v^2 / (2 * a_max). Return a tuple (decision, start_time, stopping_distance) where: - decision is 'BRAKE_NOW' if waiting t_r seconds would make stopping in time impossible; otherwise 'WAIT' - start_time is 0.0 if braking should start immediately, otherwise t_r - stopping_distance is the total distance traveled from now until the car fully stops under the chosen action If the car cannot stop before the obstacle even with immediate braking, still return 'BRAKE_NOW' because that is the safest action.

Constraints

  • 0 <= v <= 10^4
  • 0 <= d <= 10^7
  • a_max > 0
  • 0 <= t_r <= 10^3

Examples

Input: (20, 100, 5, 1)

Expected Output: ('WAIT', 1.0, 60.0)

Explanation: Braking distance is 20^2 / (2*5) = 40. During the 1-second delay, the car travels 20 more meters. Total stopping distance is 60, which is within 100, so waiting is safe.

Input: (20, 50, 5, 1)

Expected Output: ('BRAKE_NOW', 0.0, 40.0)

Explanation: Immediate braking needs 40 meters, but waiting 1 second would require 60 meters total. Since 60 > 50, the car must brake now.

Input: (20, 30, 5, 1)

Expected Output: ('BRAKE_NOW', 0.0, 40.0)

Explanation: Even immediate braking needs 40 meters, which is more than the 30-meter gap. The safest decision is still to brake now.

Input: (12, 30, 6, 1.5)

Expected Output: ('WAIT', 1.5, 30.0)

Explanation: Braking distance is 12^2 / (2*6) = 12. Reaction distance is 12 * 1.5 = 18. Total stopping distance is exactly 30, so waiting is still safe.

Input: (10, 10, 5, 1)

Expected Output: ('BRAKE_NOW', 0.0, 10.0)

Explanation: Immediate braking distance is exactly 10 meters, but waiting 1 second would make the total stopping distance 20 meters. So braking must start immediately.

Solution

def solution(v, d, a_max, t_r):
    if a_max <= 0:
        raise ValueError('a_max must be positive')

    braking_distance = (v * v) / (2.0 * a_max)
    delayed_stopping_distance = (v * t_r) + braking_distance

    if delayed_stopping_distance > d:
        return ('BRAKE_NOW', 0.0, braking_distance)
    else:
        return ('WAIT', float(t_r), delayed_stopping_distance)

Time complexity: O(1). Space complexity: O(1).

Hints

  1. First compute the braking distance if the car starts braking right now: v^2 / (2 * a_max).
  2. Then compare the total stopping distance after waiting t_r seconds, v * t_r + braking_distance, against d.
Last updated: Apr 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Write SQL Data Transformation Queries - Tesla (medium)
  • Implement a Rollback Key-Value Store - Tesla (hard)
  • Compute suffix sums over waypoints - Tesla (hard)
  • Compute time to burn tree - Tesla (medium)
  • Coordinate workers across two exclusive targets - Tesla (hard)