API Rate Limiter?
Short answer: public class RateLimiter { private readonly int limit; private readonly TimeSpan window; private readonly Dictionary<string, Queue<DateTime>> store = new(); public RateLimiter(int limit, TimeSpan window) { this.limit = limit; this.window = window; } public bool IsAllowed(string user) { if(!store.ContainsKey(user)) store[user] = new Queue<DateTime>(); var q = store[user]; while(q.Count > 0 && q.Peek() < DateTime.Now…
Explain a bit more
- window) q.Dequeue(); if(q.Count >= limit) return false; q.Enqueue(DateTime.Now); return true; } } Prevents abuse such as excessive requests, bots, and denial-of-service attempts.
Example code
public class RateLimiter
{
private readonly int limit;
private readonly TimeSpan window;
private readonly Dictionary<string, Queue<DateTime>> store = new();
public RateLimiter(int limit, TimeSpan window)
{
this.limit = limit;
this.window = window;
}
public bool IsAllowed(string user)
{
if(!store.ContainsKey(user))
store[user] = new Queue<DateTime>();
var q = store[user]; while(q.Count > 0 && q.Peek() < DateTime.Now - window) q.Dequeue(); if(q.Count >= limit)
return false; q.Enqueue(DateTime.Now); return true;
}
} Prevents abuse such as excessive requests, bots, and denial-of-service attempts.
Real-world example (ShopNest)
Answer with: definition → one ShopNest-style story → trade-off → how you would verify it in production.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png