Implement minimal RLE encode/decode with max run K
LiftoffSoftware Engineer · Technical ScreenLast updated
This question evaluates proficiency in string parsing and compression techniques, specifically run-length encoding/decoding, pattern detection, and algorithmic optimization for producing minimal-length encodings under a maximum pattern length constraint.
You are given a run-length encoding (RLE) format that compresses a string by grouping consecutive repetitions of a short substring.
Encoding format
An encoded string is a concatenation of tokens. Each token has the form:
<count><pattern>countis a positive integer written in base-10 (may be multiple digits, no+sign).patternis a non-empty string of lowercase lettersa-z.- The decoded expansion of the token is
patternrepeatedcounttimes.
Tokens are concatenated directly. Since digits appear only in count and pattern contains only letters, boundaries are determined by digit vs. letter transitions.
Example
"2aab3ab" decodes to "aab" + "aab" + "ab" + "ab" + "ab" = "aabaabababab".
Task A — Decode
Implement:
decode(encoded: str) -> str
Given a valid encoded string, return the decoded original string.
Task B — Encode (optimized)
Implement:
encode(s: str, K: int) -> str
You must produce an encoded string that decodes to exactly s, under the constraint:
- Every
patternused in any token must have length ≤ K.
Among all valid encodings, return one with minimum total encoded length (fewest characters in the encoded string). If multiple shortest encodings exist, you may return any one.
Example
For s = "aabaabababab", K = 3, one shortest encoding is:
"2aab3ab"
because it represents 2 * "aab" followed by 3 * "ab", and each pattern length is ≤ 3.
Notes / Constraints
scontains only lowercase lettersa-z.1 ≤ K ≤ len(s).countcan be larger than 9 (multi-digit).- Your encoding must use only the token format above; i.e., you cannot emit raw uncounted literals.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.