Problem. Design encode(strs) -> str and decode(s) -> list[str] so that decode(encode(strs)) == strs for
any list of strings, including empty strings and strings containing any character.
["hello", "world"] → encode → some string → decode → ["hello", "world"]
["a,b", "", "#3"] → …must also round-trip exactly
ML context. Every dataset of text has to be stored somehow: training shards, tokenised corpora, logged prompts. Formats like length-prefixed record files exist precisely because the naive approach (one record per line, or comma-separated) breaks the moment a record contains a newline or a comma. This lesson is the core of serialisation: making boundaries unambiguous.
This isn't about hashing directly; it's here because it's the classic "design a format" problem, and the length prefix is the same "store a small header so you never have to search" idea behind hash tables' O(1) jumps.
1. Step 1: Clarify
The empty list vs [""] case is a good test of any design: both must encode differently.