Sorted Squares in Descending Order
Updated
Problem statement
Given an integer array nums sorted from smallest to largest, return the square of every element from highest to lowest.
Values may be negative. Preserve duplicate occurrences. Return an empty array when nums is empty.
Try to achieve O(nums.length) time by using the sorted input.
Function
descendingSquares(nums: int[]) → int[]Examples
Example 1
nums = [-4,-1,0,3,10]return = [100,16,9,1,0]The squares [16,1,0,9,100] become [100,16,9,1,0] in highest-to-lowest order.
Example 2
nums = [-3,-1,2]return = [9,4,1]The negative value -3 produces the largest square.
Example 3
nums = [-2,0,2]return = [4,4,0]Both entries with magnitude 2 contribute 4.
Constraints
0 <= nums.length <= 5000.numsis sorted in nondecreasing order.-10000 <= nums[i] <= 10000.
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 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
Similar questions at other companies
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Assign CookiesAmazon · PHONE SCREEN
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Squares of a Sorted ArrayStryker · PHONE SCREEN
- Squares of a Sorted ArrayGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Merge Into the Zero-Padded ArrayAdobe · PHONE SCREENAdobe interview questions
- Three Partition ArrayMicrosoft · PHONE SCREENMicrosoft interview questions
- Assign CookiesExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Merge Two Sorted ArraysZillow · PHONE SCREENZillow interview questions
- Merge Sorted Arrays with DuplicatesMygate · PHONE SCREENMygate interview questions