Alien Dictionary
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole Bloomberg Alien Dictionary question, reported in February 2021, hinges on one data structure: a directed graph of letter precedences, which you then topologically sort. If you see "unknown order" and "sorted words" in the same prompt, that's the signal. This version adds a twist: you must return the lexicographically smallest valid ordering, not just any one. So a plain queue won't cut it. If you blank on the setup during the live OA, StealthCoder runs invisibly as a safety net and gives you the approach in real time.
The problem
An alien language uses the lowercase English letters, but their order is unknown. You are given an array words that is intended to be sorted from smallest to largest according to that alien alphabet. Return the lexicographically smallest ordering of every distinct character that makes the dictionary order valid. If no such ordering exists, return the empty string. For two different words, compare characters from left to right. At the first position where they differ, the character in the earlier word must come before the character in the later word. If all compared characters match, the shorter word must come first. Each precedence relationship must be counted only once, even if multiple adjacent word pairs imply it. Function alienOrder(words: String[]) → String Examples Example 1 words = ["wrt","wrf","er","ett","rftt"] return = "wertf" The adjacent pairs establish w < e, e < r, r < t, and t < f, so the only valid order is wertf. Example 2 words = ["za","zb","ca","cb"] return = "abzc" The constraints are a < b and z < c. Several orders are valid, and abzc is the lexicographically smallest one. Example 3 words = ["abc","ab"] return = "" A longer word cannot appear before its exact prefix in a sorted dictionary, so no valid alphabet exists. Constraints 1 <= words.length <= 500 1 <= words[i].length <= 100 The total number of characters across all words is at most 10^4. Every word contains only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Build the graph from adjacent word pairs only. For each pair, find the first differing character and add an edge from the earlier word's letter to the later word's letter. Dedupe edges with a set, since the problem says each relationship counts once. Otherwise your in-degrees get inflated and the sort breaks. Then run Kahn's algorithm, but swap the queue for a min-heap so the smallest available letter always goes next. That gives the lexicographically smallest order. Two pitfalls kill most attempts. First, the prefix case: if a longer word comes before its own prefix, like abc before ab, return the empty string immediately. Second, include every distinct letter as a node, even ones with no edges, or they vanish from the answer. If the output is shorter than the letter count, there's a cycle, so return empty. StealthCoder is your hedge if the heap tweak slips your mind mid-assessment.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Alien Dictionary 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
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 Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Alien Dictionary FAQ
What's the trick to Alien Dictionary?+
Treat letters as graph nodes and compare only adjacent words to create edges. Find the first differing character, add one edge, and stop. Then topologically sort. Here, use a min-heap instead of a plain queue so the result is the lexicographically smallest valid order.
How do I get the lexicographically smallest ordering?+
Use Kahn's algorithm with a min-heap of letters whose in-degree is zero. Pop the smallest, append it, decrement neighbors, and push any that hit zero. In Example 2, that produces abzc instead of another valid order like zabc.
What edge cases break most solutions?+
The prefix case, where a longer word precedes its prefix, must return an empty string. Duplicate edges from multiple pairs must be counted once. Letters with no constraints still need to appear in the output. Cycles show up as a result shorter than the distinct letter count.
How hard is this really for a Bloomberg OA?+
It's a medium-hard graph problem. The idea is standard, but the details trip people up: dedupe, prefix check, cycle detection, and the heap. With input capped at 10^4 total characters, efficiency isn't the issue. Correctness on the edge cases is.
How do I prepare in 48 hours?+
Write topological sort twice from scratch, once with a queue and once with a heap. Then code this problem end to end, including the prefix check and edge dedupe. Test with the three examples. Practice building the graph from adjacent pairs until it's automatic.