Bird
Raised Fist0
Rest APIprogramming~20 mins

Fixed window algorithm in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Fixed Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fixed Window Rate Limiter Check
Consider a fixed window rate limiter that allows 3 requests per 10 seconds. Given the following sequence of request timestamps (in seconds): [1, 2, 3, 11, 12]. What is the output of the rate limiter for each request (True means allowed, False means blocked)?
Rest API
class FixedWindowLimiter:
    def __init__(self, limit, window_size):
        self.limit = limit
        self.window_size = window_size
        self.windows = {}

    def allow_request(self, timestamp):
        window = timestamp // self.window_size
        count = self.windows.get(window, 0)
        if count < self.limit:
            self.windows[window] = count + 1
            return True
        else:
            return False

limiter = FixedWindowLimiter(3, 10)
requests = [1, 2, 3, 11, 12]
results = [limiter.allow_request(t) for t in requests]
print(results)
A[True, True, True, False, False]
B[True, True, True, False, True]
C[True, True, True, True, False]
D[True, True, True, True, True]
Attempts:
2 left
💡 Hint
Remember that the fixed window resets counts every window_size seconds.
🧠 Conceptual
intermediate
1:30remaining
Understanding Fixed Window Algorithm Behavior
Which statement best describes a limitation of the fixed window rate limiting algorithm?
AIt uses a sliding window to smooth out request rates.
BIt can cause bursts of requests at window boundaries, allowing more requests than the limit in a short time.
CIt evenly distributes requests over time to prevent any bursts.
DIt tracks each request individually to prevent any overuse.
Attempts:
2 left
💡 Hint
Think about what happens when one window ends and the next begins.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Fixed Window Implementation
What error will the following fixed window rate limiter code produce when calling allow_request(5) twice?
Rest API
class FixedWindowLimiter:
    def __init__(self, limit, window_size):
        self.limit = limit
        self.window_size = window_size
        self.windows = {}

    def allow_request(self, timestamp):
        window = timestamp // self.window_size
        if self.windows[window] < self.limit:
            self.windows[window] += 1
            return True
        else:
            return False

limiter = FixedWindowLimiter(1, 10)
print(limiter.allow_request(5))
print(limiter.allow_request(5))
AKeyError
BTypeError
CFalse, True
DTrue, False
Attempts:
2 left
💡 Hint
Check what happens when accessing a dictionary key that might not exist.
📝 Syntax
advanced
1:30remaining
Syntax Error in Fixed Window Code
Which option contains the correct syntax to increment the count for the current window in a fixed window rate limiter?
Rest API
window = timestamp // window_size
# increment count for window
Awindows[window] = windows[window] + 1 or 1
Bwindows[window] += 1 if window in windows else 1
Cwindows[window] = windows.get(window, 0) + 1
Dwindows[window] = windows[window] ? windows[window] + 1 : 1
Attempts:
2 left
💡 Hint
Use dictionary get method to provide a default value.
🚀 Application
expert
2:30remaining
Calculate Number of Allowed Requests in Fixed Window
A fixed window rate limiter allows 5 requests per 15 seconds. Given the request timestamps (in seconds): [2, 5, 7, 14, 15, 16, 29, 30], how many requests will be allowed in total?
A8
B6
C5
D7
Attempts:
2 left
💡 Hint
Group requests by their window and count up to the limit per window.

Practice

(1/5)
1.

What is the main idea behind the Fixed window algorithm in rate limiting?

easy
A. Count requests in fixed time intervals and block if limit exceeded
B. Allow unlimited requests without any restrictions
C. Count requests over a sliding time window for smooth limiting
D. Block all requests after the first one

Solution

  1. Step 1: Understand fixed window concept

    The fixed window algorithm divides time into fixed intervals (windows) and counts requests in each interval.
  2. Step 2: Identify how blocking works

    If the number of requests in the current window exceeds the limit, further requests are blocked until the next window.
  3. Final Answer:

    Count requests in fixed time intervals and block if limit exceeded -> Option A
  4. Quick Check:

    Fixed window = count + block in fixed intervals [OK]
Hint: Fixed window counts requests per fixed time block [OK]
Common Mistakes:
  • Confusing fixed window with sliding window algorithm
  • Thinking it allows unlimited requests
  • Assuming it blocks all requests immediately
2.

Which of the following is the correct way to check the current window start time in a fixed window algorithm using Python?

import time
window_size = 60  # seconds
current_time = int(time.time())
window_start = ?
easy
A. current_time - (current_time % window_size)
B. current_time % window_size
C. current_time + window_size
D. window_size - current_time

Solution

  1. Step 1: Calculate window start using modulo

    The window start is the current time minus the remainder when divided by window size, to align to window boundary.
  2. Step 2: Apply formula

    Using current_time - (current_time % window_size) gives the start of the current fixed window.
  3. Final Answer:

    current_time - (current_time % window_size) -> Option A
  4. Quick Check:

    Window start = current_time - remainder [OK]
Hint: Use modulo to find window start time [OK]
Common Mistakes:
  • Using modulo alone without subtraction
  • Adding window size instead of subtracting remainder
  • Confusing window start with window end
3.

Given the following Python code snippet implementing a fixed window rate limiter, what will be the output?

requests = [1, 1, 1, 1, 1]
limit = 3
window_start = 0
count = 0
for req in requests:
    if count < limit:
        count += req
        print("Allowed")
    else:
        print("Blocked")
medium
A. Allowed Allowed Allowed Allowed Allowed
B. Blocked Blocked Blocked Blocked Blocked
C. Allowed Allowed Allowed Blocked Blocked
D. Allowed Allowed Blocked Allowed Blocked

Solution

  1. Step 1: Trace request counts

    Count starts at 0. For each request (value 1), count increases until it reaches limit 3.
  2. Step 2: Determine allowed or blocked

    First 3 requests increase count to 3 and print "Allowed". Next requests exceed limit, so print "Blocked".
  3. Final Answer:

    Allowed Allowed Allowed Blocked Blocked -> Option C
  4. Quick Check:

    Count ≤ 3 allowed, else blocked [OK]
Hint: Count requests and block after limit reached [OK]
Common Mistakes:
  • Assuming all requests allowed
  • Resetting count incorrectly
  • Confusing count comparison operator
4.

Identify the bug in this fixed window rate limiter code snippet and select the fix:

limit = 5
window_size = 60
count = 0
window_start = int(time.time())

if int(time.time()) - window_start > window_size:
    count = 0

count += 1
if count > limit:
    print("Blocked")
else:
    print("Allowed")
medium
A. Increase limit to 10
B. Change count += 1 to count = 1
C. Remove the if condition checking time difference
D. Move window_start update inside the if block to reset window

Solution

  1. Step 1: Identify window reset logic

    The code resets count when window expires but does not update window_start, so window never moves forward.
  2. Step 2: Fix by updating window start time

    Inside the if block, update window_start = int(time.time()) to start a new window correctly.
  3. Final Answer:

    Move window_start update inside the if block to reset window -> Option D
  4. Quick Check:

    Reset count and window start together [OK]
Hint: Update window start when resetting count [OK]
Common Mistakes:
  • Not updating window start time
  • Resetting count incorrectly
  • Ignoring time difference condition
5.

You want to implement a fixed window rate limiter that allows 10 requests per minute per user. Which approach correctly handles multiple users without mixing counts?

hard
A. Store counts in a list without user identification
B. Use a dictionary with user IDs as keys and counts as values, reset counts per user each window
C. Use a single global count for all users and reset every minute
D. Allow unlimited requests for all users

Solution

  1. Step 1: Understand per-user counting

    Each user must have a separate count to avoid mixing requests between users.
  2. Step 2: Use dictionary keyed by user ID

    A dictionary with user IDs as keys and counts as values allows tracking each user's requests independently and resetting counts per window.
  3. Final Answer:

    Use a dictionary with user IDs as keys and counts as values, reset counts per user each window -> Option B
  4. Quick Check:

    Separate counts per user = dictionary keyed by user [OK]
Hint: Track counts per user with dictionary keys [OK]
Common Mistakes:
  • Using a single global count for all users
  • Not resetting counts per user
  • Ignoring user identification in counting