Code Room
CodingMediumcod-g141
Subject String parsingLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

A run-length-encoded string repeats blocks of the form <count><char>, where <count> is a positive decimal integer of one or more digits and <char> is a single lowercase letter (e.g. "3a2b1c" decodes to "aaabbc", length 6). Given the encoded string s and an integer cap, return the decoded length, but as soon as the running decoded length would exceed cap, stop and return -1. The empty string decodes to length 0. Counts can be large, so you must not materialize the decoded string.

Implement
run_length_decode_capacity(s: str, cap: int) → int
Examples
in["3a2b1c",100]out6
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.