Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Encode a Run-Length Style String
00:00
5 left

Encode a Run-Length Style String

EasyPython

Problem

Encode a string as 'frequency + character', e.g. "aabccc" is encoded as "2a1b3c".

Asked in the online screening test stage. Q1 of 2; the wording was reported as very easy.

Input and Output

Implement encode_string(s), which accepts a string and returns a string containing one count + character segment for each consecutive run. Counts must be written as decimal integers, and character order must be preserved.

Constraints

  • 0 <= len(s) <= 10^5
  • The input contains printable characters
  • Counts must be represented as decimal integers
  • Consecutive runs, not total character frequencies, are encoded

Function Signature

def encode_string(s):
Interviewer

Your question is Encode a Run-Length Style String. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.