Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Swapping Without a Temp Variable

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Swapping Without a Temp Variable. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In a UiPath automation component, two integer configuration values are stored in variables x and y. Implement a function that swaps their values without using a third variable or creating a temporary container.

Use the XOR swap technique. The function must return the swapped values as a two-element tuple, with the new value of x first and the new value of y second.

Formal Specification

  • Input: Two integers, x and y.
  • Output: A tuple (new_x, new_y) containing the original value of y followed by the original value of x.
  • Do not use a third variable, list, dictionary, or other temporary container to hold either value.
  • The input variables may contain equal, zero, positive, or negative integers.

Constraints

  • -10^9 <= x, y <= 10^9
  • Inputs are integers
  • Do not use a third variable or temporary container
  • Use O(1) auxiliary space

Function Signature

def swap_values(x, y):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output