Embercode
✕
Dicts and SetsQuick quiz

Rate limiter

7 questions. Covers Sliding window rate limiter in Python.

Questions 1 to 5 of 7

  1. 1. Why does the lesson keep each user's request times in a deque and remove from the front, instead of using a list?
  2. 2. The lesson's RateLimiter class is used unchanged. What does this code print on one line? ``python limiter = RateLimiter(2, 5000) for t in [0, 1000, 5000, 5500, 6000]: print(limiter.allow('ana', t), end=' ') ``
  3. 3. Using the RateLimiter class unchanged, what does this code print? ``python limiter = RateLimiter(3, 10000) limiter.allow('ana', 0) limiter.allow('bo', 2000) limiter.allow('cy', 4000) limiter.remove_idle_users(10500) print(limiter.tracked_users()) ``
  4. 4. Put the steps that allow runs for one request in the correct order.

    Use the arrows to put the steps in order.

    1. 1.Remove the times at the front that are one window old or older
    2. 2.If the queue holds fewer than the limit, add the new time at the back and allow the request
    3. 3.Otherwise reject the request without storing its time
    4. 4.Look up the user's queue, creating an empty one if the user is new
  5. 5. Put the steps that remove_idle_users takes for one user's queue in the correct order.

    Use the arrows to put the steps in order.

    1. 1.Check whether the queue is empty after the old times have left
    2. 2.Call _evict_old on the user's queue with the cleanup time
    3. 3.Delete the user's dict entry only when the queue is empty

3 question(s) left on this page.