FastPrepOAuth Access Token Cache

OAuth Access Token Cache

Wise logoWise● EasyFULLTIMEONSITE INTERVIEW

Problem statement

Implement an access-token utility for a sequence of requests. A token provider supplies successful replies in the order given by tokens and lifetimes.

For each request time in requestTimes, return the token used by that request.

  • The cache starts empty. Request times are nondecreasing and are processed in their input order.
  • If a cached token was fetched at time f with lifetime d, it is valid at time t exactly when t < f + d. It is expired at t = f + d.
  • Use a valid cached token without consuming a provider reply.
  • Otherwise, consume the next unused provider reply. Its value is tokens[k], its lifetime is lifetimes[k], and its fetch time is the current request time. Store it in the cache and use it for this request.
  • All provider calls succeed. A fetch takes no time. There are no other cache changes. Repeated token strings are allowed.
  • The provider arrays each contain one reply per request, so there are always enough replies. Unused replies are ignored.
  • Return the tokens in request order. If there are no requests, return an empty array.

Function

getAccessTokens(requestTimes: int[], tokens: String[], lifetimes: int[]) → String[]

Examples

Example 1

requestTimes = [0,3,5,6]tokens = ["alpha","beta","unused","spare"]lifetimes = [5,2,10,10]return = ["alpha","alpha","beta","beta"]

The first request fetches alpha, which expires at time 5. Time 3 reuses it. At time 5, fetch beta; it expires at time 7, so time 6 reuses it. Only two provider replies are consumed.

Example 2

requestTimes = [7,7,8]tokens = ["same","next","extra"]lifetimes = [1,10,1]return = ["same","same","next"]

The first request fetches same at time 7, with expiry 8. The next request at the same time reuses it. At time 8, it has expired, so fetch next.

Example 3

requestTimes = []tokens = []lifetimes = []return = []

There are no requests, so no token is fetched and the result is empty.

Constraints

  • 0 <= requestTimes.length <= 2000.
  • tokens.length = lifetimes.length = requestTimes.length.
  • 0 <= requestTimes[i] <= 2000000000.
  • requestTimes is nondecreasing.
  • 1 <= lifetimes[i] <= 2000000000.
  • 1 <= tokens[i].length <= 16.
  • Each token contains only ASCII letters and digits.

More Wise problems

See Wise hiring insights
Saved
public String[] getAccessTokens(int[] requestTimes, String[] tokens, int[] lifetimes) {
    // write your code here
}
Checking account…