FastPrepTotal Server Downtime

Total Server Downtime

Visa logoVisa● EasyFULLTIMENEW GRADINTERNOA
Learn

Problem statement

A server is monitored during every integer second from 1 through t, inclusive. You are given intervals, where intervals[i] = [start, end] means the server was running from second start through second end, inclusive.

The uptime intervals may overlap. Return the total number of seconds in the monitoring window during which the server was not running.

Function

getTotalDowntime(intervals: int[][], t: int) → int

Examples

Example 1

intervals = [[1,3],[2,5]]t = 10return = 5

The intervals overlap and cover seconds 1 through 5. The server is down during seconds 6 through 10, for a total of 5 seconds.

Example 2

intervals = [[2,2],[5,7]]t = 8return = 4

The server is up for seconds 2, 5, 6, and 7. Four of the eight monitored seconds are downtime.

Example 3

intervals = [[1,2],[2,4],[7,8]]t = 9return = 3

The first two intervals merge into [1,4], and [7,8] is separate. The server is up for 6 of the 9 monitored seconds, so total downtime is 3.

Constraints

  • 1 <= intervals.length <= 2 * 10^5.
  • 1 <= t <= 10^9.
  • 1 <= intervals[i][0] <= intervals[i][1] <= t.

Source note: The additional source frame includes a second sample whose output is 3.

More Visa problems

See Visa hiring insights
public int getTotalDowntime(int[][] intervals, int t) {
  // write your code here
}
intervals[[1,3],[2,5]]
t10
expected5
Checking account…