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 lifetimegenerate(tokenId, currentTime)- Create a new token that expires after timeToLive secondsrenew(tokenId, currentTime)- Extend an unexpired token's lifetimecountUnexpiredTokens(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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code