Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Strings Swap Without Extra Space

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

Your question is Strings Swap Without 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

A Backbase digital banking utility receives two string values that must be exchanged before further processing. Implement a function that swaps the values without declaring a third string or another named variable.

Python strings are immutable, so the solution must use Python's supported assignment semantics rather than attempting character-level mutation or arithmetic operations. The function should return the two values after swapping.

Formal Specification

  • Input: Two strings, first and second.
  • Output: A two-element tuple containing second followed by first.
  • Do not create a third named variable or use a temporary string variable.
  • Preserve every character exactly, including whitespace, repeated characters, Unicode characters, and empty strings.

Constraints

  • 0 <= len(first), len(second) <= 10^5
  • Inputs are valid Python strings
  • Do not declare a third named variable
  • Do not mutate either input string

Function Signature

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