Order Server Log Lines With Text Bodies Before Numeric Ones
Last updated: November 29, 2026
Quick Overview
Each log line is an identifier followed by a space-separated body; text logs have words in the body and numeric logs have only digits. Return the logs with all text logs first, sorted by body and then identifier, followed by numeric logs in their original order.
0
0
0 solved
Each log line is an identifier followed by a space-separated body; text logs have words in the body and numeric logs have only digits. Return the logs with all text logs first, sorted by body and then identifier, followed by numeric logs in their original order.
A common Google online assessment problem about custom sort keys over parsed strings. The interviewer checks that you split each line exactly once, design a stable composite key, and preserve the original order of numeric logs.
What the Interviewer Expects
- Splits on the first space only to separate identifier and body
- Classifies a line by the first character of its body
- Sorts text logs with a composite key of body then identifier
- Keeps numeric logs stable in arrival order
- Gives O(n log n) complexity in terms of comparisons and string length
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you sort a 500 GB log file that does not fit in memory?
- Add a third class of logs, such as error logs that must go first.
- Why is Python's sort being stable relevant here, and how would you get the same guarantee in C++?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of lines like
"a1 9 2 3"or"g1 act car". Each body is non-empty and either all words or all digits. - Output: text logs sorted by
(body, identifier), then numeric logs in original...
High-Level Design
Partition the lines into two buckets in one pass. For text logs, store a tuple (body, identifier, original_line) so a plain sort orders by body first and breaks ties by identifier. Numeric logs are ...