Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detect Anomalies in Network Log Streams

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Detect Anomalies in Network Log Streams. Start with the requirements on the right.

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.

Problem

Zscaler telemetry pipelines receive network logs in stream order. Given a finite representation of that stream, identify events whose byte volume is unusually high compared with recent events from the same source IP.

For each log, compare bytes with the previous window_size logs having the same source_ip. An event is anomalous when it is greater than mean + z_threshold * standard_deviation of that prior window. Do not use the current event when computing statistics. If fewer than two prior events exist, the event is not anomalous. If the prior standard deviation is zero, treat a strictly larger value as anomalous.

Formal Specification

Implement detect_anomalies(logs, window_size, z_threshold). logs is a list of dictionaries with integer timestamp, string source_ip, and nonnegative integer bytes fields. Return a list of zero-based indices of anomalous logs in input order. Timestamps are nondecreasing, and logs from multiple source IPs may be interleaved.

Constraints

  • 1 <= len(logs) <= 10^5
  • 1 <= window_size <= len(logs)
  • 0 < z_threshold <= 10^3
  • 0 <= logs[i]["bytes"] <= 10^12
  • 1 <= len(logs[i]["source_ip"]) <= 64
  • Timestamps are nondecreasing
  • An event is evaluated only against earlier events from the same source IP

Function Signature

def detect_anomalies(logs, window_size, z_threshold):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output