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
Understanding the Visualization
1
Input Operations
Sequence of AuthenticationManager operations
2
Token Management
Generate, renew, and count tokens based on expiry
3
Output Results
Return results for count operations
Key Takeaway
🎯 Key Insight: Use hash map to track token expiry times for efficient operations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code