Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Swap Variables Without Temp

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

Your question is Swap Variables Without Temp. 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

A CGI configuration utility receives two integer settings that must be exchanged before processing continues. Implement a function that swaps the values without declaring or using a temporary variable.

Formal Specification

Write swap_values(a, b), where a and b are integers. Return a two-element list containing the values in swapped order: [b, a]. The input variables themselves do not need to be modified outside the function.

Use Python's assignment semantics or another valid algorithmic technique. Do not create a named temporary variable to hold either value.

Constraints

  • -10^9 <= a, b <= 10^9
  • a and b are integers
  • A named temporary variable may not be used
  • Return exactly two values in swapped order

Function Signature

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