Implement Run-Length Encoding and Decoding for Strings

Quick Overview

A coding interview question asking you to implement run-length encoding and decoding for strings so that decoding always restores the original text. It tests multi-digit counts, the final run, malformed encoded input, and a format that stays unambiguous when the text itself contains digits.

Implement Run-Length Encoding and Decoding for Strings

Company: Airbnb

Role: Machine Learning Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

Implement run-length encoding and decoding for strings. `encode(s)` replaces every maximal run of one repeated character with the run's length and the character, and `decode(t)` turns an encoded string back into the original, so that `decode(encode(s)) == s` for every string the codec accepts. The source does not fix the encoded format. Unless the interviewer sets one, write each run as its length in decimal followed by the character, with nothing between runs: ```text encode("aaabccdddd") == "3a1b2c4d" decode("3a1b2c4d") == "aaabccdddd" ``` ```hint Test the edges Try your encoder on the last run of the input, on a run longer than nine characters, and on an input that contains digit characters. ``` ### Constraints and Clarifications - `encode("") == ""` and `decode("") == ""`. - A run can be long, so its count can have several digits: twelve `a` characters encode as `12a`. - Characters are compared exactly, so `a` and `A` form separate runs. ### Clarifying Questions - Can the input contain digits? If so, how should the format keep counts apart from data? - Should a single character carry a count of 1, or be written bare to keep the output short? - Should `decode` reject malformed input, such as a count with no character after it or a count of zero? - Is the input ASCII text, arbitrary Unicode, or raw bytes? ### What a Strong Answer Covers - Correct runs, including the final run and counts with several digits. - A decoder that parses multi-digit counts and reports malformed input. - A format that stays unambiguous when the data contains digits, with its size trade-offs. - Linear time in the input and output sizes, and round-trip tests. ### Follow-up Questions - What is the worst-case size of the encoded output relative to the input, and how would you change the format so that text without repeated characters does not grow much? - How would you encode and decode a stream that does not fit in memory? - How would you return the character at position i of the decoded text without decoding everything before it? - How would you encode binary data in which every byte value can appear?

Overview: A coding interview question asking you to implement run-length encoding and decoding for strings so that decoding always restores the original text. It tests multi-digit counts, the final run, malformed encoded input, and a format that stays unambiguous when the text itself contains digits.

Read the full Airbnb Machine Learning Engineer interview experience this question came from

Airbnb logo
Airbnb
Nov 13, 2026
hardMachine Learning EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Implement run-length encoding and decoding for strings. encode(s) replaces every maximal run of one repeated character with the run's length and the character, and decode(t) turns an encoded string back into the original, so that decode(encode(s)) == s for every string the codec accepts.

The source does not fix the encoded format. Unless the interviewer sets one, write each run as its length in decimal followed by the character, with nothing between runs:

encode("aaabccdddd") == "3a1b2c4d"
decode("3a1b2c4d") == "aaabccdddd"

Constraints and Clarifications

  • encode("") == "" and decode("") == "" .
  • A run can be long, so its count can have several digits: twelve a characters encode as 12a .
  • Characters are compared exactly, so a and A form separate runs.

Clarifying Questions Guidance

  • Can the input contain digits? If so, how should the format keep counts apart from data?
  • Should a single character carry a count of 1, or be written bare to keep the output short?
  • Should decode reject malformed input, such as a count with no character after it or a count of zero?
  • Is the input ASCII text, arbitrary Unicode, or raw bytes?

What a Strong Answer Covers Guidance

  • Correct runs, including the final run and counts with several digits.
  • A decoder that parses multi-digit counts and reports malformed input.
  • A format that stays unambiguous when the data contains digits, with its size trade-offs.
  • Linear time in the input and output sizes, and round-trip tests.

Follow-up Questions Guidance

  • What is the worst-case size of the encoded output relative to the input, and how would you change the format so that text without repeated characters does not grow much?
  • How would you encode and decode a stream that does not fit in memory?
  • How would you return the character at position i of the decoded text without decoding everything before it?
  • How would you encode binary data in which every byte value can appear?