Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top K Frequent Security Events
00:00
5 left

Top K Frequent Security Events

MediumPython

Problem

Given a list of log lines logs, where each line is a string in the format "timestamp source severity event_id message", and an integer k, return the k most frequent event_id values among lines whose severity is at least min_severity. Severity order is DEBUG < INFO < WARN < ERROR < CRITICAL. If two event IDs have the same frequency, return the lexicographically smaller one first. Ignore malformed lines.

Constraints

  • 1 <= len(logs) <= 10^5
  • 1 <= k <= 10^4
  • Each valid log line contains at least 5 space-separated fields
  • min_severity is one of DEBUG, INFO, WARN, ERROR, CRITICAL

Function Signature

def top_k_security_events(logs, k, min_severity):
Interviewer

Your question is Top K Frequent Security Events. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.