Leetcode 1797: Design Authentication Manager

grid47
grid47
Exploring patterns and algorithms
May 11, 2024 7 min read

You are tasked with creating an authentication system that handles tokens. Each token has an expiration time, and a user can generate new tokens or renew existing ones before they expire. The system should track and count the number of unexpired tokens at any given time.
Problem
Approach
Steps
Complexity
Input: The input consists of a series of operations: creating new tokens, renewing tokens, and counting unexpired tokens. Each token has an expiration time determined by the timeToLive parameter provided during the initialization of the AuthenticationManager.
Example: Input: ["AuthenticationManager", "generate", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1], ["aaa", 2], [6]]
Constraints:
• 1 <= timeToLive <= 10^8
• 1 <= currentTime <= 10^8
• 1 <= tokenId.length <= 5
• tokenId consists only of lowercase letters
• All generate calls will have unique tokenIds
• currentTime will strictly increase with each operation
Output: The output consists of the results of the operations, with generate and renew methods returning 'null' and the countUnexpiredTokens method returning the count of unexpired tokens at the given time.
Example: Output: [null, null, null, 1]
Constraints:
• The function must be efficient enough to handle up to 2000 operations.
Goal: Implement a system to manage token generation, renewal, and expiration, ensuring the correct number of unexpired tokens is returned when requested.
Steps:
• Initialize an authentication manager with a given timeToLive.
• For each token, store its expiration time when it is generated.
• For renewals, check if the token exists and is unexpired before extending its expiration time.
• Implement a method to count the number of unexpired tokens based on the current time.
Goal: Ensure the solution can handle edge cases where no tokens exist or all tokens have expired.
Steps:
• The solution must be efficient and capable of handling the given constraints of up to 2000 operations.
Assumptions:
• Tokens are uniquely identified by their tokenId.
• All tokens have an expiration time that is based on the current time and the timeToLive value.
Input: Input: ["AuthenticationManager", "generate", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1], ["aaa", 2], [6]]
Explanation: The system initializes with a timeToLive of 5 seconds. A new token 'aaa' is generated at time 1. At time 6, there is one unexpired token, 'aaa', so the count is 1.

Input: Input: ["AuthenticationManager", "generate", "renew", "countUnexpiredTokens"] [[5], ["aaa", 1], ["aaa", 2], [7]]
Explanation: At time 7, the token 'aaa' has expired, so there are no unexpired tokens, and the count is 0.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus