Question presented to candidate: "You are given a list of words from an alien language, and the list is sorted in that language's alphabetical order. The letters are ordinary lowercase letters, but their order is unknown. Return a string with one possible ordering of the letters, or an empty string if no ordering is consistent with the list. Please write it in Python."
What a strong answer should cover:
- Only adjacent words are compared, and only their first differing letters give a rule (an edge a -> b); letters after that carry no information.
- The prefix trap: a longer word directly before its own prefix makes the input invalid.
- A topological sort (Kahn) over the letters that appear, adding each edge once; a cycle means contradictory rules and the answer is "".
- Complexity O(C + U^2) with C total characters and U at most 26 letters, and how to tell whether the order is unique.
- Python specifics:
zip(words, words[1:])for adjacent pairs,for ... elseto detect "no difference found",ord(ch) - 97indexing,collections.deque, and"".jointo build the answer.
Clarifying questions expected:
- "Is any valid ordering acceptable, or do you want a specific one?"
- "What should I return if the list is contradictory or a word comes before its own prefix?"
- "Are all characters lowercase a to z?"
Code / implementation expected: Yes โ alienOrder(words) in Python: build edges from adjacent pairs, then Kahn's algorithm.