Implement a per-user token bucket with discrete refill intervals
Company: Otter.Ai
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
Implement a rate limiter with `allow_request(user, tokens, timestamp)`. Each user has a separate capacity, refill amount, and refill interval. All numeric configuration values arrive as strings. Refill happens in whole interval-sized steps, measured from that user's first request, rather than continuously.
Return whether the request can consume the requested number of tokens, and explain how you maintain each user's state between calls. The initial balance and invalid-input behavior are unspecified. Agree on those policies before coding.
```hint Interval boundaries
Trace requests just before and exactly on a refill boundary. Consider what time information must survive a request between two boundaries.
```
### Clarifying Questions
- Does a new user's bucket start full or empty on their first request?
- Are timestamps monotonic per user, and do they use the same unit as the refill interval?
- Should unknown users, invalid configuration strings, nonpositive costs, and backward timestamps raise errors or return a denial?
- Does a denied request still advance the refill state, and can configuration change while the user has a live bucket?
### What a Strong Answer Covers
- Parsing and validation of each user's string-valued configuration
- Independent state and first-request anchoring for each user
- Whole-interval refill, capacity capping, and treatment of fractional elapsed intervals
- Request admission, denied requests, and boundary-case tests
### Follow-up Questions
- How would concurrent requests for one user avoid spending the same tokens twice?
- If inactive buckets are evicted, what state must be preserved to retain the original refill schedule?
Overview: Implement a rate limiter with independent user buckets, string-valued configuration, and discrete refill intervals. Explain first-request timing, token consumption, boundary cases, and concurrent updates.
Read the full Otter.Ai Software Engineer interview experience this question came from