Code Room
CodingMediumcod-g1052
Subject Storage prefix compressionLevel Mid–Senior~25 minCommon in Storage & CDN · Algorithms & data structures interviewsIndustries Software development, Technology

Question

SSTable blocks shrink keys with front coding (prefix compression). Given a list of string keys sorted ascending, encode each key (after the first) as [shared, suffix] where `shared` is the length of the longest common prefix it shares with the IMMEDIATELY PRECEDING key, and `suffix` is the remaining characters of the key after that shared prefix. The first key is stored whole as [0, key]. To prove the encoding is lossless, DECODE it back by reconstructing each key as previous_key[:shared] + suffix, and return the decoded list of keys (which must equal the input). Keys total <= 10^5 characters.

Implement
prefix_compress_roundtrip(keys: list[str]) → list[str]
Examples
in[["apple","applet","apply"]]out["apple","applet","apply"]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.