Design Authentication Manager

MediumDesigndesign

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 needed

Problem

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

Input: ["AuthenticationManager","generate","generate","countUnexpiredTokens","renew","countUnexpiredTokens","renew","countUnexpiredTokens"] [[10],["x",1],["y",4],[10],["x",11],[12],["y",13],[20]]
Output: [null,null,null,2,null,1,null,1]
Token x expires at 11 and y at 14. At time 11, x expires before the renewal is processed, so the renewal is ignored. Renewing y at 13 pushes its expiry to 23, so it is still alive at 20.
Input: ["AuthenticationManager","countUnexpiredTokens","generate","renew","renew","countUnexpiredTokens","countUnexpiredTokens"] [[3],[1],["a",2],["b",3],["a",4],[6],[7]]
Output: [null,0,null,null,null,1,0]
Renewing an unknown token does nothing. Token a is renewed at 4 so it expires at 7: alive at 6, gone at 7.

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.

More design problems