Design Authentication Manager - Problem

There is an authentication system that works with authentication tokens. For each session, the user will receive a new authentication token that will expire timeToLive seconds after the currentTime. If the token is renewed, the expiry time will be extended to expire timeToLive seconds after the (potentially different) currentTime.

Implement the AuthenticationManager class:

  • AuthenticationManager(int timeToLive) constructs the AuthenticationManager and sets the timeToLive.
  • generate(string tokenId, int currentTime) generates a new token with the given tokenId at the given currentTime in seconds.
  • renew(string tokenId, int currentTime) renews the unexpired token with the given tokenId at the given currentTime in seconds. If there are no unexpired tokens with the given tokenId, the request is ignored, and nothing happens.
  • countUnexpiredTokens(int currentTime) returns the number of unexpired tokens at the given currentTime.

Note: If a token expires at time t, and another action happens on time t (renew or countUnexpiredTokens), the expiration takes place before the other actions.

Input & Output

Example 1 — Basic Authentication Operations
$ Input: operations = ["AuthenticationManager","renew","generate","countUnexpiredTokens","generate","renew","renew","countUnexpiredTokens"], params = [[5],["aaa",1],["aaa",2],[6],["bbb",7],["aaa",8],["bbb",10],[15]]
Output: [null,null,null,1,null,null,null,0]
💡 Note: AuthenticationManager(5): timeToLive = 5. renew("aaa", 1): no token "aaa" exists, ignored. generate("aaa", 2): token "aaa" expires at time 7. countUnexpiredTokens(6): "aaa" expires at 7 > 6, so 1 token. generate("bbb", 7): token "bbb" expires at time 12. renew("aaa", 8): "aaa" expired at 7 < 8, ignored. renew("bbb", 10): "bbb" expires at 12 > 10, renewed to expire at 15. countUnexpiredTokens(15): both tokens expired, so 0 tokens.
Example 2 — Token Renewal
$ Input: operations = ["AuthenticationManager","generate","renew","countUnexpiredTokens"], params = [[3],["test",1],["test",3],[4]]
Output: [null,null,null,1]
💡 Note: AuthenticationManager(3): timeToLive = 3. generate("test", 1): token expires at time 4. renew("test", 3): token still valid (4 > 3), renewed to expire at 6. countUnexpiredTokens(4): token expires at 6 > 4, so 1 token.
Example 3 — Expiration Edge Case
$ Input: operations = ["AuthenticationManager","generate","countUnexpiredTokens","countUnexpiredTokens"], params = [[2],["token",5],[7],[8]]
Output: [null,null,1,0]
💡 Note: AuthenticationManager(2): timeToLive = 2. generate("token", 5): token expires at time 7. countUnexpiredTokens(7): token expires exactly at 7, but expiration happens before the count, so 1 token. countUnexpiredTokens(8): token expired, so 0 tokens.

Constraints

  • 1 ≤ timeToLive ≤ 108
  • 1 ≤ currentTime ≤ 108
  • 1 ≤ tokenId.length ≤ 5
  • tokenId consists only of lowercase letters
  • All calls to generate will contain unique values of tokenId
  • At most 2000 calls will be made to generate, renew, and countUnexpiredTokens

Visualization

Tap to expand
Authentication Manager - Hash Map Approach INPUT Operations: 1. AuthManager(5) 2. renew("aaa",1) 3. generate("aaa",2) 4. countUnexpired(6) 5. generate("bbb",7) 6-8. renew, renew, count Token HashMap Structure: timeToLive = 5 Key Expiry "aaa" 7 (2+5) "bbb" 12 (7+5) Timeline: t=1 t=2 t=6 t=7 t=15 params: [[5],["aaa",1],...] ALGORITHM STEPS 1 Initialize Store timeToLive, create empty HashMap for tokens 2 generate(token, time) Add token to map with expiry = time + timeToLive 3 renew(token, time) If token exists AND not expired: update expiry time 4 countUnexpired(time) Count tokens where expiry > currentTime Trace: renew("aaa",1): no token generate("aaa",2): exp=7 count(6): 7>6 --> 1 token generate("bbb",7): exp=12 renew("aaa",8): 7<=8 skip renew("bbb",10): exp=15 count(15): 15<=15 --> 0 FINAL RESULT Output Array: [0] AuthManager(5) null [1] renew("aaa",1) null [2] generate("aaa",2) null [3] count(6) 1 [4] generate("bbb",7) null [5] renew("aaa",8) null [6] renew("bbb",10) null [7] count(15) 0 [null,null,null,1,null, null,null,0] Final HashMap State at t=15: "aaa": expired (7 <= 15) "bbb": expired (15 <= 15) Valid tokens: 0 -- OK Key Insight: HashMap provides O(1) token lookup and update. Expiration check happens BEFORE any action at time t (if token expires at t, it's already invalid when action occurs at t). Renew only works on unexpired tokens. Time Complexity: generate/renew O(1), countUnexpired O(n). Space Complexity: O(n) for storing tokens. TutorialsPoint - Design Authentication Manager | Hash Map Approach
Asked in
Microsoft 15 Amazon 12
18.5K Views
Medium Frequency
~25 min Avg. Time
380 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen