Infer Character Order From a Dictionary in an Unknown Script
Last updated: November 29, 2026
Quick Overview
You are given a list of words sorted lexicographically under an unknown alphabet. Return any ordering of the letters consistent with the list, or an empty string if the list is contradictory.
Meta
November 29, 20260
0
0 solved
You are given a list of words sorted lexicographically under an unknown alphabet. Return any ordering of the letters consistent with the list, or an empty string if the list is contradictory.
Alien dictionary is a recognized Meta onsite Hard combining careful constraint extraction with topological sort. The interviewer checks that you derive edges only from the first differing character of adjacent words, catch the invalid prefix case, and detect cycles.
What the Interviewer Expects
- Compares only adjacent word pairs
- Takes an edge from the first differing character only
- Rejects a longer word listed before its own prefix
- Includes letters that have no constraints
- Runs topological sort with cycle detection
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return whether the ordering is unique.
- Return all valid orderings.
- The dictionary is huge and streamed; can you build the graph incrementally?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Words contain lowercase letters only; up to 100 words of length up to 100. Every letter that appears must be in the output. Output any valid order or an empty string....
High-Level Design
Collect every letter as a node. For each adjacent pair, find the first position where they differ and add an edge from the earlier word's letter to the later word's letter; later characters carry no i...