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
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
topological sort
graph construction
cycle detection
string comparison
alien dictionary
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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported