For algorithm/data-structure snippets, make correctness and the function contract explicit: don’t silently break edge cases, validate required input constraints, and only mutate inputs when the name/description guarantees it. Also prefer native/built-in equivalents when available.
Apply:
const outputArr = [...inputArr];) and sort/search in the copy.n=0, smallest/largest lengths, and off-by-one loops. Add/keep tests for those boundaries.Array.find() rather than custom duplicates.Example (non-mutating bubble sort with correct contract):
const bubbleSort = (inputArr) => {
const outputArr = [...inputArr];
const len = outputArr.length;
let swapped;
do {
swapped = false;
for (let i = 0; i < len - 1; i++) {
if (outputArr[i] > outputArr[i + 1]) {
const tmp = outputArr[i];
outputArr[i] = outputArr[i + 1];
outputArr[i + 1] = tmp;
swapped = true;
}
}
} while (swapped);
return outputArr;
};
When in doubt, write boundary-focused tests first (e.g., empty array, length 1, n=0, invalid preconditions) and only then finalize the implementation.