Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Run-Length String Compression Choice

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Run-Length String Compression Choice. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

At Dropbox, you are asked to implement a lightweight string compressor using run-length encoding. Given a string s, compress consecutive repeated characters so that each group becomes the character followed by its count, then return whichever is shorter: the compressed string or the original string.

Formal Specification

  • Input: a string s
  • Output: a string
  • Consecutive repeated characters are encoded as character + count
  • If the compressed result is not strictly shorter than the original string, return the original string

Constraints

  • 0 <= len(s) <= 10^5
  • s may contain any printable characters
  • Compression uses consecutive character runs only
  • Return the original string if the compressed version is not strictly shorter

Function Signature

def compress_string(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output