Word Compression Algorithm
Word Compression Algorithm
Problem Description
Design and implement a string compression algorithm based on Run-Length Encoding (RLE). The algorithm should replace consecutive repeating characters with the character itself, followed by its repeat count. You must also provide the corresponding decompression algorithm.
Example:
- Input: "aaabbc"
- Output: "a3b2c1"
Requirements
- Algorithm Definition: Clearly define both the compression (encode) and decompression (decode) algorithms.
- Edge Case Handling: Your solution must explicitly address the following scenarios:
- Single-character runs (e.g., "c" should compress to "c1").
- Handling of very long input strings and very long consecutive runs of characters (e.g., inputs with millions of characters).
- Consider cases where the compression process might not reduce the string's length, or even make it longer.
- Complexity Analysis: Provide a Big-O time and space complexity analysis for both the compression and decompression algorithms.
Assumptions
- Encoding Scheme: Employ a simple Run-Length Encoding (RLE) where each run is represented by a single character followed immediately by its run length as a base-10 positive integer. No separators should be used between the character and its count. The count must always be included, even for single-character runs (e.g., 'c' becomes 'c1').
- Character Set: Input characters are Unicode code points. Counts are represented using ASCII digits (0-9).
- Ambiguity: Input strings can contain any characters, including digits. The specified format is considered unambiguous during decoding because the decoder will always read exactly one character, followed by one or more digits representing its count.
AI interview prep
Core dilemma
Balancing the simplicity of a fixed RLE scheme with the need for efficiency, robustness against edge cases (especially length expansion), and scalability for very large inputs, while adhering to strict format constraints.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Word Compression Algorithm
Word Compression Algorithm
Problem Description
Design and implement a string compression algorithm based on Run-Length Encoding (RLE). The algorithm should replace consecutive repeating characters with the character itself, followed by its repeat count. You must also provide the corresponding decompression algorithm.
Example:
- Input: "aaabbc"
- Output: "a3b2c1"
Requirements
- Algorithm Definition: Clearly define both the compression (encode) and decompression (decode) algorithms.
- Edge Case Handling: Your solution must explicitly address the following scenarios:
- Single-character runs (e.g., "c" should compress to "c1").
- Handling of very long input strings and very long consecutive runs of characters (e.g., inputs with millions of characters).
- Consider cases where the compression process might not reduce the string's length, or even make it longer.
- Complexity Analysis: Provide a Big-O time and space complexity analysis for both the compression and decompression algorithms.
Assumptions
- Encoding Scheme: Employ a simple Run-Length Encoding (RLE) where each run is represented by a single character followed immediately by its run length as a base-10 positive integer. No separators should be used between the character and its count. The count must always be included, even for single-character runs (e.g., 'c' becomes 'c1').
- Character Set: Input characters are Unicode code points. Counts are represented using ASCII digits (0-9).
- Ambiguity: Input strings can contain any characters, including digits. The specified format is considered unambiguous during decoding because the decoder will always read exactly one character, followed by one or more digits representing its count.