Coding problem
The last five errors
The task
The status page shows the five most recent errors from the server log, in the order they happened.
Complete LastFiveErrors(List<LogEntry> log). LogEntry is a record with the Second it was written, its Level and a Message. The log is in time order, oldest first. Return the last five entries whose Level is "error", the oldest of those five first. A log with fewer than five errors gives all of them. Levels are always one of "info", "warning" or "error". An empty list gives an empty list. The list and its entries are never null.
Signature
public record LogEntry(int Second, string Level, string Message); public static List<LogEntry> LastFiveErrors(List<LogEntry> log)
Examples
[(1, "error", "a"), (2, "info", "b"), (3, "error", "c"), (4, "error", "d"), (5, "error", "e"), (6, "error", "f"), (7, "error", "g")]→[(3, "error", "c"), (4, "error", "d"), (5, "error", "e"), (6, "error", "f"), (7, "error", "g")]. There are six errors, so the oldest one, a, is dropped.[(1, "info", "up"), (2, "error", "disk full")]→[(2, "error", "disk full")]. There is only one error, so it is all there is.
Limits: Up to 500,000 entries.
The tests check: an empty log, no errors, fewer than five errors, exactly five, the newest error last, entries after the last errors that are not errors, warnings not counting, and 500,000 entries in time.
Hints
The solution opens by itself when all tests pass. You remember more if you try first.
Sign in to see the solutionYour code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public record LogEntry(int Second, string Level, string Message);
public static class Solution
{
// The last five error entries of the log, oldest of the five first.
public static List<LogEntry> LastFiveErrors(List<LogEntry> log)
{
throw new NotImplementedException();
}
}Sign in to run and submit your code. It stays here while you do.