Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Swap String Prefix In Place

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

Your question is Swap String Prefix In Place. 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

Fiverr may need to rotate a mutable character buffer used for displaying marketplace text. Given a sequence of characters chars and an integer n, move the first n characters to the end while preserving the order of both segments.

The operation must be performed in place, using O(N) time and O(1) auxiliary space. Since Python strings are immutable, chars is provided as a list of single-character strings. Modify and return that same list.

Formal Specification

  • Input: chars, a list of characters, and n, an integer satisfying 0 <= n <= len(chars).
  • Output: The modified chars list after a left rotation by n positions.
  • The relative order within the first segment and the remaining segment must remain unchanged.
  • Do not create another list or use slicing to perform the rotation.

Constraints

  • 0 <= len(chars) <= 10^5
  • 0 <= n <= len(chars)
  • Every element of chars is a single-character string
  • The input list must be modified in place

Function Signature

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