Skip to main content

937. Reorder Data in Log Files

mediumAsked at Amazon

Sort log lines so letter-logs come before digit-logs, letter-logs are sorted by content (with identifier as tiebreaker), and digit-logs preserve original order. Amazon asks this to test whether you can implement a stable, multi-key comparator without bugs.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Amazon loops.

  • Glassdoor (2026-Q1)Amazon SDE I/II onsite reports cite this as a famously recurring Amazon-only problem.
  • Blind (2025-11)Recurring Amazon-only interview problem; long Amazon staple.

Problem

You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier. There are two types of logs: Letter-logs: All words (except the identifier) consist of lowercase English letters. Digit-logs: All words (except the identifier) consist of digits. Reorder these logs so that: The letter-logs come before all digit-logs. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers. The digit-logs maintain their relative ordering. Return the final order of the logs.

Constraints

  • 1 <= logs.length <= 100
  • 3 <= logs[i].length <= 100
  • All the tokens of logs[i] are separated by a single space.
  • logs[i] is guaranteed to have an identifier and at least one word after the identifier.

Examples

Example 1

Input
logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"]
Output
["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"]

Approaches

1. Custom comparator with stable sort (optimal)

Classify each log as letter or digit. Sort letters by (content, identifier); digits keep original order. Concatenate.

Time
O(N * L * log N)
Space
O(N * L)
function reorderLogFiles(logs) {
  const letters = [];
  const digits = [];
  for (const log of logs) {
    const firstSpace = log.indexOf(' ');
    const rest = log.slice(firstSpace + 1);
    if (rest[0] >= '0' && rest[0] <= '9') digits.push(log);
    else letters.push([log, rest, log.slice(0, firstSpace)]);
  }
  letters.sort((a, b) => {
    if (a[1] === b[1]) return a[2].localeCompare(b[2]);
    return a[1].localeCompare(b[1]);
  });
  return letters.map(x => x[0]).concat(digits);
}

Tradeoff: Partition first (O(N)), then sort the letters (O(N log N) * L per compare). The digit-logs keep input order — JS Array.push preserves order so concat after is enough.

Amazon-specific tips

Amazon interviewers grade this on whether you handle the multi-key comparator correctly. State the rules out loud BEFORE coding: (1) letters first, digits second; (2) letters sorted by content; (3) on content tie, sorted by identifier; (4) digits preserve input order. Forgetting any rule fails the test cases.

Common mistakes

  • Sorting digits by content — wrong, they keep input order.
  • Sorting letters by the full log including identifier — wrong, content first, identifier as tiebreaker.
  • Not detecting digit vs letter logs correctly (check the first char AFTER the identifier, not the identifier itself).

Follow-up questions

An interviewer at Amazon may pivot to one of these next:

  • What if logs could have mixed letters AND digits in content?
  • What if the comparator needed to be locale-aware?
  • What if logs were a stream?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why is the digit-log order preserved?

The problem says so explicitly. Lexicographically sorting digit-logs doesn't make sense — the digits are content, not order.

How do I detect digit-logs cleanly?

Look at the first character after the first space. Letter or digit determines the log type.