JavaScript Interview Questions on Data Transformations
A good data-transformation answer states the input contract, produces the intended output and explains whether the input changes. Knowing the names map, filter and reduce is less useful than choosing an operation whose behavior matches the task.
By Ravindra Wadile · Published

605 words · About 3 min read
A good data-transformation answer states the input contract, produces the intended output and explains whether the input changes. Knowing the names map, filter and reduce is less useful than choosing an operation whose behavior matches the task.
The following example groups fictional parcels by status. The input rows are deliberately small so you can check every output rather than trusting a complicated chain.
How can values be grouped without changing the input?
const parcels = [
{ id: "A", status: "ready" },
{ id: "B", status: "held" },
{ id: "C", status: "ready" }
];
const before = JSON.stringify(parcels);
const grouped = parcels.reduce((groups, parcel) => {
const ids = groups.get(parcel.status) ?? [];
groups.set(parcel.status, [...ids, parcel.id]);
return groups;
}, new Map());
console.assert(grouped.get("ready").join(",") === "A,C");
console.assert(grouped.get("held").join(",") === "B");
console.assert(JSON.stringify(parcels) === before);
The accumulator is a new Map. The input rows are only read. This implementation builds new arrays for each updated group. That makes the copying visible, but also has a cost for large groups. Do not call the algorithm optimal merely because it avoids mutating the input.
Is mutating an accumulator the same as mutating the input?
No. A newly created accumulator belongs to the transformation. Updating it can be a reasonable local implementation choice. The important external contract here is that the caller's input is not modified and the returned result contains the intended grouping.
For larger input, appending IDs to arrays owned solely by the new accumulator can avoid repeated array copies. Explain that ownership condition before choosing the version. Immutability language should describe the boundary that matters, rather than ban every local assignment.
When would map or filter be clearer?
Map is useful when producing one output value for each input value. Filter selects a subset. Reduce can build a different overall result. A simple filtering task does not need a reducer just to demonstrate familiarity with it.
Give the operation a name that reflects its purpose. A short helper called readyParcelIds can be easier to review than an unexplained expression repeated in several places.
What happens with empty input?
The explicit initial Map provides a valid result even when no rows exist. A reduce call without an initial value has different empty-input behavior. State the empty-result shape as part of the contract instead of discovering it only after the interviewer asks.
Also discuss malformed input. Should a missing status be rejected, mapped to a separate unknown category or left as a missing key? The example assumes valid rows. It does not silently establish a business rule for incomplete data.
Does a shallow copy make nested data independent?
No. Copying an outer array leaves its existing row objects referenced unless those are also copied. This example returns arrays of string IDs, so the output does not expose mutable source row objects. Returning the original objects would create a different sharing contract.
That difference matters if a caller later edits a returned row. Explain whether the output contains references, snapshots or selected primitive values, and choose tests accordingly.
Questions to use as follow-ups
- How should duplicate IDs be handled?
- Must groups preserve the first-seen status order?
- What output is required for an empty list?
- Who owns the arrays in the result?
- What changes if a status is missing?
For practice, add a duplicate ID and state whether it remains duplicated. Then implement only that chosen policy. A defensible answer explains the required data rule and its trade-offs; it does not guess a rule from an attractive one-line solution.
References and next practice
developer.mozilla.org — js objects · developer.mozilla.org — js execution
Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud