Reported October 2023
ZipRecruiterprefix sum

Final and Maximum Health after Deltas

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

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

The ZipRecruiter OA reported in October 2023 looks too easy, and that's the trap. You walk a running health total through a list of deltas and return the final value plus the peak. One sloppy line and you fail the hidden tests. It's a simple prefix-sum pass, no clamping, no tricks in the math. The first attempt usually dies on the edge cases: an empty deltas array, or a health that only ever goes down. If you blank on the details, StealthCoder runs invisibly on your screen during the live assessment as a safety net. Here's what to watch for before you start typing.

The problem

Start at initialHealth and apply every signed delta in order without clamping. Return [finalHealth, maximumHealth], where the maximum includes the initial state.

Function
healthExtremes(initialHealth: int, deltas: int[]) → int[]

Examples
Example 1
initialHealth = 10
deltas = [5,-3,8,-20]
return = [0,20]
The final and peak values occur at different times.
Example 2
initialHealth = 7
deltas = [-1,-2]
return = [4,7]
The initial state remains the maximum.

Constraints
-1000000000 <= initialHealth <= 1000000000
0 <= deltas.length <= 100000
Every intermediate value fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The whole solution is one loop. Set current = initialHealth and best = initialHealth. For each delta, add it to current, then update best with max(best, current). Return [current, best]. The mistake that sinks a first attempt is initializing best to 0 or to the first post-delta value. Example 2 shows why: initialHealth is 7, health only drops, and the answer's max is still 7 because the initial state counts. A second pitfall is clamping at zero. The problem says don't, and Example 1 ends at 0 only because 10+5-3+8-20 happens to equal 0. Negative health is legal. With up to 100000 deltas this is O(n) time and O(1) space. Empty deltas should return [initialHealth, initialHealth]. If your nerves spike during the live OA, StealthCoder is the hedge that hands you this loop so you can check your own against it.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Final and Maximum Health after Deltas 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Final and Maximum Health after Deltas FAQ

How hard is the ZipRecruiter health deltas problem really?+

It's easy on algorithm and unforgiving on details. One linear pass does everything. The failures come from wrong initialization of the maximum, clamping at zero, or forgetting the empty array. If you handle those three, you pass. Expect the difficulty to be in not rushing.

What's the trick to this problem?+

Track the running total and the running maximum together, and seed the maximum with initialHealth, not zero. The problem says the maximum includes the initial state, and Example 2 tests exactly that. After that it's just add, compare, repeat.

Should I clamp health at zero or stop when it goes negative?+

No. The statement says to apply every signed delta in order without clamping. Health can go negative and keep changing. Don't break early either, since later positive deltas could still matter for the final value. Process every element and return the result.

What should the function return when deltas is empty?+

Return [initialHealth, initialHealth]. The constraints allow a length of 0, so the loop never runs. The final value is the starting value and the maximum is the initial state. Initializing both variables to initialHealth handles this with no special case.

How do I prepare for this in 48 hours?+

Write the loop from memory twice, then test three cases: Example 1, Example 2, and an empty array. Add one all-negative case and one case where the peak is the last step. That covers every way this one breaks. Don't overthink it with fancier structures.

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

OA at ZipRecruiter?
Invisible during screen share
Get it