Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Car fleets

    Car fleets

    Mediumstacksortingmonotonic stack

    Cars head to the same target on a one-lane road. Car i starts at position[i] with speed speed[i]. No passing: a car that catches the one ahead slows to its speed and they form one fleet. Return how many fleets arrive.

    Example 1

    Input:  target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]
    Output: 3

    Example 2

    Input:  target = 10, position = [3], speed = [3]
    Output: 1

    Constraints

    • 1 ≤ n ≤ 10⁵
    • 0 < target ≤ 10⁶
    • 0 ≤ position[i] < target, all distinct
    • 1 ≤ speed[i] ≤ 10⁶

    Submit checks your solution against 7 tests, including hidden ones.

    /**
     * @param {int} target
     * @param {int[]} position
     * @param {int[]} speed
     * @return {int}
     */
    function carFleet(target, position, speed) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.