KatInterview

Sliding-Window Traffic Throttle with Dual Limit Rules

Data Structure DesignAuto-graded · 14 test cases
MediumAuto-gradedData Structure Designsliding-windowdequehash-map

Last updated

Tests sliding-window counting over timestamped event logs using per-source and per-source-destination deque-based rate limiting.

Problem

You are building a traffic-analysis tool for a proxy server. The tool replays a request log and flags which requests would have been throttled under configurable rules.

Each entry in the log is a dictionary with three fields:

  • timestamp — an ISO-8601 UTC timestamp string (e.g. "2024-01-01T00:00:05+00:00")
  • request_ip — the client's IPv4 address as a string
  • host — the destination hostname as a string

The log is sorted in ascending order by timestamp and fits entirely in memory.


Throttle window semantics

For a request arriving at time t, the relevant history is every earlier request whose timestamp falls in the open interval (t - T, t), where T is the window length in seconds.

  • A request timestamped exactly T seconds before the current one is outside the window and does not count.
  • Requests with equal timestamps are evaluated in their original list order, so an earlier entry in the list counts as "already seen" when a later entry with the same timestamp is evaluated.

A throttled request still enters the history. Its timestamp is recorded and can cause future requests to be throttled.


Rule 1 — per-source limit

A request from IP address src is throttled when the number of previous requests (within the window) from that same IP is at least per_ip_limit:

count(requests from src in (t - T, t)) >= per_ip_limit

Your function must return the total number of requests throttled under this rule alone as the first element of its result.


Rule 2 — combined per-source and per-destination limit

A second rule adds a per-(source IP, host) ceiling. A request from IP src to host dst is also throttled when:

count(requests from src to dst in (t - T, t)) >= per_pair_limit

A request is throttled under the combined policy when either Rule 1 or Rule 2 (or both) fires. A request that triggers both rules is still counted once.

Your function must return the total number of requests throttled under the combined policy as the second element of its result.


What to return

Return a list of exactly two integers: [rule1_throttled, combined_throttled].


Constraints

  • 1 <= len(log) <= 10^5
  • 1 <= T <= 10^6 (seconds)
  • 1 <= per_ip_limit <= 10^4
  • 1 <= per_pair_limit <= 10^4
  • Timestamps are valid ISO-8601 strings with explicit UTC offsets or a trailing Z.
  • The log is sorted in non-decreasing order by timestamp; equal timestamps are possible.
  • Both rules use the same window length T.
  • The function must handle an empty log and return [0, 0].

Examples

Example 1

Input: count_blocked_requests([{'timestamp': '2024-01-01T00:00:00+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}, {'timestamp': '2024-01-01T00:00:01+00:00', 'request_ip': '127.0.0.1', 'host': 'example2.com'}, {'timestamp': '2024-01-01T00:01:02+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}, {'timestamp': '2024-01-01T00:01:03+00:00', 'request_ip': '127.0.0.1', 'host': 'example2.com'}, {'timestamp': '2024-01-01T00:01:04+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}, {'timestamp': '2024-01-01T00:02:04+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}], 60, 1, 100)

Output: [3, 3]

Example 2

Input: count_blocked_requests([{'timestamp': '2024-01-01T00:00:00+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}, {'timestamp': '2024-01-01T00:00:01+00:00', 'request_ip': '127.0.0.1', 'host': 'example2.com'}, {'timestamp': '2024-01-01T00:00:10+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}, {'timestamp': '2024-01-01T00:00:11+00:00', 'request_ip': '127.0.0.1', 'host': 'example3.com'}, {'timestamp': '2024-01-01T00:01:04+00:00', 'request_ip': '127.0.0.1', 'host': 'example3.com'}, {'timestamp': '2024-01-01T00:01:11+00:00', 'request_ip': '127.0.0.1', 'host': 'example1.com'}], 60, 3, 1)

Output: [1, 3]

Hints

Hint 1

For each arriving request you need to know how many earlier requests from the same source (or source+destination) fall inside the open interval (t − T, t). A deque per key lets you evict expired entries from the front in O(1) amortized time — what comparison determines whether a timestamp has fallen out of the window?

Hint 2

Evict before you check the deque length, then append the current timestamp unconditionally afterward. Appending regardless of whether the request was throttled is critical — skipping it for blocked requests is the most common bug and will produce wrong counts.

Hint 3

Both answers can be produced in a single pass over the log. Rule 1 is a strict subset of the combined decision, so you never need to re-process any entry. For the combined count, use a logical OR of the two individual limit checks and increment a single counter — that way a request violating both rules is counted exactly once.