Stream Cluster Max and Median
Updated
Problem statement
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 withinmaxDistanceofvalue, 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, orINVALIDfor an unknown identifier.MEDIAN clusterId: append the exact median, using an integer or anx.5string, orINVALIDfor an unknown identifier.
Return one string for every operation in order.
Function
processClusterStream(maxDistance: int, operations: String[]) → String[]Examples
Example 1
maxDistance = 3operations = ["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 = 5operations = ["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.
More Snap Inc. interview questions
- ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
- Basic Calculator IIPHONE SCREEN · Seen Oct 2026
- LRU Cache with Thread-Safety Follow-UpPHONE SCREEN · Seen Sep 2026
- Smallest Range Covering Elements from K ListsPHONE SCREEN · Seen Sep 2026
- Most Stones Removed with Same Row or ColumnPHONE SCREEN · Seen Sep 2026
- Report Island Areas in Discovery OrderPHONE SCREEN · Seen Aug 2026
- Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
- Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
- Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
- Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Query Weekly Store Opening HoursApple · PHONE SCREENApple interview questions
- Interview Slot BookingTrueFoundry · PHONE SCREEN
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Credit Accounts with Scheduled TransfersAirbnb · OAAirbnb interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Log Analyzer: Busiest Service in a Rolling WindowByteDance · PHONE SCREENByteDance interview questions
- Consistent-Hash Packet RouterAmazon · PHONE SCREEN
- Secret Number Search with One-Call-Delayed FeedbackOpenAI · PHONE SCREENOpenAI interview questions
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN