Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rearrange Letters by Positions

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

Your question is Rearrange Letters by Positions. 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

An OnStar vehicle service receives a message whose characters arrive out of order. You are given a string letters and an integer array positions, where positions[i] is the destination index for letters[i]. Rearrange the characters into their intended order and return the resulting string.

Formal Specification

  • Input letters: a string of length N.
  • Input positions: an integer array of length N.
  • positions is a permutation of every integer from 0 through N - 1.
  • Character letters[i] must be placed at index positions[i] in the output.
  • Return the restored string.

Constraints

  • 1 <= len(letters) <= 10^5
  • len(letters) == len(positions)
  • positions contains every integer from 0 through len(letters) - 1 exactly once
  • letters can contain uppercase letters, lowercase letters, digits, and spaces

Function Signature

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