The function below mutates its input array — a classic source of bugs.
// dirty — mutates the original array!
interface CartItem { name: string; price: number; }
function solve(cart: CartItem[], item: CartItem): CartItem[] {
cart.push(item);
cart.sort((a, b) => a.price - b.price);
return cart;
}Refactor `solve(cart, item)` so it returns a new sorted array without modifying cart.
After calling solve(cart, item), the original cart array must be unchanged.
Sample tests