Swap Two Integers
Updated
Problem statement
You are given two integers a and b. Swap their values: afterwards a should hold the old value of b, and b the old value of a.
Return the two values after the swap as an array [a, b], which is the original [b, a]. Any way of swapping is allowed, including a temporary variable.
Function
swapNumbers(a: int, b: int) → int[]Examples
Example 1
a = 12b = -40return = [-40,12]After the swap a holds -40 and b holds 12.
Example 2
a = 2147483647b = 5return = [5,2147483647]The values trade places. Note that a + b = 2147483652 is larger than the largest 32-bit integer, 2147483647.
Example 3
a = 0b = 0return = [0,0]Both values are 0, so the result looks the same as the input.
Constraints
-2^31 <= a, b <= 2^31 - 1
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- N-th Fibonacci NumberONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
Similar questions at other companies
- Confusing Bib NumbersVerily · ONSITE INTERVIEW
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Climbing StairsAmazon · ONSITE INTERVIEW
- Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW