Mid From PDF Power Questions High-Impact Interview Questions

API Rate Limiter?

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.

More from Career Preparation

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details