FastPrepSwap Two Integers Without a Temporary Variable
Problems/Swap Two Integers Without a Temporary Variable

Swap Two Integers Without a Temporary Variable

Oracle logoOracle● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Oracle onsite interview. The report gives a = 30 and b = 40 and asks you to swap them without a third variable. It does not give a reusable function format, the range of numbers, how to show the output, or more test data. Our version works for any pair of signed 32-bit integers. Our version is about 92% the same as the reported question.

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

Oracle interview experiences

  • Oracle SWE Onsite Interview Experience: 1 Experience
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public int[] swapWithoutTemporary(int a, int b) {
      // write your code here
    }

}
Checking account…