Encode and Decode a String Stream
Updated
Problem statement
Implement both directions of a length-prefixed string-stream protocol.
- When
operationis"ENCODE", encodevalues. Return a one-element array containing the encoded stream. - When
operationis"DECODE", decodestream. Return the decoded strings and ignorevalues.
Each value is encoded as its decimal character length, followed by #, followed by its exact characters. Values may be empty and may themselves contain #, digits, spaces, or commas.
For DECODE, the stream is guaranteed to be a valid encoding produced by this protocol.
Function
transformStringStream(operation: String, values: String[], stream: String) → String[]Examples
Example 1
operation = "ENCODE"values = ["api","","a#b"]stream = ""return = ["3#api0#3#a#b"]Lengths make the empty string and the embedded delimiter unambiguous.
Example 2
operation = "DECODE"values = []stream = "5#hello5#world0#"return = ["hello","world",""]Reading the decimal length before each delimiter recovers every original boundary.
Constraints
operationis eitherENCODEorDECODE.0 ≤ values.length ≤ 10^5- Strings contain printable ASCII characters.
- The total number of characters in the relevant input is at most
10^6. - For
DECODE,streamis valid.
More Postman interview questions
- Group Duplicate Files by ContentONSITE INTERVIEW · Seen Feb 2026
- Configuration SystemOA · Seen Sep 2020
- Large ResponsesOA · Seen Sep 2020
- Minimum Swaps to Sort an ArrayOA · Seen Aug 2020
- Validate IP AddressOA · Seen Aug 2020
- Without WhitespacesOA · Seen Sep 2019
- Maximum Laptop Rating in a Price RangeOA · Seen Aug 2019
Similar questions at other companies
- Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
- Encode and Decode a String StreamHarness · ONSITE INTERVIEW
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Hybrid Run-Length and Bit-Packing EncoderDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Encode and Decode a String StreamBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions