Design Authentication Manager - Problem

๐Ÿ” Authentication Token Manager

You're building a secure authentication system that manages user session tokens with automatic expiration. Think of it like a digital bouncer that checks if tokens are still valid!

Your system needs to handle four key operations:

  • AuthenticationManager(timeToLive) - Initialize the system with a token lifetime
  • generate(tokenId, currentTime) - Create a new token that expires after timeToLive seconds
  • renew(tokenId, currentTime) - Extend an unexpired token's lifetime
  • countUnexpiredTokens(currentTime) - Count how many tokens are still active

Important: If a token expires exactly at time t, it's considered expired before any other operations at time t.

Example: If timeToLive = 5 and you generate token "abc" at time 10, it expires at time 15. If you call countUnexpiredTokens(15), this token won't be counted!

Input & Output

example_1.py โ€” Basic Operations
$ Input: ["AuthenticationManager", "renew", "generate", "countUnexpiredTokens", "generate", "renew", "renew", "countUnexpiredTokens"] [[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, ignore. generate("aaa", 2): Token "aaa" expires at time 7. countUnexpiredTokens(6): Token "aaa" is still valid (7 > 6). generate("bbb", 7): Token "bbb" expires at time 12. renew("aaa", 8): Token "aaa" expired at time 7, ignore. renew("bbb", 10): Token "bbb" renewed, now expires at time 15. countUnexpiredTokens(15): All tokens expired.
example_2.py โ€” Edge Case with Same Time
$ Input: ["AuthenticationManager", "generate", "countUnexpiredTokens", "countUnexpiredTokens"] [[3], ["token1", 10], [13], [14]]
โ€บ Output: [null, null, 0, 0]
๐Ÿ’ก Note: Token generated at time 10 with timeToLive=3 expires exactly at time 13. When counting at time 13, the token is considered expired (expiration happens before counting). At time 14, it's still expired.
example_3.py โ€” Multiple Renewals
$ Input: ["AuthenticationManager", "generate", "renew", "renew", "countUnexpiredTokens"] [[4], ["token", 5], ["token", 8], ["token", 11], [15]]
โ€บ Output: [null, null, null, null, 1]
๐Ÿ’ก Note: Token created at time 5 (expires at 9), renewed at time 8 (expires at 12), renewed again at time 11 (expires at 15). At time 15, it expires exactly then, but we count before expiration, so it's valid.

Constraints

  • 1 โ‰ค timeToLive โ‰ค 108
  • 1 โ‰ค currentTime โ‰ค 108
  • 1 โ‰ค tokenId.length โ‰ค 5
  • tokenId consists only of lowercase letters
  • At most 2000 calls will be made to generate, renew, and countUnexpiredTokens

Visualization

Tap to expand
๐Ÿ” Authentication Token Lifecyclet=5t=10t=15t=20GenerateActive TokenExpires: t=15Renewโ†’ t=25ExpiredHash Table Storage"user123"โ†’ 150"admin"โ†’ 85"guest"โ†’ 200"temp"โ†’ 80(expired)โœ… Active TokensโŒ Expired (lazy cleanup)
Understanding the Visualization
1
Token Generation
Create a new token with expiration time = currentTime + timeToLive
2
Token Renewal
Extend valid token's expiration time (like renewing a library book)
3
Expiration Check
Count only tokens where expiryTime > currentTime
4
Lazy Cleanup
Remove expired tokens when encountered to save memory
Key Takeaway
๐ŸŽฏ Key Insight: Hash tables provide O(1) average access time for tokens, while lazy deletion keeps memory usage reasonable by cleaning up expired tokens only when necessary.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
28.4K Views
Medium Frequency
~25 min Avg. Time
834 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