Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
String Compression Coding
00:00
5 left

String Compression Coding

MediumPython

Problem

Affirm may compact repeated characters in a sequence used by a downstream text-processing component. Given a list of single-character strings, compress each consecutive run in place using run-length encoding.

For each run, write the character once. If its count is greater than 1, write the decimal digits of the count immediately after it. Return the length of the compressed prefix. The input list may contain unused values beyond the returned length, and the algorithm must not allocate another list proportional to the input size.

Formal Specification

  • Input: chars, a mutable list of single-character strings.
  • Output: an integer representing the length of the compressed prefix.
  • Mutation: the first returned positions of chars must contain the compressed result.
  • Counts must be written as individual characters, so a run of 12 becomes "1", "2".

Examples

Example 1

Input: chars = ["a", "a", "b", "b", "c", "c", "c"]
Output: 6, chars[:6] = ["a", "2", "b", "2", "c", "3"]

Each consecutive run is replaced by its character and count.

Example 2

Input: chars = ["a", "b", "c"]
Output: 3, chars[:3] = ["a", "b", "c"]

Runs of length one omit the count.

Constraints

  • 1 <= len(chars) <= 2 * 10^4
  • Each element of chars is a single ASCII character
  • Counts may contain multiple digits
  • Use O(1) auxiliary space

Function Signature

def compress(chars):
Interviewer

Your question is String Compression Coding. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.