Infer Letter Order from a Sorted Dictionary in an Unknown Script

Last updated: November 29, 2026

Quick Overview

Given a list of words sorted lexicographically according to an unknown alphabet, return any letter ordering consistent with that sort, or an empty string if the input is contradictory.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a list of words sorted lexicographically according to an unknown alphabet, return any letter ordering consistent with that sort, or an empty string if the input is contradictory.

Framed around supporting new scripts in Google Translate or Gboard, this tests extracting constraints from data and topologically sorting them. Interviewers focus on the precise rule (only the first differing letter of adjacent words), the invalid prefix case, and cycle detection.

What the Interviewer Expects
  • Compares only adjacent words and only at the first differing character
  • Rejects the invalid case where a longer word precedes its own prefix
  • Includes letters that appear but have no constraints
  • Uses Kahn's algorithm and detects cycles by output length
  • States O(total characters) complexity
Key Topics to Cover
Topological Sort
Graphs
Kahn's Algorithm
Strings
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.
  • Words arrive in a stream; update the ordering incrementally.
  • Some input words may be misspelled noise; find an order violating the fewest constraints.
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
  • Input: list of words in sorted order over an unknown alphabet.
  • Output: a string containing every letter that appears, in a consistent order; "" if impossible.
  • Any valid order is accepted....
High-Level Design

For each adjacent pair (w1, w2), find the first index where they differ; that gives one edge w1[j] -> w2[j] and nothing else (later letters carry no information). If no position differs and w1 is lo...


Submit Your Answer
Markdown supported