OTP Request Rate Limiter

Medium ⏱ 12 min 56% acceptance ★★★★★ 4.9
Allow at most 3 OTP requests per phone per 10-minute (600 s) sliding window. Write allow_otp(log, phone, now) where log is a dict phone → list of prior timestamps: return True and record now when under the limit (counting only timestamps within 600 s), else False (unrecorded). Prune old entries as you go.

Examples

Example 1
Input
allow_otp({'98x': [0, 100, 200]}, '98x', 300)
Output
False
Explanation

Three sends already sit inside the window.

Constraints

  • Sliding window, not calendar buckets.
  • Denied requests are not recorded.

Topics

Business Logicrate limiting

Companies

TwilioMSG91PhonePe

Hints

Hint 1

Filter the list to now - t < 600 first.

Hint 2

Append only on allow.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.