Problem · Intervals

Detect a Server RAM Capacity Breach

Learn this problem
MediumMyntra logoMyntraINTERNONSITE INTERVIEW

Problem statement

A server cluster runs jobs described by parallel arrays ram, startTimes, and endTimes. Job i consumes ram[i] units during the half-open interval [startTimes[i], endTimes[i]).

Return true if the total RAM used by active jobs is ever strictly greater than totalCapacity. Otherwise, return false. A job ending at a time releases its RAM before any job starting at that same time becomes active.

Function

exceedsRamCapacity(ram: long[], startTimes: int[], endTimes: int[], totalCapacity: long) → boolean

Examples

Example 1

ram = [4,3,5]startTimes = [1,2,6]endTimes = [5,7,8]totalCapacity = 6return = true

From time 2 through time 5, the first two jobs use 7 units, which exceeds 6.

Example 2

ram = [4,3,2]startTimes = [1,5,5]endTimes = [5,7,6]totalCapacity = 5return = false

At time 5, the 4-unit job has ended before the 3-unit and 2-unit jobs start, so usage reaches but never exceeds 5.

Constraints

  • 0 <= ram.length <= 2 * 10^5.
  • ram.length == startTimes.length == endTimes.length.
  • 1 <= ram[i] <= 10^9.
  • 0 <= startTimes[i] < endTimes[i] <= 10^9.
  • 1 <= totalCapacity <= 10^18.

More Myntra problems

drafts saved locally
public boolean exceedsRamCapacity(long[] ram, int[] startTimes, int[] endTimes, long totalCapacity) {
    // Return true exactly when active RAM usage exceeds capacity.
}
ram[4,3,5]
startTimes[1,2,6]
endTimes[5,7,8]
totalCapacity6
expectedtrue
checking account