Cyclically Increment the First k Letters of a String for Each Roll

Quick Overview

Each value in a roll array shifts the first k letters of a lowercase string forward by one, with z wrapping around to a, and the task is to return the string after every roll. It tests batching many overlapping prefix updates into one linear pass when the string and the roll list can each hold 100,000 entries.

Cyclically Increment the First k Letters of a String for Each Roll

Company: PayPal

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a string `s` of lowercase English letters and an array `roll`. Each element `roll[i]` means the first `roll[i]` characters of the string are cyclically incremented by 1. A cyclic increment turns `'a'` into `'b'`, `'b'` into `'c'`, and so on, with `'z'` turning into `'a'`. After performing all roll operations, return the resulting string. Optimize the solution so that it does not process each roll operation individually over its substring. ### Function Signature ```python def roll_the_string(s: str, roll: list[int]) -> str: ``` ### Constraints - `1 <= len(s) <= 10^5` - `1 <= len(roll) <= 10^5` - `1 <= roll[i] <= len(s)` - `s` consists of lowercase English letters only. ### Examples ```text Input: s = "abz", roll = [2,3,1] Output: "dda" ``` The roll of 2 gives `"bcz"`, the roll of 3 gives `"cda"` (the `'z'` wraps to `'a'`), and the roll of 1 gives `"dda"`. ```text Input: s = "zzz", roll = [1,1,1] Output: "czz" ``` All three rolls touch only index 0, which goes from `'z'` to `'a'`, then `'b'`, then `'c'`. The other two letters are never rolled.

Overview: Each value in a roll array shifts the first k letters of a lowercase string forward by one, with z wrapping around to a, and the task is to return the string after every roll. It tests batching many overlapping prefix updates into one linear pass when the string and the roll list can each hold 100,000 entries.

PayPal logo
PayPal
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s of lowercase English letters and an array roll. Each element roll[i] means the first roll[i] characters of the string are cyclically incremented by 1. A cyclic increment turns 'a' into 'b', 'b' into 'c', and so on, with 'z' turning into 'a'.

After performing all roll operations, return the resulting string. Optimize the solution so that it does not process each roll operation individually over its substring.

Function Signature

def roll_the_string(s: str, roll: list[int]) -> str:

Constraints

  • 1 <= len(s) <= 10^5
  • 1 <= len(roll) <= 10^5
  • 1 <= roll[i] <= len(s)
  • s consists of lowercase English letters only.

Examples

Input: s = "abz", roll = [2,3,1]
Output: "dda"

The roll of 2 gives "bcz", the roll of 3 gives "cda" (the 'z' wraps to 'a'), and the roll of 1 gives "dda".

Input: s = "zzz", roll = [1,1,1]
Output: "czz"

All three rolls touch only index 0, which goes from 'z' to 'a', then 'b', then 'c'. The other two letters are never rolled.