Maximum Riders in Car
Updated
Problem statement
There are n riders and one car with room for everyone. Rider i gives a range comfort[i] = [low, high]. The rider gets in only if the number of other riders in the car is between low and high, inclusive. If that is not true, the rider does not ride.
Choose a group of riders to put in the car. The group is valid when every rider in it is comfortable with the other riders in the group. A group of k riders has k - 1 other riders for each member, so every member needs low ≤ k - 1 ≤ high.
For example, if three riders accept between 0 and 2 others, all three can ride together, because each of them sees 2 others.
Return the largest number of riders that can be in the car at the same time. Return 0 if no rider can ride.
Function
maxRiders(comfort: int[][]) → intExamples
Example 1
comfort = [[1, 3], [2, 4], [0, 2], [1, 3]]return = 3There are 4 riders. Their ranges are [1, 3], [2, 4], [0, 2] and [1, 3].
For a group of 3 riders, each member sees 2 others. All four riders accept 2, so any 3 of them can ride together.
For a group of 4 riders, each member sees 3 others. Only riders 0, 1 and 3 accept 3. Rider 2 does not, so the group of 4 is not valid.
The answer is 3.
Example 2
comfort = [[0, 2], [0, 2], [0, 2]]return = 3Each rider accepts 0 to 2 others. A group of all 3 riders gives each member 2 others, which is allowed. The answer is 3.
Example 3
comfort = [[2, 3], [2, 3]]return = 0A group of 1 gives each member 0 others, and a group of 2 gives each member 1 other. Both riders need at least 2 others, so no group is valid. The answer is 0.
Example 4
comfort = [[0, 0]]return = 1The one rider accepts 0 others and rides alone. The answer is 1.
Constraints
1 ≤ n ≤ 105, whereniscomfort.lengthcomfort[i]has exactly two numbers,[low, high]0 ≤ low ≤ high ≤ 105for every rider
More Uber Online Assessment questions
- Count Palindromic Ancestor PathsOA · Seen Aug 2026
- Jump Game with Prime-3 StepsOA · Seen Aug 2026
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Jump Game with Prime-Step RuleOA · Seen May 2026
- Pipeline Throughput Maximization Under BudgetOA · Seen May 2026
- Balanced Prefix Sets in a PermutationOA · Seen Apr 2026
- Minimum Edge Reversals to Root a TreeOA · Seen Apr 2026
- Graph Palindrome QueriesOA · Seen Apr 2026
Similar questions at other companies
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Maximum Interactive Team SizeMicrosoft · OAMicrosoft interview questions
- Linear InterpolatorTwo Sigma · OATwo Sigma interview questions
- Find Sum of BeautiesAmazon · OA
- Choose ContainersIMC Trading · OAIMC Trading interview questions
- Minimize Maximum Group DifferenceDRW · OADRW interview questions
- Maximum Team SizeExpedia · OAExpedia interview questions
- Employee With the Most OverlapsCitadel · OACitadel interview questions
- Maximum Taxi EarningsWells Fargo · OAWells Fargo interview questions
- Minimum-Cost Solar Rooftop AllocationDeloitte · OADeloitte interview questions