Decode and Explain Ambiguity in Compression Strings
Company: Pinterest
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: This question evaluates parsing of compact count-value encodings, reasoning about ambiguous encodings, and combinatorial enumeration of all valid decodings.
Constraints
- 1 <= len(s) <= 30
- s contains only digits '0'..'9'
- Each pair is: count (positive integer, no leading zeros) followed by value (single digit 0..9)
- Return all expanded arrays sorted lexicographically (standard list ordering)
- If no valid partition exists, return []
- In test data, the total number of integers across all returned arrays will not exceed 1e5
Examples
Input: 12
Expected Output:
Input: 20
Expected Output:
Hints
- Use DFS/backtracking over the index. At each index, try all possible count lengths (1..k) as long as at least one digit remains for the value.
- Disallow counts that start with '0'.
- Memoize by index to avoid recomputing decodings for the same suffix.
- Combine the current (count,value) expansion with all decodings of the remaining suffix.
- Sort the final results lexicographically for deterministic output.