Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Swap Variables Using Extra Space

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

Your question is Swap Variables Using Extra Space. 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

Honeywell Experion configuration tools may need to exchange two numeric calibration values before applying an update. Write a function that swaps two values using an explicit temporary variable.

The function must preserve both original values during the assignment sequence and return the swapped values in a two-element list.

Formal Specification

Implement swap_values(a, b).

  • Input: Two integers, a and b.
  • Output: A list [b, a], containing the second input first and the first input second.
  • Use an extra variable, such as temp, to hold one value while the assignments occur.
  • Do not modify any external state.

Constraints

  • -10^9 <= a, b <= 10^9
  • Inputs are integers
  • The returned list contains exactly two values

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