Loading...

The String Shrinker

A easy Python interview practice problem on DataDriven. Write and execute real python code with instant grading.

Domain
Python
Difficulty
easy
Seniority
mid

Problem

The logging pipeline produces status codes as long repeated-character strings (e.g. 'AAAAABBBCC') that waste storage. Before writing to the archive, the pipeline should compress these strings using run-length encoding. Write a function that replaces consecutive runs of the same character with the character followed by its count, omitting the count when it is 1. If the compressed version is not shorter, return the original.

Practice This Problem

Solve this Python problem with real code execution. DataDriven runs your Python code in a sandboxed Docker environment and grades it instantly.