23/28
Part 6 of 6 · What interviewers ask nextLearn and do
What interviewers ask next
The problem's README lists the design follow-ups. Have an answer ready for each.
Politeness: cap the rate for each host
import threading
import time
class RateLimit:
def __init__(self, per_second: float) -> None:
self.gap, self.next_slot, self.lock = 1 / per_second, 0.0, threading.Lock()
def wait(self) -> None:
"""Sleeps until this caller's reserved slot."""
# reserve a slot: arithmetic only
with self.lock:
now = time.monotonic()
slot = max(now, self.next_slot)
self.next_slot = slot + self.gap
# sleep outside the lock
time.sleep(slot - now)
Pages come back in 10 ms, and you run 8 workers. How many requests a second hit the server?
Pick an answer above to see the rest
At 10 requests a second, how long does the 41-page site take with 1, 8 or 40 workers?
Pick an answer above to see the rest