Skip to main content

Reorder Data in Log Files - Solution & Explanation

MediumArrayStringSorting13 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

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:

  1. The letter-logs come before all digit-logs.
  2. The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  3. The digit-logs maintain their relative ordering.

Return the final order of the logs.

 

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"]
Explanation:
The letter-log contents are all different, so their ordering is "art can", "art zero", "own kit dig".
The digit-logs have a relative order of "dig1 8 1 5 1", "dig2 3 6".

Example 2:

Input: logs = ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

 

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.

Approach Overview

Problem Overview: You receive an array of log strings where each log starts with an identifier followed by words. Some logs contain letters (letter-logs) while others contain digits (digit-logs). The task is to reorder the logs so all letter-logs come before digit-logs. Letter-logs are sorted lexicographically by content, and if contents match, by identifier. Digit-logs keep their original order.

Approach 1: Custom Sorting Using Comparator (O(n log n) time, O(n) space)

This approach treats the problem as a specialized sorting task. Each log is parsed into two parts using a split operation: the identifier and the content. A custom comparator determines ordering rules. If both logs are letter-logs, compare their contents first; if contents match, compare identifiers. If one is a digit-log and the other is a letter-log, the letter-log always comes first. If both are digit-logs, maintain their original order by treating them as equal in sorting priority.

The key insight is encoding the ordering rules inside the comparator instead of manually rearranging elements. Most languages allow custom sort keys or comparator functions, making the implementation concise. This approach is commonly implemented with built-in sort() utilities and works well for problems involving structured ordering with multiple conditions. Time complexity is O(n log n) due to sorting, and space complexity is O(n) depending on the sorting implementation.

This technique relies heavily on understanding sorting behavior and string parsing with string operations.

Approach 2: Two-Pass Solution with In-place Operations (O(n log n) time, O(1) extra space)

This method separates the problem into two clear phases. First pass: iterate through the array and partition logs into letter-logs and digit-logs. Since digit-logs must preserve their relative order, they are simply collected in sequence. Letter-logs are stored in the front portion of the array or a temporary list. Second pass: sort only the letter-logs based on their content and identifier.

After sorting the letter-logs, append the digit-logs back in their original order. Because digit-logs are never reordered internally, their stability is preserved automatically. This approach avoids unnecessary comparisons between digit-logs and focuses sorting only on the relevant subset.

The time complexity remains O(n log n) due to sorting the letter-logs. However, practical performance improves when many logs are digits because fewer elements are sorted. Space complexity can be reduced to O(1) if the partitioning is done in-place within the array. This solution demonstrates careful manipulation of arrays and stable ordering constraints.

Recommended for interviews: The custom comparator approach is what most interviewers expect. It directly models the ordering rules in code and is easy to reason about during discussion. The two-pass strategy shows deeper control over array manipulation and stability but is usually longer to implement. Mentioning both demonstrates understanding of sorting mechanics and trade-offs.

Approach 1: Custom Sorting Using Comparator

This approach involves separating the logs into letter-logs and digit-logs. We then use a custom sorting function to sort the letter-logs based on their content first and their identifiers second, before finally appending the digit-logs to the sorted letter-logs.

  1. Separate the logs into letter-logs and digit-logs.
  2. Use a custom compare function to sort the letter-logs. The comparator should prioritize the contents, and use identifiers as a secondary criteria.
  3. Concatenate the sorted letter-logs with the original digit-logs to form the final result.

This solution defines a function which separates the logs into letter-logs and digit-logs. It uses Python's built-in sort functionality with a custom sorting key. The key first compares everything after the identifier, and then by identifier itself.

Code

Python

Java

JavaScript

Complexity

Time complexity is O(M*N*logN), where N is the number of logs and M is the maximum length of a single log. Sorting the letter logs is the most time-consuming operation.
Space complexity is O(N), for storing the letter-logs and digit-logs separately.

Try this approach in the editor →

Approach 2: Two-Pass Solution with In-place Operations

This approach intends to utilize a two-pass operation, where in the first pass it processes and categorizes letter-logs and digit-logs, and in the second pass it performs an in-place sort on the letter logs. Lastly, it reconstructs the original array by appending digit-logs after the sorted letter-logs.

  1. Initialize two pointers for managing and separating letter-logs and digit-logs.
  2. In the first pass through the logs, reorder them so that letter and digit logs are grouped. Keep track of the end of the letter-logs.
  3. Sort the letter logs in place using a custom sorting function.
  4. Return the merged and sorted logs array.

The solution in C utilizes two pointers to distinguish between digit and letter logs and uses qsort for in-place sorting of letter logs. The custom compare function accurately handles comparison based on the directive content and identifier.

Code

C

C#

Complexity

Time complexity is O(M*N*logN) due to the sorting of letter-logs using qsort.
Space complexity is O(1), because sorting and operations are done in place.

Try this approach in the editor →

Approach 3: Custom Sorting

We can use a custom sorting method to divide the logs into two categories: letter logs and digit logs.

For letter logs, we need to sort them according to the problem requirements, i.e., first by content and then by identifier.

For digit logs, we only need to maintain their original relative order.

The time complexity is O(n times log n), and the space complexity is O(n). Where n is the number of logs.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Custom Sorting Using Comparator

Time complexity is O(M*N*logN), where N is the number of logs and M is the maximum length of a single log. Sorting the letter logs is the most time-consuming operation.
Space complexity is O(N), for storing the letter-logs and digit-logs separately.

Two-Pass Solution with In-place Operations

Time complexity is O(M*N*logN) due to the sorting of letter-logs using qsort.
Space complexity is O(1), because sorting and operations are done in place.

Custom Sorting—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Custom Sorting Using ComparatorO(n log n)O(n)General case. Clean implementation using built-in sorting with custom comparison rules.
Two-Pass Partition + Sort Letter LogsO(n log n)O(1)Useful when minimizing extra memory or when digit-logs dominate the input.

Video Solution

Reorder Data in Log Files | coding interview question amazon | leetcode 937 • thecodingworld • 26,831 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reorder Data in Log Files easy or hard?
The problem is generally classified as Medium difficulty. The main challenge is correctly implementing the ordering rules: sorting letter-logs by content and identifier while preserving the original order of digit-logs. Once the comparator logic is clear, the implementation is straightforward.
Reorder Data in Log Files Python/Java solution
In Python, the solution typically uses the built-in sort() function with a custom key that distinguishes letter-logs and digit-logs. In Java, developers implement a Comparator inside Arrays.sort(). Both implementations follow the same rule set and run in O(n log n) time.
How to solve Reorder Data in Log Files in O(n)?
A strictly O(n) solution is not practical because letter-logs must be ordered lexicographically, which requires sorting. The closest efficient strategy is partitioning logs into letter and digit groups in O(n) time and then sorting only the letter-logs, resulting in O(n log n) overall complexity.
What is the best approach for Reorder Data in Log Files?
The most common solution uses a custom sorting comparator. Letter-logs are compared by content and then identifier, while digit-logs are always placed after letter-logs and keep their original order. This approach runs in O(n log n) time due to sorting and is concise in languages like Python, Java, and JavaScript.
Is Reorder Data in Log Files asked at Google/Amazon/Meta?
Reorder Data in Log Files is a well-known interview problem frequently reported in Amazon and Google interview preparation lists. It tests custom sorting logic, string parsing, and stability of ordering, which are common evaluation areas in coding interviews.
What data structure is used in Reorder Data in Log Files?
The problem mainly uses arrays (or lists) to store logs and relies on string parsing to separate identifiers from content. A custom comparator or sorting key is applied during sorting. No complex data structures are required beyond arrays and string operations.
What is the time complexity of Reorder Data in Log Files?
The typical solution runs in O(n log n) time because the logs must be sorted based on content and identifier. Each comparison involves string checks, but the dominant factor remains the sorting step. Space complexity is usually O(n) depending on the sorting implementation.

Ready to solve this problem?

Practice Reorder Data in Log Files with our built-in code editor and test cases.

Practice on FleetCode