Longest Shared Browsing Path Between Two Customer Sessions

Last updated: November 29, 2026

Quick Overview

Given two customer sessions as ordered lists of product IDs viewed, return one longest sequence of products that appears in both sessions in the same relative order, not necessarily contiguous.

Amazon
Coding & Algorithms
Machine Learning Engineer
Amazon
November 29, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given two customer sessions as ordered lists of product IDs viewed, return one longest sequence of products that appears in both sessions in the same relative order, not necessarily contiguous.

Amazon personalization teams compare browsing sessions to find shared intent paths. The interviewer checks the longest common subsequence DP and, importantly, that you reconstruct an actual sequence rather than only the length.

What the Interviewer Expects
  • Define dp[i][j] as LCS length of suffixes a[i:] and b[j:]
  • Match extends diagonally, mismatch takes the max of skipping one side
  • Reconstruct by walking the table
  • O(n * m) time and space
  • Mention the O(min(n, m)) space variant for length only
Key Topics to Cover
2D dynamic programming
longest common subsequence
reconstruction
sequence alignment
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 compute only the length in linear space?
  • How would you handle sessions with thousands of views (Hirschberg)?
  • How would you turn this into a similarity score between sessions?
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: two lists of product IDs. Output: a list of IDs forming one longest common subsequence. Ties can be broken arbitrarily....

High-Level Design

Use suffix DP so reconstruction walks forward naturally. dp[i][j] = LCS length of a[i:] and b[j:]:

  • If a[i] == b[j], take it: 1 + dp[i+1][j+1]. Taking a match is always safe, by an exchang...

Submit Your Answer
Markdown supported