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
Coding & Algorithms
Software Engineer
Rakuten
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

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
sliding window
two pointers
frequency maps
amortized 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
  • 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions