arrays · Problem 1 of 3
Running Sum
easy
Amazon
Return an array where each element is the sum of all elements up to and including that index.
runningSum([1, 2, 3, 4]) -> [1, 3, 6, 10]Your solution
Runs your code and animates it without grading anything. Change the input to see what it does on a case the tests do not cover.
Running is free — Submit is what records it. Or press ⌘↩
Tests
4 cases, 1 hidden| call | type | expected | result |
|---|---|---|---|
| runningSum([1,2,3,4]) | basic | [1,3,6,10] | — |
| runningSum([3,-1,2]) | negatives | [3,2,4] | — |
| runningSum([]) | empty | [] | — |
| withheld | hidden | withheld | — |
Hidden cases run too — their inputs aren't listed here, so aim for a general solution rather than one fitted to the cases above.
Complexity
- target time
- O(n)
- target space
- O(n)
One pass, and the output array is the only extra space. Recomputing each prefix from scratch would be O(n^2).
Hints
Stuck? Hints open one at a time, each giving a little more away.
2 hints left