Encode and Decode Strings
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Codec.encode(strings) and Codec.decode(encoded). Use length-prefixed framing so empty strings, digits, and delimiter characters round-trip without ambiguity.
Starter code
class Codec:
def encode(self, strings):
pass
def decode(self, encoded):
passTest cases
round-trip-words
{
"operations": [
"Codec",
"encode",
"decode"
],
"arguments": [
[],
[
[
"hello",
"world"
]
],
[
"5#hello5#world"
]
]
}Expected: [null,"5#hello5#world",["hello","world"]]
Wizard outline
- Step 1: Initialize Codec
Replace the empty starter with the first real state owned by Codec. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Pass the Wizard Stateful Decode One case
Complete the readable core algorithm for one representative Interview case. The parser must advance by the declared payload length rather than searching inside payload text.
- Step 3: Harden the Wizard Stateful Decode Embedded Delimiter boundary
Repair the reviewed boundary and pass the complete submission contract. Advancing to start + length preserves empty strings, digits, and embedded delimiters for every frame.
Footguns and prerequisites
- Joining on a delimiter alone fails when input strings contain that delimiter or when empty-list and one-empty-string must differ.
- strings
Reviewed references
Practice prerequisites
- Reverse One Slice Segment(opens in a new tab)
Reverse One Slice Segment isolates the result is the unchanged prefix, the reversed half-open segment, and the unchanged suffix in that exact order. That focused state discipline is required when implementing encode decode strings as a complete Interview Problem.
Recommended approach and implementation
Encode each value as len(value)#value. Decode by scanning digits to #, parsing the length, and consuming exactly that many following characters.
Why it works: The decimal prefix uniquely determines the payload boundary regardless of payload characters. The decoder advances by the complete frame each time, reconstructing every original string in order; an empty encoding contains zero frames.
class Codec:
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
def encode(self, strings):
return ''.join(str(len(value)) + '#' + value for value in strings)
def decode(self, encoded):
output = []
index = 0
while index < len(encoded):
separator = index
while encoded[separator] != '#':
separator += 1
length = int(encoded[index:separator])
start = separator + 1
output.append(encoded[start:start + length])
index = start + length
return output