Reorder Log Files
Filed under Amazon
Free read 1 of 3Separate logs into two lists: letter-logs and digit-logs.
The question
Reorder logs such that letter-logs come before digit-logs. Letter-logs are sorted lexicographically by content, then identifier. Digit-logs maintain original relative order.
Constraints
- Logs are strings
- Identifier is the first word
- Stable sort required for digit-logs
Examples
- Input
- ['a1 9 2 3', 'g1 act car', 'zo4 4 7']
- Output
- ['g1 act car', 'a1 9 2 3', 'zo4 4 7']
- Why
- Letter-logs sorted, digit-logs kept in order.
Approach
Separate logs into two lists: letter-logs and digit-logs.
Sort letter-logs using a custom key: (content, identifier).
Return the sorted letter-logs followed by the original digit-logs.
Tips
- What they are testing: Test with identical content logs and logs with different identifier lengths.
- Common mistake: Sorting digit-logs instead of keeping them in their original relative order.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.