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 core interview patterns fundamentals.
Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element.
This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned.
If the length of the array is 0, the function should return init.
Please solve it without using the built-in Array.reduce method.
Example 1:
Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr; }
init = 0
Output: 10
Explanation:
initially, the value is init=0.
(0) + nums[0] = 1
(1) + nums[1] = 3
(3) + nums[2] = 6
(6) + nums[3] = 10
The final answer is 10.
Example 2:
Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr * curr; }
init = 100
Output: 130
Explanation:
initially, the value is init=100.
(100) + nums[0] * nums[0] = 101
(101) + nums[1] * nums[1] = 105
(105) + nums[2] * nums[2] = 114
(114) + nums[3] * nums[3] = 130
The final answer is 130.
Example 3:
Input:
nums = []
fn = function sum(accum, curr) { return 0; }
init = 25
Output: 25
Explanation: For empty arrays, the answer is always init.
Constraints:
0 <= nums.length <= 10000 <= nums[i] <= 10000 <= init <= 1000Problem summary: Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element. This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned. If the length of the array is 0, the function should return init. Please solve it without using the built-in Array.reduce method.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[1,2,3,4]
function sum(accum, curr) { return accum + curr; }
0[1,2,3,4]
function sum(accum, curr) { return accum + curr * curr; }
100[]
function sum(accum, curr) { return 0; }
25group-by)filter-elements-from-array)apply-transform-over-each-element-in-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2626: Array Reduce Transformation
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2626: Array Reduce Transformation
// type Fn = (accum: number, curr: number) => number;
//
// function reduce(nums: number[], fn: Fn, init: number): number {
// let acc: number = init;
// for (const x of nums) {
// acc = fn(acc, x);
// }
// return acc;
// }
// Accepted solution for LeetCode #2626: Array Reduce Transformation
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2626: Array Reduce Transformation
// type Fn = (accum: number, curr: number) => number;
//
// function reduce(nums: number[], fn: Fn, init: number): number {
// let acc: number = init;
// for (const x of nums) {
// acc = fn(acc, x);
// }
// return acc;
// }
# Accepted solution for LeetCode #2626: Array Reduce Transformation
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2626: Array Reduce Transformation
# type Fn = (accum: number, curr: number) => number;
#
# function reduce(nums: number[], fn: Fn, init: number): number {
# let acc: number = init;
# for (const x of nums) {
# acc = fn(acc, x);
# }
# return acc;
# }
// Accepted solution for LeetCode #2626: Array Reduce Transformation
// Rust example auto-generated from ts reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2626: Array Reduce Transformation
// type Fn = (accum: number, curr: number) => number;
//
// function reduce(nums: number[], fn: Fn, init: number): number {
// let acc: number = init;
// for (const x of nums) {
// acc = fn(acc, x);
// }
// return acc;
// }
// Accepted solution for LeetCode #2626: Array Reduce Transformation
type Fn = (accum: number, curr: number) => number;
function reduce(nums: number[], fn: Fn, init: number): number {
let acc: number = init;
for (const x of nums) {
acc = fn(acc, x);
}
return acc;
}
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.