Compress a status log

strings

Compress a status log

Uber Python Interview Question

Uber's driver app logs a letter for the driver's status every second, such as "a" for available and "b" for busy. To save space, runs of the same letter are stored as the letter followed by the length of the run.

Write a function compress(log) that returns the compressed log. Write the count only when a run is longer than 1.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

log = "aaabccdddd"

Output

"a3bc2d4"

Example 2

Input

log = "abc"

Output

"abc"

Explanation

In the first example, "aaabccdddd" has three a's, one b, two c's and four d's, so it becomes "a3bc2d4". The single b is written without a count.

Submit also runs 3 hidden test cases that check edge cases.