Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Variable Swap in Python

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

Your question is Implement Variable Swap in Python. 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 Crédit Agricole CIB market-data preprocessing step, two locally held values may need to exchange positions. Implement a function that swaps two Python values and returns them in reversed order.

The function must use Python tuple unpacking rather than an explicit temporary variable. It must not modify any external data structure, and the original argument variables do not need to change because Python arguments are passed by assignment.

Formal Specification

Implement swap_values(first, second).

  • Input: Two Python values, first and second. The values may be integers, strings, floats, or other directly assignable Python objects.
  • Output: A two-element tuple (second, first) containing the values in swapped order.
  • The function should perform the swap in constant time and use constant auxiliary space.

Constraints

  • Exactly two arguments are provided.
  • Inputs are valid Python values.
  • The swap must not use an explicit temporary variable.
  • Return a two-element tuple containing the values in reversed order.

Function Signature

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