Run-Length Encoding and Decoding
Updated
Problem statement
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 <= 100requests[i].length == 2, andrequests[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
1to3000. It decodes to at most3000characters. - All texts together have at most
6000characters, and all results together have at most6000characters.
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)