Update Array at Index (Immutable)
Arguments
arr: any[] —The input array. index: number —The index to update. value: any —The new value to write.
Returns
any[] — Solution idea
This is an immutable update helper: clone the array, update one index, and return the new array. It mirrors reducer patterns used in UI state management. Returning a new array every time avoids accidental mutation and keeps equality checks predictable for change detection and memoization.
updateAt([1, 2, 3], 1, 9); // => [1, 9, 3]
updateAt([1, 2], 5, 9); // => [1, 2]Public interview reports
These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.
- Taro: Datadog senior frontend engineer interview, 2025
One senior account describes live coding that starts simple and adds parts step by step, centred on data and tables. Immutable row updates are a supporting drill for that shape, not the reported exercise.
Reported in a public Datadog interview account. Datadog frontend interview questions
Solution
Overview
This is an immutable update helper: clone the array, update one index, and return the new array. It mirrors reducer patterns used in UI state management. Returning a new array every time avoids accidental mutation and keeps equality checks predictable for change detection and memoization.
Approach: Clone then assign
Create a shallow copy with slice or spread, validate the index, update the value on the copy, and return the copy. If the index is out of range, return the untouched copy for stability. This keeps the original input unchanged and makes the function safe for shared state.
export default function updateAt(arr, index, value) {
if (!Array.isArray(arr)) throw new TypeError('Expected array');
const copy = arr.slice();
if (index < 0 || index >= copy.length) return copy;
copy[index] = value;
return copy;
}
export default function updateAt<T>(arr: T[], index: number, value: T): T[] {
if (!Array.isArray(arr)) throw new TypeError('Expected array');
const copy = arr.slice();
if (index < 0 || index >= copy.length) return copy;
copy[index] = value;
return copy;
}
Notes & Pitfalls
Don't mutate the original array in-place. Be explicit about out-of-range indices. Returning the original array would break immutability expectations.
Empty array returns a new empty array. Negative or too-large index returns an unchanged copy. Index 0 and last index should work normally.
Array copying with .slice().Immutable update patterns.
Common mistakes on this challenge
Immutable update contract is not fully respected
The function should always return a new array and preserve original input. Start with a shallow copy ( arr.slice()) and modify only the copy.Return unchanged copy when index is out of range. Throw TypeErrorwhen input is not an array.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.