Problem · Array

Car Pooling Capacity Check

Learn this problem
MediumGoldman Sachs logoGoldman SachsFULLTIMEONSITE INTERVIEW

Problem statement

A vehicle has integer capacity. Each row [passengers, start, end] in trips means that many passengers ride on the half-open interval [start, end).

Return true if every trip can be completed without the number of passengers aboard exceeding capacity; otherwise return false. Drop-offs at a location happen before pickups at that same location.

Function

canCarryAll(capacity: int, trips: int[][]) → boolean

Examples

Example 1

capacity = 4trips = [[2,1,5],[3,3,7]]return = false

Five passengers would be aboard between locations 3 and 5.

Example 2

capacity = 5trips = [[2,1,5],[3,3,7]]return = true

The maximum load is exactly five.

Constraints

  • 1 <= capacity <= 10^9
  • 0 <= trips.length <= 200000
  • Each trip has 1 <= passengers <= 10^9 and 0 <= start < end <= 10^9.

More Goldman Sachs problems

drafts saved locally
public boolean canCarryAll(int capacity, int[][] trips) {
  // write your code here
}
capacity4
trips[[2,1,5],[3,3,7]]
expectedfalse
checking account