FastPrepSquare Each Array Value
Problems/Square Each Array Value

Square Each Array Value

AMD logoAMD● EasyINTERNONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks to square [1,2,3,4,5] and then use a dictionary. Arbitrary bounded inputs, ordered-array grading and the explicit dictionary follow-up are practice completions; approximate source match 95%.

Return an array containing the square of each input value, preserving order and repeated entries. Do not sort the result.

Follow-up: also describe a dictionary mapping each distinct input value to its square. The judged return value remains the ordered array; the first solution shows the dictionary construction.

Function

squareValues(values: int[]) → int[]

Examples

Example 1

values = [1,2,3,4,5]return = [1,4,9,16,25]

The values 1 through 5 have squares 1, 4, 9, 16 and 25.

Example 2

values = [-3,2,-3]return = [9,4,9]

Squaring preserves the input order and both occurrences of -3.

Constraints

  • 0 <= values.length <= 2000.
  • -1000 <= values[i] <= 1000.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int[] squareValues(int[] values) {
    // Write your code here.
}
Checking account…