FastPrepSwap Two Numbers
Problem · Array

Swap Two Numbers

Learn this problem
EasyGoogle logoGoogleINTERNPHONE SCREEN
See Google hiring insights

Problem statement

Given two integers first and second, return a two-element array containing their values after they are swapped.

The returned array must be [second, first]. You may use ordinary temporary storage; no arithmetic-only or bitwise-only technique is required.

Function

swapNumbers(first: int, second: int) → int[]

Examples

Example 1

first = 7second = -3return = [-3,7]

The second value becomes the first element, and the first value becomes the second element.

Example 2

first = 5second = 5return = [5,5]

Equal values remain equal after their positions are exchanged.

Example 3

first = -1000000000second = 1000000000return = [1000000000,-1000000000]

The operation simply reverses the two boundary values without arithmetic.

Constraints

  • -10^9 <= first <= 10^9
  • -10^9 <= second <= 10^9

More Google problems

drafts saved locally
public int[] swapNumbers(int first, int second) {
    // Write your code here.
}
first7
second-3
expected[-3,7]
checking account