Swap Two Integers Without a Temporary Variable
Updated
Problem statement
Given two signed 32-bit integers a and b, swap their values without using a third scalar variable.
Return a two-element array [a, b] containing the values after the swap. The returned array is only the output representation; the swapping steps themselves must not store either value in a temporary scalar variable.
Function
swapWithoutTemporary(a: int, b: int) → int[]Examples
Example 1
a = 30b = 40return = [40,30]After the swap, a contains 40 and b contains 30.
Example 2
a = -7b = 12return = [12,-7]The same operation works when one value is negative.
Example 3
a = 5b = 5return = [5,5]Swapping equal values leaves the pair unchanged.
Constraints
-2147483648 <= a <= 2147483647-2147483648 <= b <= 2147483647- Do not use a third scalar variable to hold either input value during the swap.
More Oracle Onsite Interview questions
- Course ScheduleONSITE INTERVIEW · Seen Nov 2026
- Shortest Subarray with Sum at Least KONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Minimum Meeting RoomsONSITE INTERVIEW · Seen Nov 2026
- Shortest Window with Required CountsONSITE INTERVIEW · Seen Nov 2026
- Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
- Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
- Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
- Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
- Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Add Two Binary StringsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Add BinaryMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Check Whether a Byte Is NonzeroZscaler · ONSITE INTERVIEWZscaler interview questions
- Find the Lowest Set Bit PositionExpedia · OA · ONSITE INTERVIEWExpedia interview questions
- Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
- K-th Amount a Payout Machine Can GiveRippling · ONSITE INTERVIEWRippling interview questions
- First Register Overflow TickNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Product of Two Integers Without MultiplyingCapital One · ONSITE INTERVIEWCapital One interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA