1797. Design Authentication Manager LeetCode Solution
In this guide, you will get 1797. Design Authentication Manager LeetCode Solution with the best time and space complexity. The solution to Design Authentication Manager problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Design Authentication Manager solution in C++
- Design Authentication Manager solution in Java
- Design Authentication Manager solution in Python
- Additional Resources

Problem Statement of Design Authentication Manager
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 that 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.
Example 1:
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]
Explanation
AuthenticationManager authenticationManager = new AuthenticationManager(5); // Constructs the AuthenticationManager with timeToLive = 5 seconds.
authenticationManager.renew(“aaa”, 1); // No token exists with tokenId “aaa” at time 1, so nothing happens.
authenticationManager.generate(“aaa”, 2); // Generates a new token with tokenId “aaa” at time 2.
authenticationManager.countUnexpiredTokens(6); // The token with tokenId “aaa” is the only unexpired one at time 6, so return 1.
authenticationManager.generate(“bbb”, 7); // Generates a new token with tokenId “bbb” at time 7.
authenticationManager.renew(“aaa”, 8); // The token with tokenId “aaa” expired at time 7, and 8 >= 7, so at time 8 the renew request is ignored, and nothing happens.
authenticationManager.renew(“bbb”, 10); // The token with tokenId “bbb” is unexpired at time 10, so the renew request is fulfilled and now the token will expire at time 15.
authenticationManager.countUnexpiredTokens(15); // The token with tokenId “bbb” expires at time 15, and the token with tokenId “aaa” expired at time 7, so currently no token is unexpired, so return 0.
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.
The values of currentTime across all the function calls will be strictly increasing.
At most 2000 calls will be made to all functions combined.
Complexity Analysis
- Time Complexity: O(|\texttt{generate()} + \texttt{renew()}|)
- Space Complexity: O(|\texttt{generate()} + \texttt{renew()}|)
1797. Design Authentication Manager LeetCode Solution in C++
class AuthenticationManager {
public:
AuthenticationManager(int timeToLive) : timeToLive(timeToLive) {}
void generate(string tokenId, int currentTime) {
tokenIdToExpiryTime[tokenId] = currentTime;
times.emplace(currentTime);
}
void renew(string tokenId, int currentTime) {
if (const auto it = tokenIdToExpiryTime.find(tokenId);
it == tokenIdToExpiryTime.cend() ||
currentTime >= it->second + timeToLive)
return;
times.erase(tokenIdToExpiryTime[tokenId]);
tokenIdToExpiryTime[tokenId] = currentTime;
times.emplace(currentTime);
}
int countUnexpiredTokens(int currentTime) {
const auto it = times.lower_bound(currentTime - timeToLive + 1);
// Remove expired tokens.
times.erase(times.begin(), it);
return times.size();
}
private:
const int timeToLive;
unordered_map<string, int> tokenIdToExpiryTime;
set<int> times;
};
/* code provided by PROGIEZ */
1797. Design Authentication Manager LeetCode Solution in Java
public class AuthenticationManager {
public AuthenticationManager(int timeToLive) {
this.timeToLive = timeToLive;
}
public void generate(String tokenId, int currentTime) {
tokenIdToExpiryTime.put(tokenId, currentTime);
times.add(currentTime);
}
public void renew(String tokenId, int currentTime) {
Integer expiryTime = tokenIdToExpiryTime.get(tokenId);
if (expiryTime == null || currentTime >= expiryTime + timeToLive)
return;
times.remove(expiryTime);
tokenIdToExpiryTime.put(tokenId, currentTime);
times.add(currentTime);
}
public int countUnexpiredTokens(int currentTime) {
final int expiryTimeThreshold = currentTime - timeToLive + 1;
// Remove expired tokens.
times.headSet(expiryTimeThreshold).clear();
return times.size();
}
private final int timeToLive;
private final Map<String, Integer> tokenIdToExpiryTime = new HashMap<>();
private final TreeSet<Integer> times = new TreeSet<>();
}
// code provided by PROGIEZ
1797. Design Authentication Manager LeetCode Solution in Python
from sortedcontainers import SortedSet
class AuthenticationManager:
def __init__(self, timeToLive: int):
self.timeToLive = timeToLive
self.tokenIdToExpiryTime = {}
self.times = SortedSet()
def generate(self, tokenId: str, currentTime: int) -> None:
self.tokenIdToExpiryTime[tokenId] = currentTime
self.times.add(currentTime)
def renew(self, tokenId: str, currentTime: int) -> None:
if (tokenId not in self.tokenIdToExpiryTime or
currentTime >= self.tokenIdToExpiryTime[tokenId] + self.timeToLive):
return
self.times.remove(self.tokenIdToExpiryTime[tokenId])
self.tokenIdToExpiryTime[tokenId] = currentTime
self.times.add(currentTime)
def countUnexpiredTokens(self, currentTime: int) -> int:
i = self.times.bisect_left(currentTime - self.timeToLive + 1)
# Remove expired tokens.
for _ in range(i):
self.times.remove(self.times[0])
return len(self.times)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.