Infer Character Order of a New Script From Sorted Hashtags

Last updated: November 29, 2026

Quick Overview

TikTok is adding hashtag search for a language whose alphabet order is unknown to you. Given a list of hashtags already sorted in that language's order, return one valid ordering of all characters that appear, or an empty string if the input is inconsistent.

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

0

0

0 solved


TikTok is adding hashtag search for a language whose alphabet order is unknown to you. Given a list of hashtags already sorted in that language's order, return one valid ordering of all characters that appear, or an empty string if the input is inconsistent.

Localization and search teams at TikTok deal with many scripts, and this onsite question tests whether you can derive constraints from adjacent sorted words and solve them with a topological sort. The interviewer pays close attention to the prefix edge case and to including characters with no constraints.

What the Interviewer Expects
  • Derives one ordering constraint from the first differing character of each adjacent pair
  • Detects the invalid case where a longer word comes before its own prefix
  • Includes every character seen, even those with no edges
  • Runs a topological sort and detects cycles
  • Explains why only adjacent pairs need comparison
Key Topics to Cover
topological sort
graph construction
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
  • How would you report all valid orderings, or say whether the ordering is unique?
  • What if the input list may contain a few mis-sorted entries due to bad data?
  • How would you handle multi-codepoint graphemes?
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 strings sorted by an unknown character order.
  • Output: a string with each distinct character once in a consistent order, or "" if impossible.
  • Any valid order is acceptable....
High-Level Design

Compare each adjacent pair of words. The first position where they differ gives an edge w1[i] -> w2[i]: that character comes first. Nothing after that position tells us anything. If there is no diff...


Submit Your Answer
Markdown supported