Longest Watch Session Touching at Most K Distinct Creators

Last updated: November 29, 2026

Quick Overview

Given the sequence of creator IDs for consecutive Reels a user watched and an integer k, return the length of the longest contiguous stretch that involves at most k distinct creators.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given the sequence of creator IDs for consecutive Reels a user watched and an integer k, return the length of the longest contiguous stretch that involves at most k distinct creators.

A sliding window with a frequency map, framed around Reels sessions and creator diversity. Meta interviewers use it to check that you shrink the window correctly when a new distinct value pushes the count over k.

What the Interviewer Expects
  • Uses a window with a count map keyed by creator
  • Shrinks from the left until the number of distinct creators is back to k
  • Deletes keys whose count reaches zero so len(map) is the distinct count
  • Tracks the best window length after each expansion
  • Gives O(n) time and O(k) space
Key Topics to Cover
sliding window
hash map
distinct count
two pointers
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
  • Count the number of windows with at most k distinct creators.
  • Return the longest window with exactly k distinct creators.
  • The stream is unbounded; how do you report the best window so far with bounded memory?
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: creators, a list of hashable IDs, and k >= 0. Output: the maximum length of a subarray containing at most k distinct IDs. If k = 0 the answer is 0....

High-Level Design

Grow right one step at a time and increment count[creators[right]]. If the map now has more than k keys, advance left, decrementing counts and deleting keys that hit zero, until it has k keys ag...


Submit Your Answer
Markdown supported