FastPrepTop-Scoring Items in a Time Window

Top-Scoring Items in a Time Window

Pinterest logoPinterest● MediumFULLTIMEONSITE INTERVIEW

Updated

Problem statement

Maintain scored items that a user has viewed within a configurable time window. Process the listed operations from first to last and return one item ID for each TOP operation.

  • ADD time id score adds a newly viewed item with a fixed integer score.
  • EXPIRE time removes expired items and produces no output.
  • TOP time returns the active item with greatest score, or the empty string if none exists. Equal scores choose the item inserted earliest in the operation sequence.

Each operation’s time is at least as large as the previous operation’s time. Before every operation, remove items viewed at a time less than or equal to time - window; active items are those in (time - window, time]. Thus TOP expires items automatically. Item IDs are globally unique in the batch, and scores do not change after insertion. Operations at equal times still follow input order.

For this practice exercise, use the given time window rather than a fixed number of recent items. Discuss the complexity of adding an item, expiring items, and retrieving the top item.

Function

trackTopItems(window: int, operations: String[]) → String[]

Examples

Example 1

window = 10operations = ["ADD 0 a 5","ADD 3 b 9","TOP 3","ADD 5 c 9","TOP 5","EXPIRE 13","TOP 13","TOP 15"]return = ["b","b","c",""]

The window is ten long. At the first TOP, items a (score five) and b (score nine) are active, so b is returned. After c is added with score nine, b and c tie, and b was inserted earlier, so b is returned again. At time 13 an item stays only if it was added after time three, so a and b have expired and c is the top. At time 15 c has expired too, so the result is empty.

Example 2

window = 604800operations = ["ADD 0 981292206280418755 50","ADD 0 981292206280418756 35","ADD 0 981292206280418757 91","ADD 0 981292206280418758 92","ADD 0 981292206280418759 3","ADD 0 981292206280418760 88","ADD 0 981292206280418761 34","ADD 0 981292206280418762 9","ADD 0 981292206280418763 77","ADD 0 981292206280418764 55","TOP 0"]return = ["981292206280418758"]

All ten items are added at time zero and are still active. The highest score among them is 92, so that item's ID is returned.

Example 3

window = 5operations = ["TOP 0","ADD 0 x 7","TOP 4","TOP 5"]return = ["","x",""]

The first TOP finds no items and returns an empty string. Item x is added at time 0. At time four it is still inside the window, so TOP returns x. At time 5 it sits exactly on the edge of the window and has expired, so the result is empty.

Constraints

  • 1 <= window <= 10^9.
  • 0 <= operations.length <= 20000.
  • 0 <= time <= 10^9; times are nondecreasing.
  • -10^9 <= score <= 10^9.
  • IDs contain 1 to 32 ASCII letters or digits, have no spaces, and are unique across ADD operations.
  • Operations have exactly the stated format with single spaces and valid fields.

More Pinterest Onsite Interview questions

Similar questions at other companies

Pinterest interview experiences

All Pinterest interview questionsSee Pinterest hiring insights
Saved
class Solution {
    public String[] trackTopItems(int window, String[] operations) {
        // write your code here
    }
}
Checking account…