Problem · Array

Maximum Riders in Car

MediumUberFULLTIMEOA
See Uber hiring insights

There are n riders and one car with infinite capacity. Each rider i is comfortable riding in the car only if the number of other riders also in the car is between comfort[i][0] and comfort[i][1] (inclusive). If this condition is not met, the rider will not enter the car.

All riders decide simultaneously — a valid group is a subset of riders where every rider in the group is comfortable with the group size minus one.

Return the maximum number of riders that can be in the car at the same time.

Examples
01 · Example 1
comfort = [[1, 3], [2, 4], [0, 2], [1, 3]]
return = 3

With k=3 riders, each needs to be comfortable with 2 others. Riders 0 (1–3), 1 (2–4), and 2 (0–2) all include 2 in their range → valid group of 3.
With k=4, each would need 3 others. Only riders 0, 1, and 3 cover 3 — only 3 riders qualify, not 4.

02 · Example 2
comfort = [[0, 2], [0, 2], [0, 2]]
return = 3

With all 3 riders, each is comfortable with 2 others (2 ∈ [0, 2]). All 3 can ride together.

03 · Example 3
comfort = [[2, 3], [2, 3]]
return = 0

k=1 requires 0 others; k=2 requires 1 other. Neither 0 nor 1 falls in [2, 3] for any rider, so no valid group exists.

04 · Example 4
comfort = [[0, 0]]
return = 1

The single rider is comfortable with 0 others. They ride alone.

Constraints
  • 1 ≤ n ≤ 105
  • 0 ≤ comfort[i][0] ≤ comfort[i][1] ≤ n
More Uber problems
drafts saved locally
public int maxRiders(int[][] comfort) {
    // write your code here
}
comfort[[1, 3], [2, 4], [0, 2], [1, 3]]
expected3
sign in to submit