Authentication System

Authentication System

Develop a session-based authentication system where each user session generates a new token with a specified time-to-live (TTL) expiration. The TTL expiration is calculated as the current time plus the time-to-live seconds. If a token is renewed, the TTL is extended to the time-to-live seconds after the time of the renewal.

There are three types of queries in this system:

  • "generate <token_id> <current_time>": Creates a new token with the given token_id and current_time.
  • "renew <token_id> <current_time>": Renews an unexpired token with the given token_id and current_time. If there is no unexpired token with the specified token_id, the request is ignored.
  • "count <current_time>": Returns the number of unexpired tokens at the given current_time.
  • Note : If a token expires at a specific time and any action is performed at that same time, the token's expiration occurs before any other actions are carried out.


    Function Description

    Complete the function getUnexpiredTokens in the editor below.

    getUnexpiredTokens has the following parameter(s):

    • 1. int time_to_live : the time to live for a token
    • 2. String[] queries : the queries


    Returns

    int[] : the results from type-3 query in the same order it is given


    Example 1 :

    Input: time_to_live = 5, queries = [["generate aaa 1"], ["renew aaa 2"], ["count 6"], ["generate bbb 7"], ["renew aaa 8"], ["renew bbb 10"], ["count 15"]]
    Output: [1, 0]
    Explanation: The output of the system is [1, 0]. At t = 6, the only unexpired token is "aaa". At t = 15, all tokens have expired, so the count of unexpired tokens is 0.




    Get one-to-one training from Google Facebook engineers

    Top-notch Professionals

    Learn from Facebook and Google senior engineers interviewed 100+ candidates.
    Most recent interview questions and system design topics gathered from aonecode alumnus.
    One-to-one online classes. Get feedbacks from real interviewers.

    Customized Private Class

    Already a coding expert? - Advance straight to hard interview topics of your interest.
    New to the ground? - Develop basic coding skills with your own designated mentor.
    Days before interview? - Focus on most important problems in target company question bank.