Log entries and reports arrive out of order. Sorting them into a timeline shows what happened and how long detection and containment took.
What to build
Sort incident events by time and work out the time to detect and the time to contain.
Requirements
- Each line is
HH:MM tag description, where tag is one of start, event, detect, contain (exactly one start, detect and contain). - Print the events sorted by time (equal times keep input order) as
HH:MM description. - Then print
time to detect: X min(detect minus start) andtime to contain: Y min(contain minus detect). - All times are on the same day.
Input
One event per line.
Output
The timeline, then two lines.
Constraints
3 to 50 events.
Example
Input:
10:35 contain server isolated
09:00 start phishing email opened
10:10 detect alert on new-country login
09:40 event grades copied
Output:
09:00 phishing email opened
09:40 grades copied
10:10 alert on new-country login
10:35 server isolated
time to detect: 70 min
time to contain: 25 min
Why it works
Turning HH:MM into minutes makes times easy to sort and subtract, and the two differences measure the two things incident response tries to shrink: how long the attacker went unnoticed and how long they kept going after that.
Examples
Example 1 · Input
10:35 contain server isolated 09:00 start phishing email opened 10:10 detect alert on new-country login 09:40 event grades copied
Expected output
09:00 phishing email opened 09:40 grades copied 10:10 alert on new-country login 10:35 server isolated time to detect: 70 min time to contain: 25 min
Example 2 · Input
13:00 detect antivirus alert 12:58 start malware runs 13:05 contain laptop unplugged
Expected output
12:58 malware runs 13:00 antivirus alert 13:05 laptop unplugged time to detect: 2 min time to contain: 5 min
Hints
Hint 1
minutes = hours × 60 + minutes.
Solution
Show one correct solution (try it yourself first)
const events = [];
let line = prompt();
while (line !== null) {
if (line.trim()) {
const [time, tag, ...words] = line.trim().split(/\s+/);
const [h, m] = time.split(":").map(Number);
events.push({ at: h * 60 + m, time, tag, text: words.join(" ") });
}
line = prompt();
}
events.sort((a, b) => a.at - b.at);
for (const e of events) console.log(`${e.time} ${e.text}`);
const at = (tag) => events.find((e) => e.tag === tag).at;
console.log(`time to detect: ${at("detect") - at("start")} min`);
console.log(`time to contain: ${at("contain") - at("detect")} min`);
▶ Open this solution in the compiler, then press Visualize to watch it run step by step