Level 1 Peak requests per second
The Napkin blocks the door of the design review: no system gets built until someone says how much load it takes. Its first question is about a chat app.
Write peak_qps(daily_users: int, requests_per_user: int, peak_factor: int) -> int:
- Every user sends
requests_per_userrequests a day, and a day has 86,400 seconds. - The average load is
daily_users * requests_per_user / 86,400requests per second. - The busiest second of the day carries
peak_factortimes the average. - Return that peak, rounded up to a whole number. Round once, at the very end: never round the average first.
peak_qps(10_000_000, 20, 3) # 6945: 200M requests / 86,400 s ≈ 2,314.8 per second, x3 ≈ 6,944.4, up to 6,945
peak_qps(100_000, 1, 2) # 3: ≈ 1.157 per second, x2 ≈ 2.31, up to 3 (rounding the average to 2 first gives 4)
peak_qps(86_400, 5, 1) # 5: exactly 5 per second, nothing to round
peak_qps(1_000, 1, 2) # 1: a tiny app still needs room for 1 request per second
1 <= daily_users <= 10^9, 1 <= requests_per_user <= 10^4, 1 <= peak_factor <= 100.
Show hint
stay in whole numbers. Rounding a / b up for positive integers is (a + b - 1) // b. In C++ and Java the product needs 64 bits.