As reported: Morgan Stanley round 1 (Jun 2025): implement a rate limiter for incoming requests by IP address — each IP may make up to 10 requests per minute; once the limit is exceeded, further requests from that IP are denied until the minute window resets. A hash map keyed by IP holding a timestamp and a count was the approach taken.
Implement a RateLimiter class:
RateLimiter()creates the limiter.allow(ip, timestamp)is called for each incoming request and returnstrueif the request is allowed,falseif it is denied.
Each IP may make at most 10 allowed requests per 60-second window.
Example 1
requests from "1.2.3.4" at t = 0,1,2,3,4,5,6,7,8,9,10,59,60
-> true ×10, false (t=10), false (t=59), true (t=60)
The window opened at t = 0 covers 0 … 59; the 11th and 12th requests in it are denied; t = 60 opens a new window.
Example 2
"10.0.0.1" ×11 at t=5, "10.0.0.2" at t=6, "10.0.0.1" at t=64, "10.0.0.1" at t=65
-> true ×10, false, true, false, true
IPs are independent. The window of 10.0.0.1 opened at t = 5, so t = 64 is still inside it and t = 65 opens a new one.
Clarifications
- Timestamps are in seconds and never decrease from one call to the next.
- An IP's window starts at its first request when it has no open window, and covers
[start, start + 60). A request atstart + 60or later opens a new window and is counted as its first request. - Denied requests do not count toward the limit.
Constraints: up to 10^5 calls; 0 ≤ timestamp ≤ 10^9; IPs are strings of at most 15 characters.
Code runner format: two JSON values, one per line: the list of IPs, then the list of timestamps (request i is (ips[i], timestamps[i])). The harness creates one RateLimiter, calls allow for each request in order (in Go: Constructor() and Allow), and prints the list of results.