Fixed window algorithm in Rest API - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to check and update request counts grows as more requests come in.
How does the fixed window algorithm handle many requests efficiently?
Analyze the time complexity of the following code snippet.
// Pseudocode for fixed window rate limiting
function isRequestAllowed(userId) {
currentWindow = getCurrentTime() / windowSize
count = getCount(userId, currentWindow) // get count from storage
if (count < limit) {
incrementCount(userId, currentWindow)
return true
} else {
return false
}
}
This code checks if a user has made fewer requests than the limit in the current time window, then increments the count if allowed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and updating the count for the user in the current time window.
- How many times: Once per request received.
Each request triggers a single check and update operation regardless of how many requests happened before.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and updates |
| 100 | 100 checks and updates |
| 1000 | 1000 checks and updates |
Pattern observation: The number of operations grows directly with the number of requests.
Time Complexity: O(1)
This means each request is handled in constant time, no matter how many requests have come before.
[X] Wrong: "The time to check requests grows as more requests come in because we have to look at all previous requests."
[OK] Correct: The fixed window algorithm only checks the count for the current window, not all past requests, so it stays constant time.
Understanding fixed window time complexity shows you can reason about how systems handle many requests efficiently, a useful skill for real-world API design.
"What if we changed the fixed window to a sliding window algorithm? How would the time complexity change?"
Practice
What is the main idea behind the Fixed window algorithm in rate limiting?
Solution
Step 1: Understand fixed window concept
The fixed window algorithm divides time into fixed intervals (windows) and counts requests in each interval.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.Final Answer:
Count requests in fixed time intervals and block if limit exceeded -> Option AQuick Check:
Fixed window = count + block in fixed intervals [OK]
- Confusing fixed window with sliding window algorithm
- Thinking it allows unlimited requests
- Assuming it blocks all requests immediately
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 = ?
Solution
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.Step 2: Apply formula
Usingcurrent_time - (current_time % window_size)gives the start of the current fixed window.Final Answer:
current_time - (current_time % window_size) -> Option AQuick Check:
Window start = current_time - remainder [OK]
- Using modulo alone without subtraction
- Adding window size instead of subtracting remainder
- Confusing window start with window end
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")Solution
Step 1: Trace request counts
Count starts at 0. For each request (value 1), count increases until it reaches limit 3.Step 2: Determine allowed or blocked
First 3 requests increase count to 3 and print "Allowed". Next requests exceed limit, so print "Blocked".Final Answer:
Allowed Allowed Allowed Blocked Blocked -> Option CQuick Check:
Count ≤ 3 allowed, else blocked [OK]
- Assuming all requests allowed
- Resetting count incorrectly
- Confusing count comparison operator
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")Solution
Step 1: Identify window reset logic
The code resets count when window expires but does not updatewindow_start, so window never moves forward.Step 2: Fix by updating window start time
Inside the if block, updatewindow_start = int(time.time())to start a new window correctly.Final Answer:
Movewindow_startupdate inside the if block to reset window -> Option DQuick Check:
Reset count and window start together [OK]
- Not updating window start time
- Resetting count incorrectly
- Ignoring time difference condition
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?
Solution
Step 1: Understand per-user counting
Each user must have a separate count to avoid mixing requests between users.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.Final Answer:
Use a dictionary with user IDs as keys and counts as values, reset counts per user each window -> Option BQuick Check:
Separate counts per user = dictionary keyed by user [OK]
- Using a single global count for all users
- Not resetting counts per user
- Ignoring user identification in counting
