Letter Order from a Sorted Word List
Updated
Problem statement
A made-up alphabet uses some lowercase English letters, but ranks them in its own order instead of a to z. You are given words, a list that should be sorted under that alphabet.
Two words are compared like in a normal dictionary: look at the first position where they differ, and the word whose letter there ranks lower comes first. If one word is a prefix of the other, the shorter word comes first. Equal words may sit next to each other.
Return a string that contains each letter that appears anywhere in words exactly once, ranked so that words is sorted. If more than one such string exists, return the one that comes first in ordinary a-to-z dictionary order. If no ranking can make words sorted, return an empty string "".
Function
alienOrder(words: String[]) → StringExamples
Example 1
words = ["dcb","dca","bd","ba"]return = "cdba"Comparing neighbors gives b before a (dcb, dca), d before b (dca, bd) and d before a (bd, ba). Letter c has no rule, so it can go first. The smallest valid order is "cdba".
Example 2
words = ["kite","kit"]return = """kit" is a prefix of "kite", so "kite" must come after it. No alphabet can fix that, so the answer is "".
Example 3
words = ["zy","yz","zz"]return = """zy" before "yz" needs z before y, and "yz" before "zz" needs y before z. Both cannot hold, so the answer is "".
Constraints
1 <= words.length <= 5001 <= words[i].length <= 20words[i]consists only of lowercase English letters.
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Alien DictionaryByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Select Transactions with Child-Pays-for-Parent PackagesCoinbase · PHONE SCREENCoinbase 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
- Parallel Courses IIIGoogle · ONSITE INTERVIEWGoogle interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Evaluate Formulas with Cycle DetectionInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
- Password From Ordering HintsRogo · ONSITE INTERVIEW