Longest Browsing Streak Touching At Most K Different Shops
Last updated: November 29, 2026
Quick Overview
Given a user's ordered sequence of shop ids visited on Rakuten Ichiba and an integer k, return the length of the longest contiguous stretch that involves at most k distinct shops.
Rakuten
November 29, 20260
0
0 solved
Given a user's ordered sequence of shop ids visited on Rakuten Ichiba and an integer k, return the length of the longest contiguous stretch that involves at most k distinct shops.
Session analytics on Ichiba look at how focused users are while comparing shops before buying. Phone screens use this to see if a candidate reaches a sliding window with a frequency map and can argue why each index moves at most twice.
What the Interviewer Expects
- Use a variable-size sliding window
- Maintain counts and delete keys that hit zero
- Justify O(n) by amortized pointer movement
- Handle k = 0 and empty input
- Talk through one trace
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
- Return the start and end index of the window, preferring the earliest.
- What if visits arrive as an unbounded stream and you need the answer continuously?
- How would you answer the same question for every k from 1 to K efficiently?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of shop ids (strings) in visit order and integer k >= 0. Output: maximum window length with at most k distinct ids. Contiguous means consecutive page views, not a subsequence....
High-Level Design
Expand the right edge one visit at a time and record the shop in a count map. Whenever the map holds more than k keys, shrink from the left, decrementing counts and removing a shop once its count is z...