Decoded String at Index

MediumStackmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

An encoded string s is decoded into a tape by reading it from left to right: - A letter is appended to the end of the tape. - A digit d (from 2 to 9) replaces the tape with d back-to-back copies of itself. Given s and a 1-indexed position k, return the k-th character of the fully decoded tape as a one-character string. The decoded tape can be astronomically long (far beyond memory), so you must not build it. k is guaranteed not to exceed the tape length.

Examples

Input: s = "leet2code3", k = 10
Output: "o"
The tape is "leetleetcodeleetleetcodeleetleetcode"; its 10th character is "o".
Input: s = "ha22", k = 5
Output: "h"
The tape is "hahahaha"; the 5th character is "h".
Input: s = "a2345678999999999999999", k = 1
Output: "a"
The tape is only "a" repeated, so every position holds "a".

Constraints

  • `2 <= s.length <= 100`
  • `s` has lowercase letters and digits `2`-`9`, and starts with a letter
  • `1 <= k <= 10^9`
  • `k` does not exceed the decoded length

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems