Sorted Squares From an Ordered Array
Updated
Problem statement
Given an integer array nums sorted in non-decreasing order, return an array of the squares of its values, also sorted in non-decreasing order.
Negative values make this less simple than it looks: -6 is the smallest value but its square, 36, may be the largest. Equal squares all stay in the answer.
Function
sortedSquares(nums: int[]) → int[]Examples
Example 1
nums = [-6,-2,1,3,5]return = [1,4,9,25,36]The squares in the given order are 36, 4, 1, 9, 25. Sorted: [1,4,9,25,36].
Example 2
nums = [-8,-8,-3]return = [9,64,64]All values are negative, so the squares come out largest first: 64, 64, 9. Sorted: [9,64,64]; the repeated value stays twice.
Example 3
nums = [0,2,2,7]return = [0,4,4,49]The values are already non-negative, so squaring keeps the order: [0,4,4,49].
Constraints
1 <= nums.length <= 2000-10^4 <= nums[i] <= 10^4numsis sorted in non-decreasing order.
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)