Recover the Symbol Order of a Custom Product Code Alphabet
Last updated: November 29, 2026
Quick Overview
A legacy catalog system sorts product codes using an unknown ordering of its symbols; given the codes in sorted order, return one valid ordering of all symbols, or an empty string if the list is inconsistent.
Amazon
November 29, 20260
0
0 solved
A legacy catalog system sorts product codes using an unknown ordering of its symbols; given the codes in sorted order, return one valid ordering of all symbols, or an empty string if the list is inconsistent.
A classic Amazon onsite graph problem disguised in a catalog-migration story. The interviewer checks whether you derive edges only from the first differing character of adjacent codes, catch the prefix trap, and detect contradictions with a topological sort.
What the Interviewer Expects
- Compare only adjacent codes and only at the first mismatch
- Detect the invalid case where a longer code precedes its own prefix
- Include symbols that never appear in any constraint
- Run topological sort and detect cycles
- Explain complexity in total characters C
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you tell whether the recovered order is unique?
- What if codes arrive as a stream and you must update the order incrementally?
- How would you return all valid orders?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a list of codes already sorted under the unknown order. Output: a string containing every distinct symbol in a valid order, or "" if no order is consistent. Assume lowercase letters; any vali...
High-Level Design
Each adjacent pair tells us at most one fact: at the first position where they differ, the earlier code's symbol comes before the later one's. Characters after that position say nothing. If no positio...