Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Implement Variable Swap in Python
00:00
5 left

Implement Variable Swap in Python

EasyPython

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):
Interviewer

Your question is Implement Variable Swap in Python. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.