FastPrepCalculate Server Shutdown Penalty

Calculate Server Shutdown Penalty

Stripe logoStripe● EasyNEW GRADINTERNPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

A server log contains one state per time slot. The states are separated by whitespace:

  • 0 means the server was up and functioning during that slot.
  • 1 means the server was down during that slot.

You remove the server immediately before slot removedTime. Slots with indexes smaller than removedTime occur while the server is still installed; later slots occur after removal.

The penalty is the number of down slots before removal plus the number of up slots at or after removal. Return that total penalty.

Function

calculateShutdownPenalty(logs: String, removedTime: int) → int

Examples

Example 1

logs = "0 0 0 1 0 0"removedTime = 3return = 2

No down slot occurs before removal. Two up slots occur after it, so the penalty is 2.

Example 2

logs = "1 1 0 0"removedTime = 2return = 4

The two down slots before removal and the two up slots after removal each contribute one penalty point.

Example 3

logs = "0 1 1"removedTime = 0return = 1

The server is removed before every slot. Only the first slot was an up slot, so only it is penalized.

Constraints

  • 1 <= n <= 200000, where n is the number of log states.
  • Every state is exactly 0 or 1, separated by one or more whitespace characters.
  • 0 <= removedTime <= n.

More Stripe problems

See Stripe hiring insights
public int calculateShutdownPenalty(String logs, int removedTime) {
  // write your code here
}
logs"0 0 0 1 0 0"
removedTime3
expected2
Checking account…