The Reorder Data in Log Files interview question gives you an array of log strings. Each log starts with an identifier, followed by either words (letter-logs) or digits (digit-logs). You must reorder the logs such that all letter-logs come before digit-logs, letter-logs are sorted lexicographically by their content (using the identifier as a tiebreaker), and digit-logs maintain their original relative order.
This problem is asked at Meta, Snap, Amazon, and Snapchat because it tests custom sorting with multi-level comparator logic. In production systems, log processing and reordering are critical for monitoring and observability pipelines. The problem evaluates whether candidates can write a clean custom comparator and understand stable sort semantics (important for preserving digit-log order).
The pattern is custom comparator with stable sort. Separate logs into two groups: letter-logs and digit-logs. To sort letter-logs, define a sort key that returns a tuple of (content-after-identifier, identifier). Python's sorted() is stable, so digit-logs maintain their order by just appending them after the sorted letter-logs.
A single sorted() call with a key function also works: return a tuple (0, content, identifier) for letter-logs and (1,) for digit-logs (digit-logs sort after letter-logs while maintaining original order via stable sort).
Logs: ["dig1 8 1 5 1", "let1 art can", "dig2 3 6", "let2 own kit dig", "let3 art zero"]
Letter-logs: ["let1 art can", "let2 own kit dig", "let3 art zero"]
Digit-logs: ["dig1 8 1 5 1", "dig2 3 6"]
Sort letter-logs by content then identifier:
["let1 art can", "let3 art zero", "let2 own kit dig"]Result: ["let1 art can", "let3 art zero", "let2 own kit dig", "dig1 8 1 5 1", "dig2 3 6"].
For the Reorder Data in Log Files coding problem, the array and sorting interview pattern relies on a well-defined comparator. In Python, use str.split(None, 1) to split each log into exactly two parts: the identifier and the rest. Write the key function cleanly and test it with logs that share identical content but different identifiers. Interviewers at Meta and Amazon often ask "is your sort stable?" — know that Python's sorted() is always stable.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Groups of Special-Equivalent Strings | Medium | Solve | |
| Sort Features by Popularity | Medium | Solve | |
| High-Access Employees | Medium | Solve | |
| Minimum Time Difference | Medium | Solve | |
| Invalid Transactions | Medium | Solve |