Compress Consecutive Characters with Run Lengths

Quick Overview

Implement `compress_runs(s)` and return the compressed string. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

Compress Consecutive Characters with Run Lengths

Company: Oracle

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Compress Consecutive Characters with Run Lengths Implement `compress_runs(s)` and return the compressed string. The input contains ASCII letters only. Scan left to right. Emit a character unchanged for a run of length one; otherwise emit the character followed by its decimal run length. For example, `"aaaaabbbccca"` becomes `"a5b3c3a"`, and an empty string returns an empty string. Constraints: `0 <= len(s) <= 200000`; the encoded output contains at most `400000` ASCII characters. ```hint Check representation boundaries Include tests for an empty string, one character, a single long run, and a change after a multi-digit count. ```

Quick Answer: Implement `compress_runs(s)` and return the compressed string. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.

|Home/Coding & Algorithms/Oracle
Oracle logo
Oracle
Jul 26, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Compress Consecutive Characters with Run Lengths

Implement compress_runs(s) and return the compressed string. The input contains ASCII letters only. Scan left to right. Emit a character unchanged for a run of length one; otherwise emit the character followed by its decimal run length. For example, "aaaaabbbccca" becomes "a5b3c3a", and an empty string returns an empty string.

Constraints: 0 <= len(s) <= 200000; the encoded output contains at most 400000 ASCII characters.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...