Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth passenger. All bus departure times are unique. All passenger arrival times are unique.
You are given an integer capacity, which represents the maximum number of passengers that can get on each bus.
When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.
More formally when a bus arrives, either:
capacity or fewer passengers are waiting for a bus, they will all get on the bus, orcapacity passengers with the earliest arrival times will get on the bus.Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger.
Note: The arrays buses and passengers are not necessarily sorted.
Example 1:
Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2 Output: 16 Explanation: Suppose you arrive at time 16. At time 10, the first bus departs with the 0th passenger. At time 20, the second bus departs with you and the 1st passenger. Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1st passenger to catch the bus.
Example 2:
Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2 Output: 20 Explanation: Suppose you arrive at time 20. At time 10, the first bus departs with the 3rd passenger. At time 20, the second bus departs with the 5th and 1st passengers. At time 30, the third bus departs with the 0th passenger and you. Notice if you had arrived any later, then the 6th passenger would have taken your seat on the third bus.
Constraints:
n == buses.lengthm == passengers.length1 <= n, m, capacity <= 1052 <= buses[i], passengers[i] <= 109buses is unique.passengers is unique.Problem summary: You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the ith bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the jth passenger. All bus departure times are unique. All passenger arrival times are unique. You are given an integer capacity, which represents the maximum number of passengers that can get on each bus. When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first. More formally when a bus arrives, either: If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or The capacity passengers with the earliest arrival times will get on
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search
[10,20] [2,17,18,19] 2
[20,30,10] [19,13,26,4,25,11,21] 2
minimum-speed-to-arrive-on-time)maximum-matching-of-players-with-trainers)time-taken-to-cross-the-door)time-to-cross-a-bridge)rearranging-fruits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2332: The Latest Time to Catch a Bus
class Solution {
public int latestTimeCatchTheBus(int[] buses, int[] passengers, int capacity) {
Arrays.sort(buses);
Arrays.sort(passengers);
int j = 0, c = 0;
for (int t : buses) {
c = capacity;
while (c > 0 && j < passengers.length && passengers[j] <= t) {
--c;
++j;
}
}
--j;
int ans = c > 0 ? buses[buses.length - 1] : passengers[j];
while (j >= 0 && ans == passengers[j]) {
--ans;
--j;
}
return ans;
}
}
// Accepted solution for LeetCode #2332: The Latest Time to Catch a Bus
func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
sort.Ints(buses)
sort.Ints(passengers)
j, c := 0, 0
for _, t := range buses {
c = capacity
for c > 0 && j < len(passengers) && passengers[j] <= t {
j++
c--
}
}
j--
ans := buses[len(buses)-1]
if c == 0 {
ans = passengers[j]
}
for j >= 0 && ans == passengers[j] {
ans--
j--
}
return ans
}
# Accepted solution for LeetCode #2332: The Latest Time to Catch a Bus
class Solution:
def latestTimeCatchTheBus(
self, buses: List[int], passengers: List[int], capacity: int
) -> int:
buses.sort()
passengers.sort()
j = 0
for t in buses:
c = capacity
while c and j < len(passengers) and passengers[j] <= t:
c, j = c - 1, j + 1
j -= 1
ans = buses[-1] if c else passengers[j]
while ~j and passengers[j] == ans:
ans, j = ans - 1, j - 1
return ans
// Accepted solution for LeetCode #2332: The Latest Time to Catch a Bus
/**
* [2332] The Latest Time to Catch a Bus
*
* You are given a 0-indexed integer array buses of length n, where buses[i] represents the departure time of the i^th bus. You are also given a 0-indexed integer array passengers of length m, where passengers[j] represents the arrival time of the j^th passenger. All bus departure times are unique. All passenger arrival times are unique.
* You are given an integer capacity, which represents the maximum number of passengers that can get on each bus.
* When a passenger arrives, they will wait in line for the next available bus. You can get on a bus that departs at x minutes if you arrive at y minutes where y <= x, and the bus is not full. Passengers with the earliest arrival times get on the bus first.
* More formally when a bus arrives, either:
*
* If capacity or fewer passengers are waiting for a bus, they will all get on the bus, or
* The capacity passengers with the earliest arrival times will get on the bus.
*
* Return the latest time you may arrive at the bus station to catch a bus. You cannot arrive at the same time as another passenger.
* Note: The arrays buses and passengers are not necessarily sorted.
*
* Example 1:
*
* Input: buses = [10,20], passengers = [2,17,18,19], capacity = 2
* Output: 16
* Explanation: Suppose you arrive at time 16.
* At time 10, the first bus departs with the 0^th passenger.
* At time 20, the second bus departs with you and the 1^st passenger.
* Note that you may not arrive at the same time as another passenger, which is why you must arrive before the 1^st passenger to catch the bus.
* Example 2:
*
* Input: buses = [20,30,10], passengers = [19,13,26,4,25,11,21], capacity = 2
* Output: 20
* Explanation: Suppose you arrive at time 20.
* At time 10, the first bus departs with the 3^rd passenger.
* At time 20, the second bus departs with the 5^th and 1^st passengers.
* At time 30, the third bus departs with the 0^th passenger and you.
* Notice if you had arrived any later, then the 6^th passenger would have taken your seat on the third bus.
*
* Constraints:
*
* n == buses.length
* m == passengers.length
* 1 <= n, m, capacity <= 10^5
* 2 <= buses[i], passengers[i] <= 10^9
* Each element in buses is unique.
* Each element in passengers is unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/the-latest-time-to-catch-a-bus/
// discuss: https://leetcode.com/problems/the-latest-time-to-catch-a-bus/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn latest_time_catch_the_bus(buses: Vec<i32>, passengers: Vec<i32>, capacity: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2332_example_1() {
let buses = vec![10, 20];
let passengers = vec![2, 17, 18, 19];
let capacity = 2;
let result = 16;
assert_eq!(
Solution::latest_time_catch_the_bus(buses, passengers, capacity),
result
);
}
#[test]
#[ignore]
fn test_2332_example_2() {
let buses = vec![20, 30, 10];
let passengers = vec![19, 13, 26, 4, 25, 11, 21];
let capacity = 2;
let result = 20;
assert_eq!(
Solution::latest_time_catch_the_bus(buses, passengers, capacity),
result
);
}
}
// Accepted solution for LeetCode #2332: The Latest Time to Catch a Bus
function latestTimeCatchTheBus(buses: number[], passengers: number[], capacity: number): number {
buses.sort((a, b) => a - b);
passengers.sort((a, b) => a - b);
let [j, c] = [0, 0];
for (const t of buses) {
c = capacity;
while (c && j < passengers.length && passengers[j] <= t) {
--c;
++j;
}
}
--j;
let ans = c > 0 ? buses.at(-1)! : passengers[j];
while (j >= 0 && passengers[j] === ans) {
--ans;
--j;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.