Pack a List of Arbitrary Strings Into One RPC Payload

Last updated: November 29, 2026

Quick Overview

Write encode(strings) that turns a list of arbitrary strings (which may contain any character, including delimiters) into one string, and decode(s) that recovers the exact original list.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Write encode(strings) that turns a list of arbitrary strings (which may contain any character, including delimiters) into one string, and decode(s) that recovers the exact original list.

Framed around a simplified wire format for an internal RPC like Stubby or gRPC, where strings can contain anything. The interviewer checks that you reject naive delimiter joins, pick length prefixing or escaping, and reason about round-trip correctness including empty strings and an empty list.

What the Interviewer Expects
  • Explains why joining on a delimiter breaks on adversarial input
  • Uses length-prefixed framing (or escaping) and justifies it
  • Distinguishes an empty list from a list with one empty string
  • Decodes in a single linear scan
  • Discusses how real formats like protobuf use varints
Key Topics to Cover
Serialization
Length-Prefix Framing
Strings
Parsing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Make the length prefix a variable-length integer; how do you encode and decode it?
  • How would you support streaming decode when the payload arrives in chunks?
  • What are the trade-offs between escaping and length prefixing?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • encode(list[str]) -> str and decode(str) -> list[str] with decode(encode(x)) == x for every list x.
  • Strings may contain any character, including digits, '#', newlines, or be empty.
  • The emp...
High-Level Design

Joining with a separator fails as soon as a string contains that separator. Escaping works but makes both sides fiddly. Length-prefix framing is the robust choice: write each string as len + '#' + s...


Submit Your Answer
Markdown supported