This question evaluates a developer's ability to implement real-time rule-based rate limiting, covering rule matching, time-windowed quota enforcement, state management for high-cardinality keys, and algorithmic/data-structure choices.
Design and implement a rule-based rate limiter that decides whether each incoming request should be allowed or rejected.
You are given:
first_name
last_name
ip
country
limit requests per window_seconds
for the matching key.
A request matches a rule if all attributes specified in the rule's filter equal the request's corresponding attributes. (Attributes not present in the filter are treated as wildcards.)
Implement a class/function with an API similar to:
bool allow(Request r, int timestamp_seconds)
Where timestamp_seconds is non-decreasing across calls.
R
.
N
.
timestamp_seconds
is strictly increasing or may have ties.
A rule: filter {ip="1.2.3.4"}, limit 5, window 60 means: for that IP, allow at most 5 requests in any 60-second window (or in the last 60 seconds, depending on window type you confirm).
Return true for allowed, false for rejected.