Infer an Alien Alphabet from Sorted Words

Quick Overview

Infer a valid alien alphabet from words already sorted in that language. Build precedence constraints from adjacent words, reject invalid prefix orderings and cycles, and produce the lexicographically smallest topological order when several answers are possible.

Infer an Alien Alphabet from Sorted Words

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given a list of words sorted according to an unknown alphabet. Return one valid ordering of every distinct character that appears in the words. If the ordering is invalid or impossible, return an empty string. When more than one ordering is valid, return the lexicographically smallest valid ordering under ordinary character order so the output is deterministic. ### Constraints & Assumptions - The list contains between 1 and 1,000 lowercase words. - The total number of characters across all words is at most 100,000. - Every distinct character must appear exactly once in a non-empty result. - A longer word appearing before its exact prefix makes the input invalid. ### Clarifications - Only the first differing character in each adjacent word pair creates an ordering constraint. - Duplicate constraints must not increase a character's in-degree more than once. - A cycle means no alphabet can satisfy the supplied ordering. ### Examples ```text words = ["wrt", "wrf", "er", "ett", "rftt"] output = "wertf" words = ["abc", "ab"] output = "" ``` ### Hints ```hint Compare neighboring words Find the first position where each adjacent pair differs; later positions do not add constraints. ``` ```hint Make ties deterministic Consider which topological-sort frontier structure always selects the smallest available character. ```

Quick Answer: Infer a valid alien alphabet from words already sorted in that language. Build precedence constraints from adjacent words, reject invalid prefix orderings and cycles, and produce the lexicographically smallest topological order when several answers are possible.

|Home/Coding & Algorithms/ByteDance
ByteDance logo
ByteDance
Aug 15, 2026, 12:00 AM
mediumSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

Problem

You are given a list of words sorted according to an unknown alphabet. Return one valid ordering of every distinct character that appears in the words. If the ordering is invalid or impossible, return an empty string. When more than one ordering is valid, return the lexicographically smallest valid ordering under ordinary character order so the output is deterministic.

Constraints & Assumptions

  • The list contains between 1 and 1,000 lowercase words.
  • The total number of characters across all words is at most 100,000.
  • Every distinct character must appear exactly once in a non-empty result.
  • A longer word appearing before its exact prefix makes the input invalid.

Clarifications

  • Only the first differing character in each adjacent word pair creates an ordering constraint.
  • Duplicate constraints must not increase a character's in-degree more than once.
  • A cycle means no alphabet can satisfy the supplied ordering.

Examples

words = ["wrt", "wrf", "er", "ett", "rftt"]
output = "wertf"

words = ["abc", "ab"]
output = ""

Hints

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...