Count Distinct Active Pins per Time Segment from Overlapping Interaction Logs

Read the full interview experience this question came from →

Quick Overview

Given pin interaction logs with start and end times, count how many distinct pins are active in each time segment when the same pin can have many overlapping intervals from different users. Tests interval processing over arbitrary timestamps, deduplication by pin, and precise half-open boundary handling.

Count Distinct Active Pins per Time Segment from Overlapping Interaction Logs

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are given interaction logs for pins. Each log `[pin_id, start, end]` means that some user was interacting with pin `pin_id` during the half-open time interval `[start, end)`. For every elementary time segment defined by the logs, report how many distinct pins were being interacted with. The same pin can appear in many logs, because several users can interact with it, and its intervals can overlap. A pin counts once in a segment, no matter how many of its intervals cover that segment. This is the last step of a three-step progression. The first version limited timestamps to a 60-second window (0 to 59) and assumed each pin appeared in at most one log. The second version removed the window limit, allowing arbitrary timestamps. This version also removes the one-log-per-pin assumption. ### Function Signature ```python def distinct_active_pins(logs: list[list[int]]) -> list[list[int]]: ... ``` ### Rules - Let `T` be the sorted list of distinct values that appear as a `start` or an `end` in any log. - For each pair of consecutive values `T[i] < T[i + 1]`, output one row `[T[i], T[i + 1], count]`. Here `count` is the number of distinct `pin_id` values that have at least one log with `start <= T[i]` and `T[i + 1] <= end`. - Output the rows in ascending order of their start time. Output a row for every consecutive pair, including rows whose count is `0` and neighboring rows with equal counts. Do not merge rows. - Intervals are half-open. A log that ends at time `t` and another that starts at time `t` do not overlap. - If `logs` is empty, return an empty list. ### Constraints - `0 <= len(logs) <= 100000` - `0 <= pin_id <= 10^9` - `0 <= start < end <= 10^12` - Timestamps can exceed `2^31 - 1`, so use 64-bit integers. - Logs are unsorted, and some may be exact duplicates of each other. ### Examples Input: `logs = [[7, 1, 5], [7, 3, 8], [9, 4, 6]]` Output: `[[1, 3, 1], [3, 4, 1], [4, 5, 2], [5, 6, 2], [6, 8, 1]]` During `[3, 5)`, two of pin 7's logs overlap, but pin 7 is counted only once. Input: `logs = [[1, 0, 2], [2, 5, 6]]` Output: `[[0, 2, 1], [2, 5, 0], [5, 6, 1]]`

Overview: Given pin interaction logs with start and end times, count how many distinct pins are active in each time segment when the same pin can have many overlapping intervals from different users. Tests interval processing over arbitrary timestamps, deduplication by pin, and precise half-open boundary handling.

Read the full Pinterest Software Engineer interview experience this question came from

|Home/Coding & Algorithms/Pinterest
Pinterest logo
Pinterest
Sep 2, 2026
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

You are given interaction logs for pins. Each log [pin_id, start, end] means that some user was interacting with pin pin_id during the half-open time interval [start, end). For every elementary time segment defined by the logs, report how many distinct pins were being interacted with.

The same pin can appear in many logs, because several users can interact with it, and its intervals can overlap. A pin counts once in a segment, no matter how many of its intervals cover that segment.

This is the last step of a three-step progression. The first version limited timestamps to a 60-second window (0 to 59) and assumed each pin appeared in at most one log. The second version removed the window limit, allowing arbitrary timestamps. This version also removes the one-log-per-pin assumption.

Function Signature

def distinct_active_pins(logs: list[list[int]]) -> list[list[int]]:
    ...

Rules

  • Let T be the sorted list of distinct values that appear as a start or an end in any log.
  • For each pair of consecutive values T[i] < T[i + 1] , output one row [T[i], T[i + 1], count] . Here count is the number of distinct pin_id values that have at least one log with start <= T[i] and T[i + 1] <= end .
  • Output the rows in ascending order of their start time. Output a row for every consecutive pair, including rows whose count is 0 and neighboring rows with equal counts. Do not merge rows.
  • Intervals are half-open. A log that ends at time t and another that starts at time t do not overlap.
  • If logs is empty, return an empty list.

Constraints

  • 0 <= len(logs) <= 100000
  • 0 <= pin_id <= 10^9
  • 0 <= start < end <= 10^12
  • Timestamps can exceed 2^31 - 1 , so use 64-bit integers.
  • Logs are unsorted, and some may be exact duplicates of each other.

Examples

Input: logs = [[7, 1, 5], [7, 3, 8], [9, 4, 6]]

Output: [[1, 3, 1], [3, 4, 1], [4, 5, 2], [5, 6, 2], [6, 8, 1]]

During [3, 5), two of pin 7's logs overlap, but pin 7 is counted only once.

Input: logs = [[1, 0, 2], [2, 5, 6]]

Output: [[0, 2, 1], [2, 5, 0], [5, 6, 1]]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...