Stream Cluster Max and Median
Reported by candidates from Snap's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Snap reported this one in October 2026, and it's a streaming problem dressed up as clustering. You get ADD, MAX and MEDIAN operations, and the whole thing hinges on one data structure: two heaps per cluster to track a running median. Representatives never change, so assignment is simple. The median part is where people stall. If you've got the OA coming and you've never built a running median, this is the one to sort out first. StealthCoder sits invisibly on your screen as a safety net if you blank on the heap balancing during the live assessment.
The problem
Process an integer data stream whose values are assigned to stored clusters. A cluster's representative is its first value and never changes. ADD value: among representatives within maxDistance of value, choose the closest; break a distance tie by the smaller zero-based cluster identifier. If none qualifies, create a new cluster. Append the assigned cluster identifier. MAX clusterId: append that cluster's maximum value, or INVALID for an unknown identifier. MEDIAN clusterId: append the exact median, using an integer or an x.5 string, or INVALID for an unknown identifier. Return one string for every operation in order. Function processClusterStream(maxDistance: int, operations: String[]) → String[] Examples Example 1 maxDistance = 3 operations = ["ADD 10","ADD 12","ADD 20","ADD 17","MAX 0","MEDIAN 0","MAX 1","MEDIAN 1"] return = ["0","0","1","1","12","11","20","18.5"] Ten and twelve form cluster 0. Twenty starts cluster 1, and seventeen joins it at distance three from its representative. Example 2 maxDistance = 5 operations = ["ADD 0","ADD 10","ADD 5","MEDIAN 0","MEDIAN 1","MAX 4"] return = ["0","1","0","2.5","10","INVALID"] Value five ties both representatives and joins the smaller cluster identifier. Constraints 0 <= maxDistance <= 10^9. 1 <= operations.length <= 5000. Added values are signed 32-bit integers. Every operation has one of the exact forms above.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Two separate problems live here. First, assignment: scan all representatives, keep those within maxDistance, pick the smallest distance, and break ties by the lower cluster id. With at most 5000 operations, a linear scan per ADD is fine, so don't overbuild it. Second, per-cluster stats. Keep a max-heap for the lower half and a min-heap for the upper half in each cluster, plus a running max value. Rebalance after each insert so sizes differ by at most one. Median is the top of the larger heap if the count is odd, otherwise the average of both tops. Pitfalls: averaging can give x.5, so format it as a string and avoid float drift. Use 64-bit sums for 32-bit values. Distance should also be computed in 64-bit. Return INVALID for ids that don't exist yet. StealthCoder is the hedge if the heap rebalancing slips away mid-OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Stream Cluster Max and Median 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 Snap's OA.
Snap 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.
Stream Cluster Max and Median FAQ
What's the trick in the Snap Stream Cluster Max and Median problem?+
Split it in two. Assignment is a linear scan over representatives with a tie-break on the smaller cluster id. Stats use a two-heap running median per cluster, plus a tracked max. Don't mix the two concerns in one structure.
How hard is this OA really?+
Medium. Nothing here is exotic, but there are several small details: tie-breaking, INVALID ids, and x.5 formatting. If you've done a running median problem before, it's mostly careful implementation rather than new ideas.
Do I need a fancy structure to find the closest representative?+
No. With up to 5000 operations, scanning every representative on each ADD is at most about 25 million checks in the worst case, which is fine. Keep the scan simple and get the tie-break right instead of optimizing.
How do I output the median without floating point bugs?+
Work in integers. For an odd count, print the middle value. For an even count, add the two middle values as 64-bit, then if the sum is even print sum/2, otherwise print the floor division followed by.5. Careful with negatives.
How do I prepare for this in 48 hours?+
Write the two-heap running median from scratch until it's automatic. Then practice the custom assignment loop with ties. Test edge cases: maxDistance of 0, negative values, unknown cluster ids, and a single-element cluster.