Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
Return the running sum of nums.
Example 1:
Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Example 2:
Input: nums = [1,1,1,1,1] Output: [1,2,3,4,5] Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:
Input: nums = [3,1,2,10,1] Output: [3,4,6,16,17]
Constraints:
1 <= nums.length <= 1000-10^6 <= nums[i] <= 10^6Problem summary: Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,3,4]
[1,1,1,1,1]
[3,1,2,10,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1480: Running Sum of 1d Array
class Solution {
public int[] runningSum(int[] nums) {
for (int i = 1; i < nums.length; ++i) {
nums[i] += nums[i - 1];
}
return nums;
}
}
// Accepted solution for LeetCode #1480: Running Sum of 1d Array
func runningSum(nums []int) []int {
for i := 1; i < len(nums); i++ {
nums[i] += nums[i-1]
}
return nums
}
# Accepted solution for LeetCode #1480: Running Sum of 1d Array
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
return list(accumulate(nums))
// Accepted solution for LeetCode #1480: Running Sum of 1d Array
impl Solution {
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
let mut total = 0;
let mut arr = Vec::new();
for n in nums {
total += n;
arr.push(total);
}
return arr;
}
}
// Accepted solution for LeetCode #1480: Running Sum of 1d Array
function runningSum(nums: number[]): number[] {
for (let i = 1; i < nums.length; ++i) {
nums[i] += nums[i - 1];
}
return nums;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.