Infer a Lexicographically Smallest Alien Alphabet
Updated
Problem statement
An alien language uses the lowercase English letters, but their order is unknown. You receive an array words sorted according to that language.
Infer a character order that is consistent with the dictionary. Include every distinct character that appears in words exactly once.
- If several orders are valid, return the lexicographically smallest valid order.
- If a longer word appears before its exact prefix, or the inferred precedence rules contain a cycle, return the empty string.
Function
alienOrder(words: String[]) → StringExamples
Example 1
words = ["wrt","wrf","er","ett","rftt"]return = "wertf"The adjacent words imply w < e < r < t < f, so the order is unique.
Example 2
words = ["za","zb","ca","cb"]return = "abzc"The rules require a < b and z < c; choosing the smallest available character at each step gives abzc.
Example 3
words = ["abc","ab"]return = ""A longer word cannot precede its exact prefix in a valid sorted dictionary.
Constraints
1 <= words.length <= 1001 <= words[i].length <= 100- Every word contains only lowercase English letters.
- The total number of characters is at most
10^4.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Alien DictionaryByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Course Ordering with Concurrent UpdatesNuro · PHONE SCREENNuro interview questions
- Alien DictionaryBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Resolve Task DependenciesAmazon · 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
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)