Optimize compression for in-place

Last updated: September 11, 2025

Quick Overview

Given a string, implement an in-place algorithm to optimize its compression by replacing consecutive repeated characters with a single character followed by the count of repetitions. The function should return the length of the modified string and modify the input string directly. For example, given the input "aabbcc", the output should be "a2b2c2" with a length of 6.

Lyft
Coding & Algorithms
Machine Learning Engineer
Lyft
September 11, 2025
Machine Learning Engineer
Take-home Project
Coding & Algorithms
Easy

10

0

508 solved


Given a string, implement an in-place algorithm to optimize its compression by replacing consecutive repeated characters with a single character followed by the count of repetitions. The function should return the length of the modified string and modify the input string directly. For example, given the input "aabbcc", the output should be "a2b2c2" with a length of 6.

Lyft uses this problem in the Take-home Project 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
Hash maps and frequency counting
Edge cases and input validation
Data structure selection and trade-offs
Tree structures and recursion
Binary search and divide and conquer
Time and space complexity analysis
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 your solution change if the input was sorted?
  • 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 problem requires us to compress a string by replacing consecutive repeated characters with a single character followed by the count of repetitions. This can be efficiently solved using the **two p...

Approach
  1. Initialize write pointer to 0, which will track the position to write compressed characters.
  2. Use a read pointer to iterate through the string.
  3. For each character, count its consecutiv...

Submit Your Answer
Markdown supported

Related Questions