Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Detect Anomalies with Sliding Window
00:00
5 left

Detect Anomalies with Sliding Window

MediumPython

Problem

Pearson MyLab records student interaction events in chronological order. Detect students whose activity exceeds an allowed interaction count within any fixed-size sliding time window.

An interaction log is represented as a list of two-item arrays: [timestamp, student_id]. Timestamps are integer seconds and the logs are sorted by nondecreasing timestamp. For each student, return the student ID if any window of window_size seconds contains more than max_events interactions.

Treat a window ending at timestamp t as the inclusive interval [t - window_size + 1, t]. Return the anomalous student IDs in lexicographic order. A student should appear at most once.

Formal Specification

Implement detect_anomalies(logs, window_size, max_events).

  • Input: logs, a list of [int, str] pairs; window_size and max_events, positive integers.
  • Output: a sorted list of student ID strings whose interaction count exceeds max_events in at least one window.

Constraints

  • 0 <= len(logs) <= 10^5
  • Each log entry has the form [timestamp, student_id]
  • 0 <= timestamp <= 10^9
  • 1 <= window_size <= 10^9
  • 1 <= max_events <= 10^5
  • Each student_id is a nonempty string
  • logs is sorted by nondecreasing timestamp

Function Signature

def detect_anomalies(logs, window_size, max_events):
Interviewer

Your question is Detect Anomalies with Sliding Window. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.