FastPrepK Smallest Values in a Stream
Problems/K Smallest Values in a Stream

K Smallest Values in a Stream

WhatNot logoWhatNot● EasyINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An applicant for a software intern role, who was later offered the job and turned it down, describes this coding question from a data structures round around October 2022. Their process had a Karat screen, a recruiter stage, two 45-minute technical interviews with engineers, and an interview on the company's principles run by one of its leaders. The report does not say which of these interviews held this question.

The task was to hold on to the smallest k numbers of a stream that has no fixed end. No input format or limits are given, so here the stream is a list of arrivals mixed with requests, and each request returns the current smallest k numbers in order.

Our version is about 75% the same as the reported question.

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 integer x arrives.
  • "GET": a request for the smallest k numbers 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 <= 100
  • 1 <= events.length <= 2 * 104
  • Each event is "GET" or "ADD x" with a single space, where x is an integer written without leading zeros.
  • -109 <= x <= 109
  • At least one event is "GET", and all the returned lists together hold at most 104 numbers.

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

WhatNot interview experiences

  • Whatnot SWE New Grad Second Technical Interview: 8 Experiences
  • Whatnot interviews · October 2026
  • Whatnot Experienced SWE Phone Screen Questions: 2 Experiences
  • Whatnot SWE Interview Questions: 6 Experiences
All WhatNot interview questionsSee WhatNot hiring insights
Saved
class Solution {
    public int[][] kSmallest(int k, String[] events) {
        // write your code here
    }
}
Checking account…