Count Fleets Reaching a Common Destination

Quick Overview

Count Fleets Reaching a Common Destination. Cars start at distinct positions below target and move toward it at their positive speeds.

Count Fleets Reaching a Common Destination

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Cars start at distinct positions below target and move toward it at their positive speeds. A car cannot pass another; after catching a car or fleet it travels with that slower fleet. Catching exactly at target counts as joining. Return how many fleets arrive. ```cpp class Solution { public: int carFleet(int target, vector<int>& position, vector<int>& speed); }; ``` ### Examples **Example 1:** ```text target = 10, position = [0,5], speed = [2,1] Output: 1 Explanation: Both reach target at time5, so they form one fleet there. ``` **Example 2:** ```text target = 10, position = [0,5], speed = [1,1] Output: 2 Explanation: Equal speeds preserve the gap, and the cars never meet. ``` ### Constraints - Both arrays have the same n in [1,100000]. - Target is in [1,1000000]. - Positions are distinct integers in [0,target-1]. - Speeds are integers in [1,1000000]. ### Contract considerations Preserve no-passing and meeting-at-target semantics. ```hint Read the boundary condition When can a trailing car reach the destination no later than the fleet ahead? ``` ### Boundary reminders Preserve no-passing and meeting-at-target semantics.

Overview: Count Fleets Reaching a Common Destination. Cars start at distinct positions below target and move toward it at their positive speeds.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Cars start at distinct positions below target and move toward it at their positive speeds. A car cannot pass another; after catching a car or fleet it travels with that slower fleet. Catching exactly at target counts as joining. Return how many fleets arrive.

class Solution {
public:
    int carFleet(int target, vector<int>& position, vector<int>& speed);
};

Examples

Example 1:

target = 10, position = [0,5], speed = [2,1]
Output: 1
Explanation: Both reach target at time5, so they form one fleet there.

Example 2:

target = 10, position = [0,5], speed = [1,1]
Output: 2
Explanation: Equal speeds preserve the gap, and the cars never meet.

Constraints

  • Both arrays have the same n in [1,100000].
  • Target is in [1,1000000].
  • Positions are distinct integers in [0,target-1].
  • Speeds are integers in [1,1000000].

Contract considerations

Preserve no-passing and meeting-at-target semantics.

Boundary reminders

Preserve no-passing and meeting-at-target semantics.