FastPrepFind the Best Server Shutdown Time

Find the Best Server Shutdown Time

Stripe logoStripe● EasyINTERNPHONE SCREEN
Learn

Problem statement

A server log is a binary string. Each character describes one time slot:

  • 0 means the server was running.
  • 1 means the server was down.

You may shut down the server immediately before any slot, including before the first slot or after the last slot. If you shut it down at index t, the penalty is:

  • one point for every down slot before t; and
  • one point for every running slot at or after t.

Return the shutdown index with the smallest penalty. If several indices have the same minimum penalty, return the earliest one.

What the interview report shared

The reported progression first scored a supplied shutdown time, then reused that penalty rule to choose a minimum-penalty shutdown position. Both endpoints were valid choices.

Function

bestShutdownTime(log: String) → int

Examples

Example 1

log = "0011"return = 2

At index 2, both running slots occur before shutdown and both down slots occur after shutdown, so the penalty is 0.

Example 2

log = "0101"return = 1

Indices 1 and 3 both have penalty 1. The earlier index is returned.

Constraints

  • 1 <= log.length <= 2 * 10^5.
  • log contains only 0 and 1.
  • For this exercise, equal minimum penalties use the earliest shutdown index.

More Stripe problems

See Stripe hiring insights
public int bestShutdownTime(String log) {
    // write your code here
}
log"0011"
expected2
Checking account…