Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The demand-forecasting job writes its daily errors (actual minus forecast) to a metrics table, already sorted ascending, so large over-forecasts sit at the start as big negatives and large under-forecasts sit at the end. The accuracy dashboard only cares how big each miss was, and wants those sizes smallest first.
Write sorted_abs_errors(errors). It takes a list of integer errors sorted ascending, which may include negatives and zero, and returns a new list of their absolute values sorted ascending.
Function to write
sorted_abs_errors(errors: list[int]) -> list[int]A new list of the absolute error sizes, sorted ascending, one per input error.
How to approach it
The largest size is always at the left end or the right end. Compare those two, place the larger one in the last free slot of the result, and move that pointer inward.
Sample cases
+ 3 held back until you submit
negatives outweigh the positives
After abs() the list reads 7, 3, 2, 3, 11 — no longer sorted, which is the whole trap.
Input
Argument 1
[
-7,
-3,
2,
3,
11
]Returns
[
2,
3,
3,
7,
11
]no forecast errors
An empty error list returns an empty list, with no index into an empty input.
Input
Argument 1
[] (empty list)
Returns
[] (empty list)
every forecast ran high
Every actual fell below its forecast, so all errors are negative and the sizes come out in reverse order.
Input
Argument 1
[
-9,
-4,
-4,
-1
]Returns
[
1,
4,
4,
9
]equal sizes from both sides
-5 and 5 tie at the ends, and both sizes must appear in the output, as must the zero.
Input
Argument 1
[
-5,
-2,
0,
2,
5
]Returns
[
0,
2,
2,
5,
5
]Constraints
errors is a list of integers sorted ascending. It may contain negatives, zero and repeated values.-5 and 5 both contribute a 5.errors list returns an empty list [].sorted() or .sort() on the absolute values — the input order already tells you where the largest sizes are.Worked example
Take [-7, -3, 2, 3, 11]. The biggest size must sit at one of the two ends, because the most negative and the most positive values are both there. Compare the ends: |-7| is 7, |11| is 11, so 11 goes in the last slot and the right pointer moves to 3. Now 7 against 3: 7 goes in the next slot back and the left pointer moves to -3. 3 against 3: a tie, take either — say the right, so 3 is placed and the right pointer moves to 2. 3 against 2: place 3, left moves to 2. One element left: place 2. Reading the slots front to back gives [2, 3, 3, 7, 11].
The trap: [abs(e) for e in errors] returns [7, 3, 2, 3, 11] on that input, and on [-9, -4, -4, -1] it returns the sizes in exactly reverse order.
What this tests
Two pointers closing in from both ends. This is LeetCode 'Squares of a Sorted Array' (977) in data clothing, with absolute values instead of squares, and Exponent's data-engineer question list tags that problem at PayPal and Meta. The target is O(n) time and O(n) space for the output; taking abs() and then sorting is O(n log n) and passes every sample, which is why interviewers ask for the linear version. A data engineer meets this whenever a signed metric — drift, error, balance change — is stored sorted and someone needs it ranked by magnitude.
sorted_abs_errors(errors: list[int]) -> list[int]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.