Running Sum of 1D Array

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

Given an integer array nums, build its **running sum**. The running sum is a new array result of the same length where each entry result[i] equals the total of every element from the start of nums up to and including index i. Formally: result[i] = nums[0] + nums[1] + ... + nums[i] Return the resulting array. This is the classic **prefix sum** construction: each position carries the accumulated total of everything seen so far. Aim to do it in a single pass with O(n) time and O(1) extra space (beyond the output array).

Examples

Input: nums = [1, 2, 3, 4]
Output: [1, 3, 6, 10]
The running sums are [1, 1+2, 1+2+3, 1+2+3+4] = [1, 3, 6, 10].
Input: nums = [1, 1, 1, 1, 1]
Output: [1, 2, 3, 4, 5]
Each step adds one more 1, so the totals climb by 1 each index.
Input: nums = [3, -2, 5, -1]
Output: [3, 1, 6, 5]
Negative values pull the accumulated total back down: 3, then 3-2=1, then 1+5=6, then 6-1=5.

Constraints

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6
  • The returned array has the same length as nums.

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