Interview question
Aggregate errors by minute
Aggregates log entries into per-minute error counts with deterministic output.
TL;DR
Aggregates log entries into per-minute error counts with deterministic output.
Grouping by time buckets, filtering, dictionaries, ordering, and observability-style data shaping.
Practice the problem like a real interview: restate, reason, implement, and test.
Implement CountErrorsByMinute(IEnumerable<LogEntry> logs). Only entries with level Error count. Truncate timestamps to the minute in UTC and return sorted buckets.
Input: two error logs at 10:05:03 and 10:05:59, one info log at 10:05:20
Output: 10:05 -> 2
Error counts.I would filter to error entries, convert timestamps to UTC, truncate seconds and smaller units, then group by that minute. I would return a DTO rather than exposing raw grouping types. This mirrors real observability tasks where exact bucket boundaries and deterministic ordering matter.
public sealed record LogEntry(DateTimeOffset Timestamp, string Level, string Message);
public sealed record ErrorBucket(DateTimeOffset Minute, int Count);
public static IReadOnlyList<ErrorBucket> CountErrorsByMinute(IEnumerable<LogEntry>? logs)
{
if (logs is null)
{
return Array.Empty<ErrorBucket>();
}
static DateTimeOffset TruncateToUtcMinute(DateTimeOffset timestamp)
{
var utc = timestamp.ToUniversalTime();
return new DateTimeOffset(
utc.Year,
utc.Month,
utc.Day,
utc.Hour,
utc.Minute,
0,
TimeSpan.Zero);
}
return logs
.Where(log => string.Equals(log.Level, "Error", StringComparison.Ordinal))
.GroupBy(log => TruncateToUtcMinute(log.Timestamp))
.OrderBy(group => group.Key)
.Select(group => new ErrorBucket(group.Key, group.Count()))
.ToList();
}
The grouping pass is O(n). Sorting buckets is O(k log k), where k is the number of minute buckets. Space is O(k).
10:05:00 and 10:05:59 to catch truncation.error lower-case if the rule says exact Error only.Next in Coding Practice: Version Compare