Longest Shared Viewing Sequence Between Two Users' Watch Histories
Last updated: November 29, 2026
Quick Overview
Given two lists of video IDs watched by two users in order, return one longest sequence of video IDs that appears as a subsequence of both histories.
Meta
November 29, 20260
0
0 solved
Given two lists of video IDs watched by two users in order, return one longest sequence of video IDs that appears as a subsequence of both histories.
A typical Meta phone screen problem dressed in Facebook Watch or Reels data: the interviewer wants a clean 2D DP and, crucially, the reconstruction of the actual sequence rather than just its length. Expect to be pushed on memory once you have the table.
What the Interviewer Expects
- Define dp[i][j] as the LCS length of the first i and first j items
- Write the recurrence for equal and unequal tails
- Reconstruct one optimal sequence by walking the table backwards
- State O(nm) time and discuss O(min(n,m)) space for length only
- Test on empty and disjoint histories
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the length using only two rows of memory?
- What if you need every distinct longest sequence, not just one?
- How would you handle histories of a million items each?
- How does this change if the shared run must be contiguous?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Inputs are two lists a and b of video IDs (any hashable). Output one longest list s such that s is a subsequence of both, meaning the items keep their relative order but need not be adjacent. ...
High-Level Design
Let dp[i][j] be the length of the longest common subsequence of a[:i] and b[:j]. If a[i-1] == b[j-1], the best answer uses that shared item, so dp[i][j] = dp[i-1][j-1] + 1. Otherwise one of ...