FastPrepSwap Two Numbers Without a Third Variable
Problems/Swap Two Numbers Without a Third Variable

Swap Two Numbers Without a Third Variable

Infosys logoInfosys● EasyNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Infosys new grad onsite interview. The report does not give the function interface or the limits on the numbers. Our version is about 85% the same as the reported question.

Given two signed integers, first and second, swap their values without using a third variable to store either original value.

Return a two-element array containing the swapped values in the order [second, first]. The returned array is the output container; it is not a temporary variable for performing the swap.

Function

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

Examples

Example 1

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

After the swap, first holds -2 and second holds 5, so the returned array is [-2, 5].

Example 2

first = 9second = 9return = [9,9]

The two input values are equal, so swapping them leaves the ordered pair unchanged.

Constraints

  • -2^31 <= first <= 2^31 - 1
  • -2^31 <= second <= 2^31 - 1
  • Do not use an additional variable to store either original value while performing the swap.

More Infosys Onsite Interview questions

  • Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
  • Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
  • Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
  • Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
  • Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
  • Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
  • Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
  • Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
  • Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
  • Missing and Repeated NumberCommvault · ONSITE INTERVIEWCommvault interview questions
  • Check Endianness Of A Byte ArrayNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
  • Swap Two Integers Without a Temporary VariableOracle · ONSITE INTERVIEWOracle interview questions
  • Extract a Register Bit FieldApple · ONSITE INTERVIEWApple interview questions
  • Add Two Binary StringsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
  • Add BinaryMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Count Set Bits in an Integer ArrayAkuna Capital · ONSITE INTERVIEWAkuna Capital interview questions
  • Check Whether a Byte Is NonzeroZscaler · ONSITE INTERVIEWZscaler interview questions
  • Find the Odd-Occurrence ValueExpedia · ONSITE INTERVIEWExpedia interview questions
All Infosys interview questionsSee Infosys hiring insights
Saved
class Solution {
    public int[] swapNumbers(int first, int second) {
        // write your code here
    }
}
Checking account…