Sort Squared Ranking Score Deltas in Linear Time

Last updated: November 29, 2026

Quick Overview

Given a sorted array of ranking score deltas that may include negatives, return a sorted array of their squares in O(n) time.

Meta
Coding & Algorithms
Machine Learning Engineer
Meta
November 29, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a sorted array of ranking score deltas that may include negatives, return a sorted array of their squares in O(n) time.

Shows up in Meta screens for engineers who work near feed ranking, where you often deal with signed deltas. The interviewer wants you to see that the largest square sits at one of the two ends and use two pointers instead of re-sorting.

What the Interviewer Expects
  • Notes that sorting the squares works but costs O(n log n)
  • Identifies that the biggest magnitude is at either end of the sorted input
  • Fills the output from the back with two pointers
  • Handles all-negative and all-positive inputs without special cases
  • States O(n) time and O(n) output space
Key Topics to Cover
two pointers
sorted array
merge from ends
squares
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the squares in descending order instead.
  • Apply any convex function f(x) = ax^2 + bx + c and keep the result sorted.
  • Can you do it in place if the output may overwrite the input?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: deltas, sorted ascending, length up to 10^5, values may be negative. Output: a new list of x*x for each element, sorted ascending....

High-Level Design

Squares are large at both ends and small in the middle, so the input is two sorted runs in disguise. Keep lo at the start and hi at the end, and fill the result from the last index: whichever end ...


Submit Your Answer
Markdown supported