Alien Dictionary
Updated
Problem statement
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[]) → StringExamples
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 <= 5001 <= words[i].length <= 100- The total number of characters across all words is at most
10^4. - Every word contains only lowercase English letters.
More ByteDance Phone Screen questions
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Infer a Lexicographically Smallest Alien AlphabetMicrosoft · PHONE SCREEN · ONSITE INTERVIEW
- Alien DictionaryBloomberg LP · ONSITE INTERVIEW
- Alien DictionaryAmazon · ONSITE INTERVIEW
- Recover an Alien Alphabet from Sorted WordsOpenAI · PHONE SCREEN
- Alien Dictionary OrderGoogle · PHONE SCREEN
- Letter Order from a Sorted Word ListRippling · PHONE SCREEN · ONSITE INTERVIEW
- Alien DictionaryAirbnb · PHONE SCREEN · ONSITE INTERVIEW
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEW
- Validate CSV References and Field DependenciesStripe · PHONE SCREEN
- Password From Ordering HintsRogo · ONSITE INTERVIEW
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience