FastPrepSquares of a Sorted Array

Squares of a Sorted Array

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums sorted in nondecreasing order, return an array containing the square of every value, also sorted in nondecreasing order.

Function

sortedSquares(nums: int[]) → int[]

Examples

Example 1

nums = [-4,-1,0,3,10]return = [0,1,9,16,100]

Squaring gives [16,1,0,9,100], which sorts to the returned array.

Example 2

nums = [-7,-3,2,3,11]return = [4,9,9,49,121]

The negative values contribute large squares from the left end.

Example 3

nums = []return = []

An empty input has no squares.

Constraints

  • 0 <= nums.length <= 100000.
  • -10000 <= nums[i] <= 10000.
  • nums is sorted in nondecreasing order.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] sortedSquares(int[] nums) {
    // Write your code here.
}
nums[-4,-1,0,3,10]
expected[0,1,9,16,100]
Checking account…