Design a Compressed String Iterator (Advanced)

Data Structures
Medium
Stripe
85.2K views

Design an iterator for a run-length encoded string that handles sequences of characters and numbers, supporting `next` and `hasNext` efficiently.

Why Interviewers Ask This

Stripe evaluates this question to assess a candidate's ability to optimize space complexity while maintaining efficient time complexity in iterator design. They specifically look for understanding of run-length encoding edge cases, such as multi-digit numbers and empty strings, and the capacity to implement stateful logic without reconstructing the entire string in memory.

How to Answer This Question

1. Clarify requirements: Confirm if input contains only digits followed by characters or mixed formats, and define behavior for invalid inputs. 2. Analyze constraints: Discuss how storing the full decoded string violates O(1) space goals, necessitating a pointer-based approach. 3. Define state variables: Propose tracking current character, remaining count, and an index pointer into the compressed string. 4. Draft hasNext logic: Explain how to skip past digit sequences to find the next available character efficiently. 5. Implement next method: Detail parsing multi-digit counts, decrementing the counter, and returning the character while updating internal state. 6. Edge case review: Test scenarios like single-character runs, end-of-string boundary conditions, and consecutive digits.

Key Points to Cover

  • Explicitly stating the decision to use O(1) space instead of pre-decoding
  • Demonstrating correct parsing logic for multi-digit integers within the string
  • Separating hasNext and next logic to prevent redundant traversals
  • Handling boundary conditions where the string ends mid-sequence
  • Discussing time complexity trade-offs between initialization and iteration

Sample Answer

To solve the Compressed String Iterator problem efficiently, I would avoid decoding the entire string upfront to maintain O(1) space complexity, which aligns with Stripe's focus on performance and resource efficiency. First, I'd initialize three pointers: one for the current position in the encoded string, one to track the number of remaining instances of the current character, and another to store the current character itself. In the hasNext method, I would iterate through the string to skip over any completed character runs and parse the next digit sequence to update the remaining count and current character. If no more characters exist, it returns false. For the next method, I would first ensure hasNext is true, then return the current character and decrement the remaining count. If the count reaches zero, I advance the main pointer to parse the next digit sequence and character. A critical detail here is handling multi-digit numbers correctly; I must loop until a non-digit character is found to construct the full integer value. For example, given 'a12', the iterator should yield 'a' twelve times before moving on. I would also handle edge cases like trailing digits without characters or empty inputs gracefully. This approach ensures that both next and hasNext operations run in amortized O(1) time relative to the output size, making it highly scalable for large encoded datasets.

Common Mistakes to Avoid

  • Decoding the entire string immediately, which fails the space complexity constraint
  • Failing to parse multi-digit numbers correctly, treating each digit as a separate count
  • Not updating the internal state pointer after a character is fully exhausted
  • Ignoring edge cases where the string ends with a number but no following character

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 57 Stripe questions