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
Coding & Algorithms
Software Engineer
Doordash
May 27, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

18

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
Time and space complexity analysis
Data structure selection and trade-offs
Edge cases and input validation
Sorting and searching
Hash maps and frequency counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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
  1. Initialize a write pointer write_index to 0, which will point to the position in the string where the next character or its count will be written.
  2. Use a read pointer read_index to traverse t...

Submit Your Answer
Markdown supported

Related Questions