Implement encode/decode for list of strings
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's proficiency in string manipulation and serialization techniques, focusing on designing an invertible encode/decode scheme for lists of strings.
Constraints
- 0 <= number of strings <= 10^4
- The total number of characters across all input strings is at most 2 * 10^5
- Each original string may contain digits, spaces, and symbols, but it does not contain '#'
- For `decode`, the input string is guaranteed to be a valid encoding produced by the same scheme
Examples
Input: ("encode", ["foo", "lis", "jljl", "12345678901"])
Expected Output: "3foo#3lis#4jljl#1112345678901#"
Explanation: Each string is written as `<length><string>#`: `3foo#`, `3lis#`, `4jljl#`, and `1112345678901#`.
Input: ("decode", "3foo#3lis#4jljl#1112345678901#")
Expected Output: ["foo", "lis", "jljl", "12345678901"]
Explanation: Split at each `#`, then recover the length prefix inside each block.
Input: ("encode", [])
Expected Output: ""
Explanation: An empty list encodes to an empty string.
Input: ("decode", "")
Expected Output: []
Explanation: An empty encoded string represents no strings.
Input: ("encode", ["", "1", "12$3", "a b", "00"])
Expected Output: "0#11#412$3#3a b#200#"
Explanation: `"" -> 0#`, `"1" -> 11#`, `"12$3" -> 412$3#`, `"a b" -> 3a b#`, and `"00" -> 200#`.
Input: ("decode", "0#11#412$3#3a b#200#")
Expected Output: ["", "1", "12$3", "a b", "00"]
Explanation: This includes an empty string, strings starting with digits, spaces, and symbols.
Hints
- When decoding, first find the next `#`. Everything before it belongs to exactly one encoded block.
- If a block before `#` has total length `m`, and the length prefix has `d` digits, then the original string length must be `m - d`.