Practice/OpenAI/Leetcode 1797. Design Authentication Manager
Leetcode 1797. Design Authentication Manager
CodingMust
Problem
Design a session token management system that tracks authentication tokens with automatic expiration. Each token has a fixed lifetime (timeToLive seconds) and can be renewed before it expires.
Your system should support three operations:
- generate(tokenId, currentTime): Create a new token with the given ID at the specified time. The token will expire after timeToLive seconds.
- renew(tokenId, currentTime): Extend the expiration time of an existing token. This operation only succeeds if the token exists and has not yet expired at currentTime. If successful, the token's expiration is reset to currentTime + timeToLive.
- countUnexpiredTokens(currentTime): Return the number of tokens that are still valid at the given time.
Important: A token that expires exactly at time T is considered expired at time T (not valid).
Requirements
- Implement a class with a constructor that takes timeToLive as a parameter
- Track multiple tokens independently by their unique string IDs
- Properly handle token expiration (a token expires timeToLive seconds after creation or last renewal)
- The renew operation must check if the token is still valid before renewing
- Efficiently count unexpired tokens without iterating through all tokens every time
- Handle edge cases: renewing non-existent tokens, renewing expired tokens, counting at various times
Constraints
- 1 ≤ timeToLive ≤ 10^8
- 1 ≤ currentTime ≤ 10^8
- currentTime is strictly increasing across all function calls
- 1 ≤ tokenId.length ≤ 5
- tokenId consists only of lowercase letters
- At most 2000 calls will be made to generate, renew, and countUnexpiredTokens combined
Examples
Example 1:
`
Input:
SessionManager(5)
generate("token1", 1)
renew("token1", 2)
countUnexpiredTokens(6)
renew("token1", 6)
countUnexpiredTokens(7)
Output: [null, null, null, 1, null, 0]
Explanation:
- SessionManager(5): Initialize with timeToLive = 5
- generate("token1", 1): Create token1 at time 1, expires at time 6
- renew("token1", 2): Token is valid, renew it. Now expires at time 7
- countUnexpiredTokens(6): Token expires at 7, so it's still valid at 6. Return 1
- renew("token1", 6): Token is valid, renew it. Now expires at time 11
- countUnexpiredTokens(7): Token expires at 11, so it's still valid at 7. Return 1
`
Example 2:
`
Input:
SessionManager(3)
generate("a", 1)
generate("b", 2)
generate("c", 5)
countUnexpiredTokens(6)
renew("a", 4)
renew("b", 5)
countUnexpiredTokens(8)
Output: [null, null, null, null, 1, null, null, 1]
Explanation:
- SessionManager(3): Initialize with timeToLive = 3
- generate("a", 1): Token a expires at time 4
- generate("b", 2): Token b expires at time 5
- generate("c", 5): Token c expires at time 8
- countUnexpiredTokens(6): Only token c is valid (a expired at 4, b at 5). Return 1
- renew("a", 4): Token a already expired, renewal fails
- renew("b", 5): Token b already expired (expires exactly at 5), renewal fails
- countUnexpiredTokens(8): Token c expires at 8, so it's expired. Return 0
`