Design Authentication Manager
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
An authentication service issues tokens that live for timeToLive seconds. A token generated or renewed at time t expires at time t + timeToLive. **Expiry takes effect before anything else happening at that same time**, so a token whose expiry equals the current time is already expired.
Implement AuthenticationManager:
- AuthenticationManager(timeToLive) sets the lifetime used for every token.
- generate(tokenId, currentTime) creates a new token tokenId at currentTime.
- renew(tokenId, currentTime) resets the expiry of tokenId to currentTime + timeToLive, but **only if** that token exists and is unexpired at currentTime. Otherwise the call is ignored.
- countUnexpiredTokens(currentTime) returns how many tokens are unexpired at currentTime.
currentTime strictly increases across all calls, and generate is never called with a tokenId that was generated before.
Examples
Constraints
- 1 <= timeToLive <= 10^8
- 1 <= currentTime <= 10^8
- `tokenId` consists of 1 to 5 lowercase letters and digits.
- `currentTime` strictly increases across all calls.
- At most 2000 calls are made in total.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.