Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free

Code Analysis and Bug Finding

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Whenever you're ready, walk me through how you'd approach this.

Sign up free to chat or submit. Log in

Problem

Oxa builds autonomous-driving software, and this function is part of a module that turns a vehicle's recent speed samples into a braking-distance report for the safety monitor.

fn braking_distance_report(speeds_mps: &Vec<i32>, deceleration: i32) -> Vec<i32> {
    let mut distances = Vec::new();

    for i in 0..speeds_mps.len() + 1 {
        let speed = speeds_mps[i];
        let delta = speeds_mps[i] - speeds_mps[i - 1];
        let distance = (speed * speed) / (2 * deceleration);
        distances.push(distance + delta);
    }

    let peak = speeds_mps.iter().max().unwrap();
    let average = speeds_mps.iter().sum::<i32>() / speeds_mps.len() as i32;

    println!("peak: {}, average: {}", peak, average);
    distances
}

Analyze this function and tell me some bugs that you see, and explain what the code is trying to do. Be specific about which line panics or produces a wrong result, and under what input.