K Smallest Values in a Stream
Updated
Problem statement
Numbers arrive one at a time in a stream that may never end. You are given an integer k and the array events, in arrival order. Each event is one of these:
"ADD x": the integerxarrives."GET": a request for the smallestknumbers among all that arrived before this event.
Equal numbers count separately: if 5 arrives twice, both copies can be among the smallest. If fewer than k numbers have arrived, return all of them.
For each GET, return the requested numbers sorted from smallest to largest. Return one list per GET, in the order the events appear.
Function
kSmallest(k: int, events: String[]) → int[][]Examples
Example 1
k = 3events = ["ADD 7","ADD 2","ADD 9","GET","ADD 4","ADD 1","GET"]return = [[2,7,9],[1,2,4]]At the first GET only three values have arrived, so all of them are returned, smallest first: [2,7,9]. Then 4 and 1 arrive, and the three smallest of the five values are [1,2,4].
Example 2
k = 2events = ["GET","ADD 5","GET","ADD 5","ADD 8","GET"]return = [[],[5],[5,5]]The first GET comes before any value, so it returns an empty list. The second one has only one value to return. At the third, the values are 5, 5 and 8, and both copies of 5 count.
Example 3
k = 2events = ["ADD -1","ADD 0","ADD -1000000000","GET","ADD 3","GET"]return = [[-1000000000,-1],[-1000000000,-1]]The two smallest of -1, 0 and -1000000000 are -1000000000 and -1. The new value 3 is larger than both, so the second answer is the same.
Example 4
k = 1events = ["ADD 6","GET","ADD 6","ADD 10","GET","ADD -2","GET"]return = [[6],[6],[-2]]With k = 1 each GET returns only the smallest value so far: 6, still 6 after 6 and 10 arrive, then -2.
Constraints
1 <= k <= 1001 <= events.length <= 2 * 104- Each event is
"GET"or"ADD x"with a single space, wherexis an integer written without leading zeros. -109 <= x <= 109- At least one event is
"GET", and all the returned lists together hold at most104numbers.
More WhatNot Onsite Interview questions
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Detect Blocked Chat PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Minimum Remove to Make Valid ParenthesesONSITE INTERVIEW · Seen Dec 2024
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2024
- Merge Two Sorted ListsONSITE INTERVIEW · Seen Jul 2024
- Number of Submatrices That Sum to TargetONSITE INTERVIEW · Seen Jul 2024
- Parse and Store Log RecordsONSITE INTERVIEW · Seen Oct 2023
- Room Occupancy at Query TimesONSITE INTERVIEW · Seen Oct 2023
- Average Purchase PriceONSITE INTERVIEW · Seen Sep 2022
Similar questions at other companies
- Config Store with Priority OverridesMicrosoft · PHONE SCREENMicrosoft interview questions
- Majority ElementByteDance · PHONE SCREENByteDance interview questions
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Recommend Films from Viewers Who Watched Everything You DidBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Total Years of Work ExperienceLinkedIn · PHONE SCREENLinkedIn interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Count Adjacent Symbol Pairs for BPEAmazon · ONSITE INTERVIEW
- Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Merge Sorted ArrayDP World · ONSITE INTERVIEW