DSA Tracker

Problem

Easy

Squares of a Sorted Array

An easy Two Pointers interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Two Pointers
Sheets
1
Core for
10 roles

The problem

The input array is sorted but may contain negative numbers. Square every value and return the squares in ascending order.

Example 1

Input
nums = [-5, -2, 0, 4, 9]
Output
[0, 4, 16, 25, 81]
Why
Squares are 25, 4, 0, 16, 81; sorted they read 0, 4, 16, 25, 81.

Example 2

Input
nums = [-6, -4, 1, 4, 8]
Output
[1, 16, 16, 36, 64]

Constraints

  • 1 <= nums.length <= 10^4
  • -10^4 <= nums[i] <= 10^4
  • nums is sorted in non-decreasing order

How to think about it

Updated 2026-09-29

Squaring a sorted sequence creates a V-shape: the largest values reside strictly at the outer extremes, where absolute values are greatest. Comparing the two ends always uncovers the next largest square, letting you populate the destination array backwards in a single coordinated pass.

Approaches, worst first

  1. Square and sort

    time O(n log n) · space O(n)

    Square each number in place or into a new list, then run a standard sorting algorithm. Correct and trivial to write, but it completely discards the partial ordering already present in the input.

  2. Inward two-pointer fillWrite this one

    time O(n) · space O(n)

    Position pointers at both ends of the array and allocate a result buffer of size n. At each step, square both ends, place the larger square at the current tail of the result buffer, and advance that pointer inward.

Where people lose marks · 3
  • Filling the output array forward from index 0. The two pointers surface the largest squares first, not the smallest; writing forward requires an extra reverse or binary search for the inflection point.
  • Comparing squared values rather than absolute values before squaring. While math is equivalent, squaring large numbers prematurely can cause arithmetic overflows in fixed-width numeric environments.
  • Moving both pointers when values tie at the ends. Only one pointer must advance per written element to ensure no elements are dropped.

The theory behind it

Two Pointers — the ground this problem stands on. All Two Pointers problems

What Two Pointers is

Two pointers is a search coordinated by two fingers placed on different entries of an ordered row, stepping inward toward each other or advancing at different speeds. Instead of scanning with nested loops that re-examine identical pairs, each pointer moves unidirectionally based on comparisons. Because every movement permanently prunes bad candidate combinations, the search inspects the collection in a single joint pass.

When to reach for it

Reach for two pointers when problems require finding index pairs, reversing sequences, trapping rainwater, or filtering duplicates in sorted arrays. When an input is already sorted and an exhaustive search takes quadratic time, opposing pointers meeting in the center often solve it linearly. Fast and slow pointer variations also detect cycles or locate midpoint nodes across linked sequences.

How the pattern works

Initialize pointers at opposing edges for convergent searches, or start both at the beginning to move at different speeds. Under a convergent setup, calculate current pair metrics; if the sum falls short of your target, advance the lower pointer rightward to increase value, and if it exceeds your target, shift the upper pointer leftward to decrease value. Each pointer move throws away only pairs that were already known to be wrong, so the right answer is never missed.

More Two Pointers problems

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .