Problem · Array

Top-K URLs Overall and in the Last 24 Hours

Learn this problem
MediumOracle logoOracleFULLTIMEPHONE SCREEN

Problem statement

You are given access-log records in parallel arrays. Record i contains URL urls[i] and Unix-second timestamp timestamps[i].

Return two ranked URL lists:

  1. the top k URLs across all records;
  2. the top k URLs whose timestamps are in the inclusive interval [queryTime - 86400, queryTime].

Rank a URL with more accesses first. Break equal-frequency ties by lexicographically smaller URL. If a group contains fewer than k distinct URLs, return all of them. The result is a two-row string matrix in overall-then-recent order.

Function

topKUrls(urls: String[], timestamps: long[], queryTime: long, k: int) → String[][]

Examples

Example 1

urls = ["old","old","old","new","new","edge"]timestamps = [1,2,3,200000,199999,113600]queryTime = 200000k = 2return = [["old","new"],["new","edge"]]

Overall, old appears three times and new twice. Only new and edge fall in the inclusive recent window.

Example 2

urls = ["b","a","c"]timestamps = [10,10,10]queryTime = 10k = 5return = [["a","b","c"],["a","b","c"]]

All frequencies tie, so lexicographic order decides; fewer than k distinct URLs are returned.

Constraints

  • 1 <= urls.length = timestamps.length <= 2 * 10^5
  • 1 <= k <= 10^5
  • Each URL is a non-empty lowercase path of length at most 100.
  • 0 <= timestamps[i] <= queryTime <= 10^12

More Oracle problems

drafts saved locally
public String[][] topKUrls(String[] urls, long[] timestamps, long queryTime, int k) {
    // Write your code here.
}
urls["old","old","old","new","new","edge"]
timestamps[1,2,3,200000,199999,113600]
queryTime200000
k2
expected[["old", "new"], ["new", "edge"]]
checking account