Minimum Value to Get Positive Step by Step Sum

EasyArrays & Hashingprefix sum

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums. Pick a **positive** integer startValue, then add the elements of nums to it one at a time from left to right. Return the smallest positive startValue such that the running total is at least 1 after every single addition.

Examples

Input: nums = [-3,2,-3,4,2]
Output: 5
Starting at 5 the running totals are 2, 4, 1, 5, 7; starting at 4 the third total would be 0.
Input: nums = [1,2]
Output: 1
The totals only grow, and `startValue` must be positive, so 1 works.
Input: nums = [1,-2,-3]
Output: 5
The lowest prefix sum is -4, so the start must be 5.

Constraints

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More prefix sum problems