Implement string compression and decompression
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string manipulation, run-length encoding concepts, parsing numeric counts, and validation of encoded formats, including handling invalid inputs. It is in the Coding & Algorithms domain and is primarily a practical implementation task that also probes conceptual understanding of parsing, edge-case reasoning, and efficiency trade-offs.
Constraints
- 0 <= len(text) <= 100000
- For `compress`, `text` contains only English letters `A-Z` and `a-z`
- For `decompress`, a valid encoding alternates `letter` + `positive integer count`
- For valid decompression inputs, the total decompressed output length will not exceed 100000
Examples
Input: ("compress", "aaabbccccd")
Expected Output: "a3b2c4d1"
Explanation: The runs are `a x 3`, `b x 2`, `c x 4`, and `d x 1`.
Input: ("decompress", "a3b2c4d1")
Expected Output: "aaabbccccd"
Explanation: Expand each letter by its count: `a3 -> aaa`, `b2 -> bb`, `c4 -> cccc`, `d1 -> d`.
Input: ("compress", "")
Expected Output: ""
Explanation: An empty string compresses to an empty string.
Input: ("decompress", "a12B3")
Expected Output: "aaaaaaaaaaaaBBB"
Explanation: `a12` expands to 12 lowercase `a` characters and `B3` expands to 3 uppercase `B` characters.
Input: ("decompress", "ab2")
Expected Output: None
Explanation: Invalid: after `a`, a numeric count is required, but `b` appears immediately.
Input: ("decompress", "a03")
Expected Output: None
Explanation: Invalid: counts cannot start with `0`, so leading zeros are not allowed.
Hints
- For compression, scan left to right and count the length of each run of identical characters.
- For decompression, after reading one letter, keep reading digits until the next letter to build the full count. Make sure the count exists and does not start with `0`.