Longest Content Window with Bounded Labels
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Amazon OA, reported in September 2026, is treating each item like a single value and counting items instead of distinct labels. The problem looks like a DP candidate, but it's a sliding window with a frequency map. You get a stream of label sets, a limit k, and you return the longest contiguous window with at most k distinct labels, smallest start on ties. Empty stream returns [-1, -1]. If your mind goes blank mid-assessment, StealthCoder runs invisibly as a safety net, but the pattern below is enough to walk in ready.
The problem
A content stream arrives in order. Each item carries a non-empty set of labels. Given contentLabels and a limit k, find the longest contiguous window of items whose combined label set contains at most k distinct labels. Return the inclusive zero-based endpoints [start, end]. If several windows have the same maximum length, return the one with the smallest start. Return [-1, -1] when the stream is empty. Labels within one item are unique. This finite batch represents the same ordered add(contentLabels) stream followed by a longest() query. Function longestContentWindow(contentLabels: String[][], k: int) → int[] Examples Example 1 contentLabels = [["toxic","ok"],["toxic","ok","spam"]] k = 2 return = [0,0] The first item uses exactly two labels. Including the second item introduces spam, exceeding the limit. Example 2 contentLabels = [["toxic","ok"],["toxic","ok","spam"]] k = 3 return = [0,1] All two items together use the three permitted labels. Example 3 contentLabels = [["a"],["b","c"],["a","b"],["d"]] k = 3 return = [0,2] The first three items use a, b, c. Adding the final item introduces a fourth label. Constraints 0 <= contentLabels.length <= 200000. 1 <= k <= 200000. Each item has between 1 and 20 unique non-empty labels. The total number of label occurrences does not exceed 400000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: distinct labels only grow when you extend right and only shrink when you move left, and a valid window stays valid when you shrink it. That's monotonic, so use two pointers with a hash map of label to count. Add every label of item r, incrementing counts and tracking the number of keys with count above zero. While distinct exceeds k, remove item l's labels, decrementing counts and deleting zeros, then advance l. After each step, compare r-l+1 to the best length and only replace on a strictly longer window, which gives the smallest start on ties. The common pitfall is using a set instead of counts, so removing an item wrongly drops a label that another item in the window still carries. Total work is O(total labels), around 400000, so it's fast. StealthCoder is the hedge if the removal logic slips under pressure on the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Longest Content Window with Bounded Labels cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Longest Content Window with Bounded Labels FAQ
What's the trick in this Amazon longest content window problem?+
Sliding window with a label-to-count hash map. Expand right by adding all labels of an item, then shrink from the left while distinct labels exceed k. Counts matter because several items in the window can share a label, so you only drop it when its count hits zero.
Why not use a set of labels for the window?+
A set can't tell you when a label is truly gone. If two items both carry 'toxic' and you remove one, the label must stay. A count map decrements per occurrence and deletes the key only at zero, keeping the distinct count accurate.
How do I handle ties and the empty case?+
Update the best answer only when the new window is strictly longer than the current best. Since you scan left to right, the earliest start wins automatically. If contentLabels is empty, return [-1, -1] before running the loop.
What's the time complexity and will it pass the limits?+
O(N + total labels), since each item is added once and removed at most once, and each label occurrence is touched twice. With at most 400000 label occurrences, that's comfortably fine. Avoid rebuilding a set per window, which would be quadratic.
How do I prepare for this in 48 hours?+
Write the longest substring with at most k distinct characters solution from memory, then adapt it so each step adds and removes a list of labels. Test on the three examples, plus an empty input and k larger than all labels combined.