Top-Scoring Items in a Time Window
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 scoreadds a newly viewed item with a fixed integer score.EXPIRE timeremoves expired items and produces no output.TOP timereturns 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
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Filter Pins by Excluded IDsONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Policy Violation Set ChecksONSITE INTERVIEW · Seen Sep 2026
- Shortest Sequence of Source SubsequencesONSITE INTERVIEW · Seen Sep 2026
- Insert Addition and Multiplication OperatorsONSITE INTERVIEW · Seen Sep 2026
- Unique Active Pins by Time SegmentONSITE INTERVIEW · Seen Sep 2026
- Coin ChangeONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Maximum Earnings Across WarehousesAmazon · ONSITE INTERVIEW
- Rolling Hit CounterDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Longest Subarray Within an Absolute-Difference LimitGoogle · ONSITE INTERVIEWGoogle interview questions
- Rolling CSAT TrackerDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions
- Longest Continuous Subarray Within an Absolute-Difference LimitUber · ONSITE INTERVIEWUber interview questions
- Rate Limiter with Global and Per-User LimitsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
- Rate Limiter Sliding Window With Per-Entity LimitsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Metric Counter with a Fake ClockSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Per-Client Sliding-Window Rate LimiterNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions