← 返回 microsoft 的题目列表Object-Oriented Design (Medium) + follow-up coding
类型:online_judge
Problem: Object-Oriented Design (Medium)
Design a class RateLimiter that limits how many requests a user can make within a fixed time window.
Requirements
Constructor: RateLimiter(limit: int, window_seconds: int)
Method: allow(user_id: str, timestamp: int) -> bool
timestamp is an integer (seconds). For the same user_id, timestamps are non-decreasing.
Consider the window (timestamp - window_seconds + 1) ... timestamp. If the number of requests by this user in this window is less than limit, allow and record this request and return True; otherwise return False.
Constraints
1 <= limit <= 1e5
1 <= window_seconds <= 1e9
Total allow calls <= 2e5
Distinct user_id <= 2e5
Example
RateLimiter(limit=3, window_seconds=10)
Calls:
allow("u1", 1) -> True
allow("u1", 2) -> True
allow("u1", 3) -> True
allow("u1", 4) -> False
allow("u1", 12) -> True
Implement the class (assume single-threaded).
Example
Input
RateLimiter(3,10)
allow u1 1
allow u1 2
allow u1 3
allow u1 4
allow u1 12
Output
True
True
True
False
True