Reported September 2021
Stripearray

Calculate Server Shutdown Penalty

Reported by candidates from Stripe's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Stripe OA. Under 2s to a working solution.
Founder's read

Stripe reported this one in September 2021, and it looks like a warm-up until you read the penalty rule twice. You get a whitespace-separated log of 0s and 1s plus a removal index, and you count down slots before it and up slots at or after it. It's a single pass over an array of tokens. No tricks in the data structure, just a split and a counter. If the parsing or the off-by-one slips when the clock is running, StealthCoder is the safety net that runs invisibly during the live OA and hands you the answer.

The problem

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 = 3
return = 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 = 2
return = 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 = 0
return = 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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The data structure is just an array of states. Split the string on whitespace, then walk the indexes once. For i less than removedTime, add 1 when the state is 1. For i at or after removedTime, add 1 when the state is 0. That's O(n) time and O(1) extra space if you scan without building a second array. The pitfalls are small but real. Split on one or more whitespace characters, not a single space, because the constraints allow multiple. Watch the boundary: removedTime itself counts as after removal. Also handle removedTime equal to 0 and equal to n, where one side of the sum is empty. You can use prefix counts, but with a single query that's overkill. If you blank on the boundary or the parsing during the live OA, StealthCoder is the hedge that reads the problem and gives you working code.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Calculate Server Shutdown Penalty cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum penalty for a shop. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Stripe's OA.

Stripe reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Calculate Server Shutdown Penalty FAQ

How hard is the Stripe Calculate Server Shutdown Penalty problem really?+

Easy. It's a linear scan with a conditional counter. The only difficulty is parsing the whitespace-separated string and getting the boundary right at removedTime. If you can write a loop and a split, you can finish it in a few minutes.

What's the trick to this problem?+

There isn't a deep one. Count 1s at indexes below removedTime and 0s at indexes from removedTime onward. Add them. The whole problem is reading the penalty definition carefully and not flipping which side counts which state.

Where do people lose points on this one?+

Off-by-one at removedTime, and parsing. Index removedTime belongs to the after-removal side. Also, the input says one or more whitespace characters, so split on a whitespace pattern, not a literal single space. Test the edge cases of removedTime 0 and n.

Do I need prefix sums for n up to 200000?+

No. One pass is O(n), which is fine for 200000. Prefix sums only help if you'd answer many removedTime queries on the same log. Here there's one query, so a single loop is simpler and less error-prone.

How do I prepare for this in 48 hours?+

Practice string-to-array parsing in your language and write a couple of boundary-driven counting loops. Run the three given examples, then try removedTime 0 and removedTime n by hand. That covers nearly everything this problem can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Stripe.

OA at Stripe?
Invisible during screen share
Get it