Code RoomAlien dictionary
HardPrep Room Coding #1473

Alien dictionary

CodingAlgorithms & data structuresSenior–Staff~35 min

You are given a list of words sorted lexicographically according to the rules of an unknown alien language that uses lowercase English letters. Derive any valid ordering of the alphabet consistent with the input. The order between two letters is implied by the first position at which two adjacent words differ. Return a string of the letters in a valid order; if no valid order exists (a contradiction or the prefix rule is violated, e.g. ['abc','ab']), return the empty string. If multiple orders are valid, return any one of them.

Implement
alien_order(words: list[str]) → str
Examples
in[["wrt","wrf","er","ett","rftt"]]out"wertf"
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[["wrt","wrf","er","ett","rftt"]]"wertf"not run yetsample