The Design Compressed String Iterator interview question involves building a specialized iterator for a compressed string like "L1e2t1c1o1d1e1". In this format, each character is followed by the number of times it repeats. You need to implement next() to return the next uncompressed character and hasNext() to check if more characters exist. This Design Compressed String Iterator coding problem tests your ability to parse and expand data lazily.
Google ask this to evaluate your Iterator interview pattern skills and how you handle data streams. It tests whether you can expand the string "on-the-fly" (lazily) rather than uncompressing the whole thing at once, which could use memory. It's a test of memory efficiency and string parsing.
This problem uses Lazy Evaluation and String Parsing.
index pointer.currentChar and remainingCount variables.next() is called:
remainingCount > 0, decrement it and return currentChar.remainingCount == 0, parse the next character and its following multi-digit count from the string.Compressed: "a2b3".
next(): Sees 'a', reads count '2'. Returns 'a', remaining=1.next(): remaining > 0. Returns 'a', remaining=0.next(): remaining=0. Sees 'b', reads count '3'. Returns 'b', remaining=2.hasNext(): Returns true since index hasn't reached the end or remaining > 0."a1000000000", the program will crash."a10").Always implement iterators Lazily. Interviewers are checking to see if you can process data without loading the entire resulting dataset into RAM. Use a while loop to parse consecutive digits into a single integer.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Peeking Iterator | Medium | Solve | |
| Encode and Decode Strings | Medium | Solve | |
| Iterator for Combination | Medium | Solve | |
| Unique Word Abbreviation | Medium | Solve | |
| RLE Iterator | Medium | Solve |