Square a sorted list
Return the squares of a sorted integer list in sorted order in linear time. Negative values are permitted.
items = [-4, -1, 0, 3]
print(abs(items[0]), abs(items[-1]))
print(max(items[0]**2, items[-1]**2))Reason about the state
The greatest absolute value lies at one end. Fill the output from right to left while consuming one endpoint per step. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the squares of a sorted integer list in sorted order in linear time. Negative values are permitted.
The explanation opens here once every check passes.