Classify an Alien Dictionary Ordering
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter's March 2022 OA includes an alien dictionary variant with a twist: you don't just find an order, you classify it. The input can hold up to 10000 words and 100000 total letters, so anything that compares all pairs or tries permutations is dead on arrival. This is a topological sort problem in disguise. You build precedence edges from adjacent words, then decide between three outputs: a unique order, AMBIGUOUS, or INVALID. If you've seen the classic version, the new part is the uniqueness check. If you haven't, the pattern is still learnable in one sitting.
The problem
words is intended to be sorted under an unknown alphabet. Derive precedence edges from the first differing character of every adjacent pair and include every observed character. Return the unique character order when exactly one topological ordering exists. Return AMBIGUOUS when multiple orders are valid. Return INVALID for a cycle or when a longer word precedes its exact prefix. Function alienDictionaryOrder(words: String[]) → String Examples Example 1 words = ["wdc","wdr","cd","dd","dr"] return = "wcdr" The adjacent differences force the unique order wcdr. Example 2 words = ["ab","ac"] return = "AMBIGUOUS" Only b before c is fixed, leaving a unconstrained. Constraints 0 <= words.length <= 10000 The total number of lowercase letters is at most 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is Kahn's algorithm with a queue-size check. Compare only adjacent word pairs, take the first differing character, and add an edge a to b once (dedupe edges or your indegrees get wrong). Register every character seen, even ones with no edges. Run Kahn's. If the queue ever holds more than one node at a time, multiple orders exist, so the answer is AMBIGUOUS, unless a cycle shows up anyway, and INVALID should win in that case. After the loop, if the output length is less than the character count, that's a cycle. The classic pitfall is the prefix case: if a longer word comes before its own prefix, like abc then ab, return INVALID right away. Also handle an empty input. Total work is linear in the letters, which fits the limits. If you blank on the live OA, StealthCoder is the safety net that can read the problem and hand you a working Kahn's setup.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Classify an Alien Dictionary Ordering cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as alien dictionary. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Classify an Alien Dictionary Ordering FAQ
What's the core trick in the ZipRecruiter alien dictionary question?+
Build a directed graph from the first differing character of each adjacent word pair, then run Kahn's topological sort. Uniqueness comes from watching the queue. If more than one character has indegree zero at any step, more than one valid order exists and you return AMBIGUOUS.
How do I detect INVALID versus AMBIGUOUS?+
INVALID has two causes: a cycle in the graph, or a longer word placed before its exact prefix. Check the prefix case while building edges. Detect a cycle when the sorted output is shorter than the number of distinct characters. Cycle or prefix violation always beats AMBIGUOUS.
Why can't I compare every pair of words?+
With up to 10000 words, all-pairs comparison is far too slow. Adjacent pairs already imply every constraint through transitivity. Comparing only neighbors keeps the work proportional to the total letters, which is capped at 100000.
What edge cases break most solutions?+
Duplicate edges inflating indegree, characters that appear but have no edges, an empty word list, and the prefix case like abc before ab. Also remember that every observed character must appear in the output, not only the ones that got edges.
How should I prepare in 48 hours?+
Write Kahn's algorithm from memory twice. Then add the three-way classification: queue size greater than one for AMBIGUOUS, short output for a cycle, and the prefix check. Test on the two examples plus a cycle case like ab, ba. That's enough to cover this question.