Message Serializer and Deserializer
Updated
Problem statement
Implement a way to turn an ordered list of text messages into one string and recover the same messages from it. Process each operation independently and return one row for each operation, in the same order.
["ENCODE", message1, message2, ...]: encode the messages into one string. Return a row containing only that string.["DECODE", encoded]: decode a valid encoded string. Return the decoded messages as one row, preserving their order and all characters.
To encode one message, write its character count in ordinary decimal notation, then #, then the message itself. Concatenate these records without extra separators. The decimal count has no leading zeros, except that zero is written as 0.
Messages may be empty and may contain digits, spaces and #. For example, ["ab#", ""] encodes as 3#ab#0#. An empty message list encodes as the empty string; decoding the empty string produces an empty row.
Every DECODE input is a valid complete encoding under these rules. You do not need to detect malformed data or maintain state between operations.
Function
processMessageCodec(operations: String[][]) → String[][]Examples
Example 1
operations = [["ENCODE","hello","#",""],["DECODE","5#hello1##0#"]]return = [["5#hello1##0#"],["hello","#",""]]The three messages have lengths 5, 1 and 0. Each record is the length, then #, then the message. The second message is itself #, so its record ends with two # signs in a row. When decoding, read the count and then copy exactly that many characters. So the # inside the message is never taken as a separator, and decoding gives back the same three messages.
Example 2
operations = [["ENCODE"],["DECODE",""]]return = [[""],[]]The ENCODE operation has no messages, so it returns the empty string. Decoding the empty string finds no records, so that row is empty.
Example 3
operations = [["DECODE","12#abcdefghijkl2#12"],["ENCODE","12","ab#"]]return = [["abcdefghijkl","12"],["2#123#ab#"]]In the DECODE row, the first count has two digits, so read both digits before the #. That gives the first message, abcdefghijkl. The next message is made of digits, but they are message text, not a count. In the ENCODE row, the # in ab# is copied as a normal character. So the rows are the two decoded messages and one encoded string.
Constraints
1 <= operations.length <= 2000.- Every operation has one of the two forms described above.
- Each message contains printable ASCII characters and has length at most
100000. - The total number of messages encoded or decoded across all operations is at most
20000. - The total length of all message and encoded-string arguments is at most
200000characters.
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Format a Signed Integer with CommasHudson River Trading · PHONE SCREENHudson River Trading interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Run-Length DecodingApple · PHONE SCREENApple interview questions
- Trace Pointer Rebinding and Pointee UpdatesArista Networks · PHONE SCREENArista Networks interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Decode and Store StringsAirbnb · PHONE SCREENAirbnb interview questions
- Delimiter-Parsing String Iteratoramo · PHONE SCREEN