Ctrl K

A Letter in a Growing Word

Hard
A word starts as "a" and grows according to the array steps. On every step a copy of the word is appended to its right: - if steps[i] == 0, the copy is exact; - if steps[i] == 1, every letter of the copy is replaced by the next one in the alphabet (z wraps around to a). For example, "az" becomes "azba". After all steps the word has 2^len(steps) letters. Return the letter at position position (1-based). position can be huge, so building the word itself is not an option.
Example 1
position=6, steps=[1, 1, 0] → 'b'

"a" → "ab" → "abbc" → "abbcabbc". The sixth letter is "b".

Example 2
position=4, steps=[0, 0] → 'a'

No letter is ever shifted: the word is "aaaa" and every position holds "a".

Example 3
position=8, steps=[1, 1, 1] → 'd'

"a" → "ab" → "abbc" → "abbcbccd". The last letter is "d".

Constraints
  • 1 ≤ len(steps) ≤ 100
  • steps[i] is 0 or 1
  • 1 ≤ position ≤ 10¹⁴
  • After all steps the word has at least position letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsOnly Twos Multiplied EasyOnly Fours Multiplied EasyDoubling Rows of Bits MediumA String That Copies Itself EasyThe K-th Ordering of Digits Hard
loading Python…
Input
position=6, steps=[1, 1, 0]
Expected
'b'

Run checks the first 3 tests in your browser. Submit checks all 48 tests on the server, hidden ones included.