FastPrepFastPrep
Problem Brief

Get Latest K Requests ๐ŸŒฟ

FULLTIMEOA

Given n request ids as an array of strings, requests, and an integer k, after all requests are received, find the k most recent requests. Report the answer in order of most recent to least recent.

Function Description

Complete the function getLatestKRequests in the editor below.

getLatestKRequests takes the following arguments:

  1. str requests[n]: the request ids
  2. int k: the number of requests to report

Returns

str[k]: the k most recent requests

1Example 1

Input
requests = ["item1", "item2", "item3", "item1", "item3"], k = 3
Output
["item3", "item1", "item2"]
Explanation
Starting from the right and moving left, collecting distinct requests, there is "item3" followed by "item1". Skip the second instance of "item3" and find "item2". The answer is ["item3", "item1", "item2"].

Constraints

Limits and guarantees your solution can rely on.

Unknown for now. If you happen to know about it, feel free to lmk. Many thanks in advance! ๐Ÿฅฐ
public String[] getLatestKRequests(String[] requests, int k) {
    // write your code here
}
Input

requests

["item1", "item2", "item3", "item1", "item3"]

k

3

Output

["item3", "item1", "item2"]

Sign in to submit your solution.