Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Parse Counters and Flag Anomalies

MediumPython00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the Python screen.

The question is on your right: Parse Counters and Flag Anomalies. Read through the requirements first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

At Datadog, performance counters are emitted as log lines in the format name=value. Write a function that parses a list of counter strings and flags anomalous counters based on recent history.

A line is valid only if it contains exactly one = and the value is a valid integer. Ignore invalid lines. For each counter name, keep the sequence of parsed values in input order. A counter value is considered anomalous if it is at least threshold greater than the average of the previous window_size valid values for the same counter. If fewer than window_size previous values exist for that counter, do not evaluate anomaly status for that occurrence.

Return a list of anomalies in the order they are detected. Each anomaly should be represented as [name, index, value], where index is the original position in the input list.

Formal Specification

  • Input: lines as a list of strings, window_size as an integer, threshold as an integer
  • Output: A list of [counter_name, original_index, counter_value]

Constraints

  • 1 <= len(lines) <= 10^5
  • 1 <= window_size <= 10^4
  • 0 <= threshold <= 10^9
  • Each valid line has exactly one '=' and a non-empty counter name
  • Parsed integer values are in the range [-10^9, 10^9]

Function Signature

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