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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Topological sort
Graphs
Strings
Cycle detection
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported