Encode and Decode Strings

MediumStringslength prefix encoding

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A client needs to send a list of strings over a channel that only carries a single string. The strings may contain **any** character, including # and digits, so joining them with a separator is ambiguous. The agreed encoding writes each string as its **length in decimal**, then a #, then the string's characters, and concatenates these pieces in order. For example ["ab", "#1"] becomes "2#ab2##1", and an empty list becomes "". Implement decode_strings(s), which takes a string produced by this encoding and returns the original list of strings in order. Be ready to explain why the length prefix makes decoding unambiguous where a plain delimiter does not, and how you would write the matching encoder.

Examples

Input: s = "5#hello5#world"
Output: ["hello","world"]
Read 5, skip #, take "hello"; then read 5 again and take "world".
Input: s = "0#3#a#b"
Output: ["","a#b"]
The first string is empty; the second has length 3 and itself contains a #.

Constraints

  • 0 <= s.length <= 2 * 10^5
  • s is a valid encoding of a list of 0 to 200 strings
  • each original string has length 0 to 200 and may contain any printable ASCII character

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.