Count Blocked Rate-Limited Requests
Company: Render
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
This question evaluates understanding of rate-limiting logic, sliding-window time-based counting, and data structure design for tracking per-IP and per-(IP, host) request histories.
Company: Render
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given an in-memory request log for a web platform. Each log entry has:
timestamp
: an ISO-8601 UTC timestamp
request_ip
: the client IPv4 address
host
: the hostname being requested
The log is already sorted by time in ascending order, and you may assume it fits in memory.
A request is evaluated against a sliding window of T seconds. For a request at time t, only earlier requests with timestamps in the interval (t - T, t) are counted. A request that happened exactly T seconds earlier is not in the window.
Important rules:
Implement logic to count how many requests would be blocked by this rule:
x
is blocked if there have already been at least
IPReqMax
requests from the same IP in the previous
T
seconds.
In other words, the current request is blocked when:
count(previous requests from same IP in (t - T, t)) >= IPReqMax
Input log:
timestamp,request_ip,host
2024-01-01T00:00:00+00:00,127.0.0.1,example1.com
2024-01-01T00:00:01+00:00,127.0.0.1,example2.com
2024-01-01T00:01:02+00:00,127.0.0.1,example1.com
2024-01-01T00:01:03+00:00,127.0.0.1,example2.com
2024-01-01T00:01:04+00:00,127.0.0.1,example1.com
2024-01-01T00:02:04+00:00,127.0.0.1,example1.com
With IPReqMax = 1 and T = 60:
So the answer is 3 blocked requests.
Now add a second rule:
x
to host
h
is also blocked if there have already been at least
IPHostReqMax
requests from the same IP to the same host in the previous
T
seconds.
A request is blocked if either of these is true:
count(previous requests from same IP in (t - T, t)) >= IPReqMax
count(previous requests from same IP and same host in (t - T, t)) >= IPHostReqMax
Again, blocked requests still contribute to both counters for future requests.
Input log:
timestamp,request_ip,host
2024-01-01T00:00:00+00:00,127.0.0.1,example1.com
2024-01-01T00:00:01+00:00,127.0.0.1,example2.com
2024-01-01T00:00:10+00:00,127.0.0.1,example1.com
2024-01-01T00:00:11+00:00,127.0.0.1,example3.com
2024-01-01T00:01:04+00:00,127.0.0.1,example3.com
2024-01-01T00:01:11+00:00,127.0.0.1,example1.com
With IPReqMax = 3, IPHostReqMax = 1, and T = 60:
So the answer is 3 blocked requests.
Write an efficient function or program that, given the sorted request log and the parameters:
T
IPReqMax
IPHostReqMax
returns:
Aim for an approach that is efficient for large logs loaded into memory.
Quick Answer: This question evaluates understanding of rate-limiting logic, sliding-window time-based counting, and data structure design for tracking per-IP and per-(IP, host) request histories.