FastPrepAuthentication System 🐹

Authentication System 🐹

TikTok logoTikTokMediumOA
Learn

Problem statement

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

getUnexpiredTokens(time_to_live: int, queries: String[][]) → int[]

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

Examples

Example 1

time_to_live = 5queries = [["generate aaa 1"], ["renew aaa 2"], ["count 6"], ["generate bbb 7"], ["renew aaa 8"], ["renew bbb 10"], ["count 15"]]return = [1, 0]

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.

Constraints

  • Unknwon for now

More TikTok problems

See TikTok hiring insights
public int[] getUnexpiredTokens(int time_to_live, String[][] queries) {
  // write your code here
}
time_to_live5
queries[["generate aaa 1"], ["renew aaa 2"], ["count 6"], ["generate bbb 7"], ["renew aaa 8"], ["renew bbb 10"], ["count 15"]]
expected[1, 0]
Checking account…