Design an algorithm to encode a list of strings into a single string and decode it back to the original list. The strings can contain any character, including delimiters like commas and colons.
For example, given ["hello", "world"], your encode function might produce "5#hello5#world", and your decode function would return ["hello", "world"]. Given ["he,llo", "wor:ld"], it should still work correctly.
Explain your encoding scheme, why it handles arbitrary characters, and analyze the time and space complexity.
How to approach it
- Hint 1
A simple delimiter like comma fails because the strings themselves might contain commas.
- Hint 2
If you know the length of each string, you can read exactly that many characters without ambiguity.
- Hint 3
Length-prefixed encoding: prepend each string with its length followed by a separator that cannot be confused with the data.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.