Optimize compression for O(1) space
Last updated: May 27, 2026
Quick Overview
Given a string, optimize the compression of its characters such that the resulting compressed string uses O(1) space. The output should be the length of the compressed string, and the characters should be stored in the original string itself. For example, if the input is "aabbcc", the output should be 6, as the compressed form would be "a2b2c2".
Doordash
May 27, 202618
0
2,884 solved
Given a string, optimize the compression of its characters such that the resulting compressed string uses O(1) space. The output should be the length of the compressed string, and the characters should be stored in the original string itself. For example, if the input is "aabbcc", the output should be 6, as the compressed form would be "a2b2c2".
Doordash uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.
What the Interviewer Expects
- Identify the correct data structure and algorithm for the problem
- Write clean, bug-free code with proper variable naming
- Analyze time and space complexity correctly
- Handle basic edge cases (empty input, single element)
- Communicate your thought process while coding
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you parallelize this solution?
- Can you optimize the space complexity of your solution?
- What is the worst-case input for your solution?
- How would you test this solution thoroughly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
The task is to compress a string in such a way that we count consecutive characters and replace them with the character followed by its count. The output length is the length of this compressed string...
Approach
- Initialize a write pointer
write_indexto 0, which will point to the position in the string where the next character or its count will be written. - Use a read pointer
read_indexto traverse t...