Merge Sorted Squares of Two Arrays
Updated
Problem statement
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 Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- 3SumAmazon · PHONE SCREEN
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Meeting Rooms IIUber · PHONE SCREENUber interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions