FastPrepRun-Length Encoding and Decoding
Problems/Run-Length Encoding and Decoding

Run-Length Encoding and Decoding

Airbnb logoAirbnb● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported from an Airbnb phone screen for a full-time machine learning engineer role, in the candidate's second try at the company. The report names the coding task only as run-length encoding plus decoding, and says the candidate finished it right at the end of the time.

The report gives no format, function, edge cases or limits. We chose the count-then-letter token format that our other Airbnb decoding problem uses, lowercase letters only so a digit is always part of a count, a count on every run even when it is 1, one list of encode and decode requests in a single function, decode inputs that are always valid (neighbouring tokens may share a letter), empty text for empty input, the limits, and every example.

Our version is about 75% the same as the reported question.

Run-length encoding writes a text as a series of runs. A run is a group of the same letter in a row, made as long as possible. Each run is written as its length in decimal digits, followed by the letter. For example, aaabbc has the runs aaa, bb and c, so it encodes to 3a2b1c. Decoding goes the other way: 3a2b1c decodes to aaabbc.

You are given a list of requests, requests. Each requests[i] = [kind, text]. If kind is "encode", the result is the run-length encoding of text. If kind is "decode", the result is the decoded form of text. Return the results in the same order as the requests.

A text to encode uses only lowercase English letters. A run of length 1 still gets its count, so c encodes to 1c. A text to decode is a series of tokens. Each token is a positive count with no leading zeros, followed by one lowercase letter. To decode, write each token's letter as many times as its count, and join the tokens in order. Two tokens next to each other may use the same letter: 2a3a decodes to aaaaa. An empty text gives an empty result for both kinds.

Function

runLengthCodec(requests: String[][]) → String[]

Examples

Example 1

requests = [["encode","aaabbc"],["decode","3a2b1c"]]return = ["3a2b1c","aaabbc"]

aaabbc has a run of three a, then two b, then one c, so it encodes to 3a2b1c. Decoding 3a2b1c writes those runs back out and gives aaabbc.

Example 2

requests = [["encode","abcd"],["encode","zzzzzzzzzzzz"],["decode","12z1y"]]return = ["1a1b1c1d","12z","zzzzzzzzzzzzy"]

Every letter of abcd is its own run, and a run of one still gets its count, so the result is 1a1b1c1d. Twelve z in a row give the two-digit count 12z. When decoding 12z1y, read all the digits 12 before the letter.

Example 3

requests = [["encode",""],["decode","2a3a1b"],["encode","aabaa"]]return = ["","aaaaab","2a1b2a"]

The empty text encodes to the empty string. In 2a3a1b two tokens next to each other use the same letter; decoding just writes each one out, giving aaaaab. In aabaa the two blocks of a are split by a b, so they are separate runs: 2a1b2a.

Constraints

  • 1 <= requests.length <= 100
  • requests[i].length == 2, and requests[i][0] is "encode" or "decode".
  • 0 <= requests[i][1].length <= 3000
  • A text to encode has only lowercase English letters.
  • A text to decode is empty or made only of tokens as described above, each with a count from 1 to 3000. It decodes to at most 3000 characters.
  • All texts together have at most 6000 characters, and all results together have at most 6000 characters.

More Airbnb Phone Screen questions

  • Merge Event PreferencesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
  • Text JustificationPHONE SCREEN · Seen Nov 2026
  • Decode and Store StringsPHONE SCREEN · Seen Oct 2026
  • Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
  • Smallest Common RegionPHONE SCREEN · Seen Aug 2026
  • Longest Chain BookingPHONE SCREEN · Seen Aug 2026
  • Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
  • Parse Query StringPHONE SCREEN · Seen May 2026

Similar questions at other companies

  • Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
  • Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
  • Reverse a StringAmazon · PHONE SCREEN
  • Reverse a String ManuallyNewgen Software · PHONE SCREEN
  • Reverse a String with Two PointersSnowflake · PHONE SCREENSnowflake interview questions
  • Palindrome with One DeletionWhatNot · PHONE SCREENWhatNot interview questions
  • Valid Word AbbreviationMeta · PHONE SCREENMeta interview questions
  • Reverse a StringPersistent Systems · PHONE SCREEN
  • Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Valid Palindrome IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions

Airbnb interview experiences

  • Airbnb OA · November 2026
  • Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
  • Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
  • Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
  • Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
  • Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)
All Airbnb interview questionsSee Airbnb hiring insights
Saved
class Solution {
    public String[] runLengthCodec(String[][] requests) {
        // Write your code here.
    }
}
Checking account…