LeetCode: Design Authentication Manager Solution
1/**2 * @param {number} timeToLive3 */4var AuthenticationManager = function (timeToLive) {5 this.map = new Map()6 this.timeToLive = timeToLive7}89/**10 * @param {string} tokenId11 * @param {number} currentTime12 * @return {void}13 */14AuthenticationManager.prototype.generate = function (tokenId, currentTime) {15 this.map.set(tokenId, currentTime)16}1718/**19 * @param {string} tokenId20 * @param {number} currentTime21 * @return {void}22 */23AuthenticationManager.prototype.renew = function (tokenId, currentTime) {24 if (this.map.get(tokenId) + this.timeToLive > currentTime) {25 this.map.set(tokenId, currentTime)26 }27}2829/**30 * @param {number} currentTime31 * @return {number}32 */33AuthenticationManager.prototype.countUnexpiredTokens = function (currentTime) {34 return Array.from(this.map).filter(35 ([token, time]) => time + this.timeToLive > currentTime36 ).length37}
Comments
Loading comments...
Tags
leetcode
hash table
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.