FastPrepLength-Prefixed String Codec

Length-Prefixed String Codec

Snowflake logoSnowflake● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Encode a list of strings into one string and then decode it without loss.

Use the deterministic length-prefix format length#value for each input string, concatenated with no separator. Return an array whose first element is the encoded string and whose remaining elements are the strings obtained by decoding it.

Lengths are measured in characters. The character # and decimal digits may appear inside values.

Function

encodeAndDecodeStrings(strings: String[]) → String[]

Examples

Example 1

strings = ["lint","code"]return = ["4#lint4#code","lint","code"]

Each value is preceded by its character count and a hash delimiter.

Example 2

strings = ["","a#b","12"]return = ["0#3#a#b2#12","","a#b","12"]

Empty strings and delimiter characters remain unambiguous because decoding trusts the length.

Example 3

strings = []return = [""]

An empty list encodes to the empty string and decodes to no values.

Constraints

  • 0 <= strings.length <= 10000.
  • 0 <= strings[i].length <= 10000.
  • The total number of input characters is at most 200000.
  • Each string contains printable ASCII characters.

More Snowflake problems

See Snowflake hiring insights
public String[] encodeAndDecodeStrings(String[] strings) {
    // Write your solution here.
}
strings["lint","code"]
expected["4#lint4#code", "lint", "code"]
Checking account…