Find the k most frequent ad IDs in every sliding window of an ad log

Quick Overview

Slide a fixed-size window over a log of ad IDs and report the k most frequent IDs in every window, with ties going to the smaller ID. It tests sliding-window count updates and how to keep a frequency ranking current as counts change by one at each step.

Find the k most frequent ad IDs in every sliding window of an ad log

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given a stream of ad logs as an array of ad IDs `ads`, a window size `w`, and an integer `k`. Find the `k` most frequent ad IDs within each sliding window of size `w`. For each window position, return the `k` most frequent ad IDs sorted by frequency in descending order. If two ads have the same frequency, the one with the smaller ID comes first. If a window has fewer than `k` distinct ads, return all of its distinct ads, ordered by the same rules. Return a list of lists, where each inner list holds the top ad IDs for the corresponding window position. ### Function Signature ```python def top_k_frequent_ads(ads: list[int], k: int, w: int) -> list[list[int]]: ``` ### Rules - The windows are `ads[i:i + w]` for `i = 0, 1, ..., len(ads) - w`, so the result has `len(ads) - w + 1` inner lists, in left-to-right order. - Within a window, order IDs by count descending, then by ID ascending. - Each inner list has `min(k, d)` IDs, where `d` is the number of distinct IDs in that window. ### Constraints - `1 <= len(ads) <= 10^5` - `1 <= ads[i] <= 10^4` - `1 <= k <= 100` - `1 <= w <= len(ads)` ### Examples ```text Input: ads = [1, 2, 1, 3, 2, 1, 4, 2, 3, 1], k = 2, w = 5 Output: [[1, 2], [1, 2], [1, 2], [2, 1], [2, 1], [1, 2]] ``` The first two windows, [1, 2, 1, 3, 2] and [2, 1, 3, 2, 1], both have 1 and 2 twice each, so the smaller ID 1 leads. In [1, 3, 2, 1, 4], 1 appears twice and 2 wins the three-way tie at one. The next two windows, [3, 2, 1, 4, 2] and [2, 1, 4, 2, 3], have 2 twice and 1 first among the singles, and the last window [1, 4, 2, 3, 1] has 1 twice and 2 first among the singles. ```text Input: ads = [5, 5, 5, 3, 3], k = 1, w = 3 Output: [[5], [5], [3]] ``` The windows are [5, 5, 5], [5, 5, 3] and [5, 3, 3]. Ad 5 leads the first two, and in the last one ad 3 appears twice against once for ad 5. ```text Input: ads = [7, 7, 2], k = 3, w = 2 Output: [[7], [2, 7]] ``` Each window has fewer than 3 distinct ads, so every distinct ad is returned. In [7, 2] both appear once, so the smaller ID 2 comes first.

Overview: Slide a fixed-size window over a log of ad IDs and report the k most frequent IDs in every window, with ties going to the smaller ID. It tests sliding-window count updates and how to keep a frequency ranking current as counts change by one at each step.

Pinterest logo
Pinterest
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given a stream of ad logs as an array of ad IDs ads, a window size w, and an integer k. Find the k most frequent ad IDs within each sliding window of size w.

For each window position, return the k most frequent ad IDs sorted by frequency in descending order. If two ads have the same frequency, the one with the smaller ID comes first. If a window has fewer than k distinct ads, return all of its distinct ads, ordered by the same rules.

Return a list of lists, where each inner list holds the top ad IDs for the corresponding window position.

Function Signature

def top_k_frequent_ads(ads: list[int], k: int, w: int) -> list[list[int]]:

Rules

  • The windows are ads[i:i + w] for i = 0, 1, ..., len(ads) - w , so the result has len(ads) - w + 1 inner lists, in left-to-right order.
  • Within a window, order IDs by count descending, then by ID ascending.
  • Each inner list has min(k, d) IDs, where d is the number of distinct IDs in that window.

Constraints

  • 1 <= len(ads) <= 10^5
  • 1 <= ads[i] <= 10^4
  • 1 <= k <= 100
  • 1 <= w <= len(ads)

Examples

Input: ads = [1, 2, 1, 3, 2, 1, 4, 2, 3, 1], k = 2, w = 5
Output: [[1, 2], [1, 2], [1, 2], [2, 1], [2, 1], [1, 2]]

The first two windows, [1, 2, 1, 3, 2] and [2, 1, 3, 2, 1], both have 1 and 2 twice each, so the smaller ID 1 leads. In [1, 3, 2, 1, 4], 1 appears twice and 2 wins the three-way tie at one. The next two windows, [3, 2, 1, 4, 2] and [2, 1, 4, 2, 3], have 2 twice and 1 first among the singles, and the last window [1, 4, 2, 3, 1] has 1 twice and 2 first among the singles.

Input: ads = [5, 5, 5, 3, 3], k = 1, w = 3
Output: [[5], [5], [3]]

The windows are [5, 5, 5], [5, 5, 3] and [5, 3, 3]. Ad 5 leads the first two, and in the last one ad 3 appears twice against once for ad 5.

Input: ads = [7, 7, 2], k = 3, w = 2
Output: [[7], [2, 7]]

Each window has fewer than 3 distinct ads, so every distinct ad is returned. In [7, 2] both appear once, so the smaller ID 2 comes first.