Magicsheet logo

Reorder Data in Log Files

Medium
42.3%
Updated 6/1/2025

Reorder Data in Log Files

What is this problem about?

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.

Why is this asked in interviews?

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).

Algorithmic pattern used

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).

Example explanation

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:

  • "art can" < "art zero" < "own kit dig"
  • Sorted: ["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"].

Common mistakes candidates make

  • Sorting digit-logs as well (they must keep their original order).
  • Using the full log string as the sort key instead of separating the identifier from the content.
  • Confusing which part is the identifier — it is always the first space-delimited token.
  • Not using a stable sort, which can reorder digit-logs with equal keys.

Interview preparation tip

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.

Similar Questions