Chronological Log Reversal

🎯 Level: Medium Score: 50 ⏱️ Estimated Time: 30 mins
📜 Problem Description

In a distributed system, log entries are crucial for debugging and monitoring. Imagine a scenario where a faulty logging agent sometimes records the key parameters of an event in reverse chronological or logical order within a single log line. Your task is to develop a utility that corrects these log entries by reversing the order of their essential segments (words) to restore the intended sequence.

A segment is defined as a sequence of non-space characters. In the corrupted log entries, segments are guaranteed to be separated by exactly one space. There will be no leading or trailing spaces in any log entry.

Example

Consider a corrupted log entry: "status initiated process 1234"

The correct, reversed order should be: "1234 process initiated status"

Input Format

The first line contains an integer T, denoting the number of test cases.

For each test case, a single line follows, containing the string s which represents a corrupted log entry.

Output Format

For each test case, output a single line containing the corrected log entry, with its segments reversed in order.

Constraints

  • 1 <= T <= 100
  • 1 <= length of s <= 10^4
  • Each character of s is either a lowercase English alphabet or a space.
📌 Constraints
1 <= T <= 100; 1 <= length of s <= 10^4
📝 Sample Input/Output
Sample Input Demonstration
3
event id 456 failure
system boot completed
alone
Step-by-step Explanation of Sample 1:

Input: "event id 456 failure"

  • The segments are: "event", "id", "456", "failure".
  • Reversing their order gives: "failure", "456", "id", "event".
  • Joining them back with a single space results in: "failure 456 id event".
Step-by-step Explanation of Sample 2:

Input: "system boot completed"

  • Segments: "system", "boot", "completed".
  • Reversed order: "completed", "boot", "system".
  • Joined: "completed boot system".
Step-by-step Explanation of Sample 3:

Input: "alone"

  • Segments: "alone".
  • Reversed order: "alone".
  • Joined: "alone".
Expected Output:
failure 456 id event
completed boot system
alone
💡 Hints
  • 💡 Consider how you can break down the input string into individual segments (words) based on the space delimiter.
  • 💡 Once you have the segments, think about how to reverse their order efficiently. Python lists offer a straightforward way to do this.
  • 💡 After reversing the order of segments, you'll need to reconstruct the final string, ensuring they are joined back with a single space as per the original structure.
📚 Topics

String & Tries

🏢 Asked By Companies

Adobe, Amazon, Apple, Cisco, Facebook, Goldman Sachs, MakeMyTrip, Microsoft, Paytm, Samsung, Sap Labs

😕 No submissions found

Start coding and your submissions will appear here.