FastPrepEncode and Decode a String Stream
Problems/Encode and Decode a String Stream

Encode and Decode a String Stream

Harness logoHarness● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report describes a string encoding and decoding question. We reuse the function signature, the examples, and the limits from an equivalent task we already reviewed. Our version is about 95% the same as the reported question.

Implement both directions of a length-prefixed string-stream protocol.

  • When operation is "ENCODE", encode values. Return a one-element array containing the encoded stream.
  • When operation is "DECODE", decode stream. Return the decoded strings and ignore values.

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

  • operation is either ENCODE or DECODE.
  • 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, stream is valid.

More Harness interview questions

  • Streaming Ingredient DishesONSITE INTERVIEW · Seen Jul 2026
  • Minimum MovesOA · Seen Oct 2026
  • Choose a Road to Maximize Average Route Toll IncreasePHONE SCREEN · Seen Sep 2026

Similar questions at other companies

  • Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
  • Encode and Decode a String StreamPostman · ONSITE INTERVIEWPostman interview questions
  • 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
  • Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
  • Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
See Harness hiring insights
Saved
class Solution {
    public String[] transformStringStream(String operation, String[] values, String stream) {
        // write your code here
    }
}
Checking account…