Compress Consecutive Characters with Run Lengths
Last updated: July 26, 2026
Quick Overview
This question asks the candidate to implement a function that compresses a string by replacing consecutive characters with their counts, known as run-length encoding.
Oracle
July 26, 20260
0
6 solved
This question asks the candidate to implement a function that compresses a string by replacing consecutive characters with their counts, known as run-length encoding.
This question is commonly asked during onsite interviews for software engineering roles at Oracle to assess a candidate's coding skills and problem-solving abilities. The interviewer is looking for the candidate's understanding of string manipulation, algorithm efficiency, and their ability to write clean and maintainable code. This question also tests the candidate's ability to handle edge cases and optimize their solution.
What the Interviewer Expects
- Clearly explain the approach to solving the problem.
- Implement a working solution with proper edge case handling.
- Discuss the time and space complexity of the solution.
- Provide a clean and readable code structure.
- Test the solution with various input cases.
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 handle an empty string input?
- Can you optimize your solution further?
- What would you do if the input string is very large?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Approach
To solve the problem of compressing consecutive characters, we will iterate through the string while keeping track of the current character and its count. Whenever we encounter a different character, ...
Algorithm
Here is a Python implementation of the run-length encoding algorithm:
def compress_string(s: str) -> str:
if not s:
return ""
result = []
count = 1
for i in range(1,...