All Blind 75 questions

Encode and Decode Strings

FreeArrays & hashingMedium7 of 75

The problem

Design encode(words) and decode(data) so any list of strings can be converted to one string and recovered exactly. Strings may be empty or contain your delimiter.

Example

["a#b", "", "cat"] → "3#a#b0#3#cat" → original list

Need a hint?

A delimiter alone fails when the payload contains that delimiter.

Write pseudocode, trace the example, or note an edge case. This scratchpad does not run code.

Notes stay in this browser when storage is available.

Read the solution approach

Write each string’s length, a separator, then the string. To decode, read digits up to the separator, then consume exactly that many characters. Repeat until the encoded input ends. Use the same length unit in encoder and decoder, and collect pieces before joining to avoid quadratic concatenation.

Complexity

O(total payload length + length-prefix characters) time and space.

Before moving on, explain why the algorithm is correct and trace a boundary case without looking at the approach.