Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Rate Violations

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

Your question is Sliding Window Rate Violations. 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

IBM API Connect must identify client IP addresses whose request rate exceeds a configured threshold. Given request records, return every IP with more than k requests in at least one sliding window of t seconds.

Formal Specification

Implement detect_rate_abuse(requests, k, t), where requests is a list of two-element lists [ip, timestamp]. ip is a string and timestamp is an integer number of seconds. The input records may be in any order.

A window is inclusive: requests at timestamps s and s + t belong to the same window. An IP is abusive if some window contains at least k + 1 of its requests. Return the abusive IP addresses once each, sorted lexicographically. Return an empty list if no IP exceeds the threshold.

Constraints

  • 0 <= len(requests) <= 2 * 10^5
  • 0 <= k <= len(requests)
  • 1 <= t <= 10^9
  • Timestamps are integers from 0 through 10^9
  • IP addresses are non-empty strings
  • Duplicate IP and timestamp pairs are allowed

Function Signature

def detect_rate_abuse(requests, k, t):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output