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.

Scan the ASCII-letter string `s` from left to right and return its run-length encoding. Emit a character alone for a run of length one; otherwise emit the character followed by the run's decimal length. Return the empty string for empty input.

Constraints

  • 0 <= len(s) <= 200000, and s contains ASCII letters only.
  • A run of length one emits no numeric suffix; longer runs emit their base-10 length.
  • The encoded output contains at most 400000 ASCII characters.

Examples

Input: ('',)

Expected Output: ''

Explanation: Empty input returns empty output.

Input: ('a',)

Expected Output: 'a'

Explanation: A singleton run emits only its character.

Hints

  1. Test empty and singleton strings, a two-character run, and all-singleton input.
  2. Include a run whose count has multiple decimal digits followed by a different character.
  3. Use uppercase and lowercase transitions to confirm comparisons remain case-sensitive.

Loading coding console...