Problem · Array
Encode and Decode a String Stream
Learn this problemProblem 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 problems
- 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