Alien Dictionary
Updated
Problem statement
You are given an array words sorted according to an unknown alphabet. Derive an ordering of every distinct lowercase letter that appears in words.
For each adjacent pair of words, the first position where their letters differ establishes which letter must come first. If one word is a prefix of the other, the shorter word must come first.
Return the lexicographically smallest valid ordering. Return an empty string when the ordering is impossible because of an invalid prefix or a cycle.
Function
alienOrder(words: String[]) → StringExamples
Example 1
words = ["wrt","wrf","er","ett","rftt"]return = "wertf"The adjacent pairs imply t < f, w < e, r < t, and e < r, so the ordering is wertf.
Example 2
words = ["za","zb","ca","cb"]return = "abzc"The constraints are a < b and z < c. Several topological orders are valid; abzc is lexicographically smallest.
Example 3
words = ["abc","ab"]return = ""The longer word appears before its exact prefix, so no alphabet can make the list sorted.
Constraints
1 <= words.length <= 500.1 <= words[i].length <= 100.- The total number of letters across all words is at most
10000. - Every word contains only lowercase English letters.
More Intuit Online Assessment questions
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
- Pressure-Isolation Shutdown OrderOA · Seen Sep 2026
- Submask Sum QueriesOA · Seen Sep 2026
Similar questions at other companies
- Alien DictionaryByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Infer a Lexicographically Smallest Alien AlphabetMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Alien DictionaryBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Alien DictionaryAmazon · ONSITE INTERVIEW
- Recover an Alien Alphabet from Sorted WordsOpenAI · PHONE SCREENOpenAI interview questions
- Alien Dictionary OrderGoogle · PHONE SCREENGoogle interview questions
- Letter Order from a Sorted Word ListRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Alien DictionaryAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Validate CSV References and Field DependenciesStripe · PHONE SCREENStripe interview questions