Problem ยท Array

Get Latest K Requests ๐ŸŒฟ

Learn this problem
โ— EasyIBMFULLTIMEOA
See IBM hiring insights

Problem statement

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

getLatestKRequests(requests: String[], k: int) โ†’ String[]

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

Examples

Example 1

requests = ["item1", "item2", "item3", "item1", "item3"]k = 3return = ["item3", "item1", "item2"]
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

Unknown for now. If you happen to know about it, feel free to lmk. Many thanks in advance! ๐Ÿฅฐ

More IBM problems

drafts saved locally
public String[] getLatestKRequests(String[] requests, int k) {
    // write your code here
}
requests["item1", "item2", "item3", "item1", "item3"]
k3
expected["item3", "item1", "item2"]
checking account