FastPrepMerge Sorted Squares of Two Arrays
Problems/Merge Sorted Squares of Two Arrays

Merge Sorted Squares of Two Arrays

WhatNot logoWhatNot● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Senior engineering reports describe merging two sorted arrays by squared value; one explicitly requires squared output and includes negative numbers. The judged core matches at about 95%; ascending order, multiplicity, empty inputs, and bounds complete the practice contract.

Given two integer arrays a and b, each sorted from smallest to largest, return one array containing the square of every element from both arrays, sorted from smallest to largest.

Negative values are allowed. Keep one result for every input element, including repeated values and equal squares. Either or both arrays may be empty.

Can you solve the task in O(a.length + b.length) time without sorting the squared values?

Function

mergeSortedSquares(a: int[], b: int[]) → int[]

Examples

Example 1

a = [1,3,7]b = [4,10,11]return = [1,9,16,49,100,121]

Squaring the two reported inputs gives [1,9,49] and [16,100,121]; merging gives the displayed order.

Example 2

a = [-4,-1,0,3]b = [-2,2]return = [0,1,4,4,9,16]

Both -2 and 2 contribute 4.

Example 3

a = []b = [0,5]return = [0,25]

An empty first input contributes no elements.

Constraints

  • 0 <= a.length + b.length <= 5000.
  • Both arrays are sorted in nondecreasing order.
  • -10000 <= a[i], b[i] <= 10000.

More WhatNot problems

  • User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Adjacent Pair and Run RemovalPHONE SCREEN · Seen Oct 2026
  • Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public int[] mergeSortedSquares(int[] a, int[] b) {
        // write your code here
    }
}
Checking account…