Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Swap Two Words Without Temp

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

Your question is Swap Two Words 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

Hexaware Technologies QA automation utilities may need to reorder captured words while preserving the original list object. Given a list of words and two valid indices, swap the words at those indices in place without declaring a third variable or creating another list.

Formal Specification

Implement swap_words(words, first_index, second_index).

  • Input: words, a mutable list of strings; first_index and second_index, two valid integer indices.
  • Output: The same list object after the two selected words have been exchanged.
  • The function must not use an explicitly declared temporary variable.
  • The relative order of all other words must remain unchanged.

Constraints

  • 2 <= len(words) <= 10^5
  • Each word is a non-empty string
  • Both indices are valid
  • The list must be modified in place
  • No explicitly declared third variable may be used

Function Signature

def swap_words(words, first_index, second_index):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output