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 arr and a mapping function fn, return a new array with a transformation applied to each element.
The returned array should be created such that returnedArray[i] = fn(arr[i], i).
Please solve it without the built-in Array.map method.
Example 1:
Input: arr = [1,2,3], fn = function plusone(n) { return n + 1; }
Output: [2,3,4]
Explanation:
const newArray = map(arr, plusone); // [2,3,4]
The function increases each value in the array by one.
Example 2:
Input: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
Output: [1,3,5]
Explanation: The function increases each value by the index it resides in.
Example 3:
Input: arr = [10,20,30], fn = function constant() { return 42; }
Output: [42,42,42]
Explanation: The function always returns 42.
Constraints:
0 <= arr.length <= 1000-109 <= arr[i] <= 109fn returns an integer.Problem summary: Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element. The returned array should be created such that returnedArray[i] = fn(arr[i], i). Please solve it without the built-in Array.map method.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
function plusone(n) { return n + 1; }
[1,2,3]function plusI(n, i) { return n + i; }
[1,2,3]function constant(n, i) { return 42; }
[10,20,30]group-by)filter-elements-from-array)array-reduce-transformation)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
// Auto-generated Java example from ts.
class Solution {
public void exampleSolution() {
}
}
// Reference (ts):
// // Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
// function map(arr: number[], fn: (n: number, i: number) => number): number[] {
// for (let i = 0; i < arr.length; ++i) {
// arr[i] = fn(arr[i], i);
// }
// return arr;
// }
// Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
// Auto-generated Go example from ts.
func exampleSolution() {
}
// Reference (ts):
// // Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
// function map(arr: number[], fn: (n: number, i: number) => number): number[] {
// for (let i = 0; i < arr.length; ++i) {
// arr[i] = fn(arr[i], i);
// }
// return arr;
// }
# Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
# Auto-generated Python example from ts.
def example_solution() -> None:
return
# Reference (ts):
# // Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
# function map(arr: number[], fn: (n: number, i: number) => number): number[] {
# for (let i = 0; i < arr.length; ++i) {
# arr[i] = fn(arr[i], i);
# }
# return arr;
# }
// Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
// 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 #2635: Apply Transform Over Each Element in Array
// function map(arr: number[], fn: (n: number, i: number) => number): number[] {
// for (let i = 0; i < arr.length; ++i) {
// arr[i] = fn(arr[i], i);
// }
// return arr;
// }
// Accepted solution for LeetCode #2635: Apply Transform Over Each Element in Array
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
for (let i = 0; i < arr.length; ++i) {
arr[i] = fn(arr[i], i);
}
return arr;
}
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.