Longest Browsing Run Touching at Most K Distinct Boards

Last updated: November 29, 2026

Quick Overview

Given the sequence of board ids for Pins a user viewed in order, find the longest contiguous run that touches at most k distinct boards. Return the start index and the length, preferring the earliest start on ties.

Pinterest
Coding & Algorithms
Software Engineer
Pinterest
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given the sequence of board ids for Pins a user viewed in order, find the longest contiguous run that touches at most k distinct boards. Return the start index and the length, preferring the earliest start on ties.

Pinterest product teams care about how focused a browsing session is, for example whether a user is deep in one wedding board or skimming many topics. This sliding window question appears in technical screens to check that you can maintain an invariant with a hash map and move two pointers correctly.

What the Interviewer Expects
  • Recognizes the at-most-k-distinct sliding window pattern
  • Maintains counts and deletes keys that hit zero so the distinct count is accurate
  • Gets tie-breaking right by only updating on strictly longer windows
  • Handles k of zero and empty input
  • Explains why each index enters and leaves the window once
Key Topics to Cover
sliding window
two pointers
hash maps
invariants
session analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return all maximal runs instead of just the longest?
  • What changes if views carry timestamps and the run must also fit within 10 minutes?
  • How would you compute this for millions of users in a batch job?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of board ids (strings), integer k.
  • Output: (start, length) of the longest window with at most k distinct values.
  • Ties: earliest start. Empty input or k <= 0 returns (0, 0)....
High-Level Design

Keep a window [left, right] and a count of each board inside it. Extend right one step at a time. When the number of distinct boards exceeds k, advance left, decrementing counts and removing boards wh...


Submit Your Answer
Markdown supported