Return Squared Temperature Deltas in Sorted Order
Last updated: November 29, 2026
Quick Overview
Given a sorted array of temperature deltas that may be negative, return a new array of their squares, also sorted ascending, in linear time.
0
0
0 solved
Given a sorted array of temperature deltas that may be negative, return a new array of their squares, also sorted ascending, in linear time.
A quick Google phone-screen problem that looks trivial with a sort but tests whether the candidate sees that the largest squares sit at the two ends of the input. The interviewer listens for the reason two pointers work and for filling the output from the back.
What the Interviewer Expects
- Recognizes the largest square is at one of the two ends
- Fills the result from the last index backward
- Achieves O(n) instead of O(n log n)
- Handles all-negative and all-positive inputs
- Mentions the alternative of starting from the zero crossing
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you do it if you must output the squares in descending order?
- What if the function applied is any convex function, not squaring?
- Could you do it in place with O(1) extra space?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
deltas, sorted ascending, may contain negatives and zeros. - Output: a new list of
x * xfor each element, sorted ascending. - Target O(n) time....
High-Level Design
Squaring is not monotone over the whole array: negatives get larger as they move left. But the squares form a valley, large at both ends and smallest near zero. So the biggest remaining square is alwa...