FastPrepK-th Smallest Number in a Stream
Problems/K-th Smallest Number in a Stream

K-th Smallest Number in a Stream

Zillow logoZillow● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported at Zillow by a candidate for an SDE intern role (Summer 2023, United States) who interviewed in October 2022 and got an offer. The candidate reached the company through a campus career event and had two rounds: a first round of one hour and a second round of two hours with two questions. This task was the first question of round 2: while numbers keep coming in, be ready to give the k-th smallest of those seen so far at any point.

The report numbers the two rounds but does not say what kind of interview they were, so we list it under phone screens, our default when the kind of round is not given.

Our choices: the function kthSmallestInStream gets k and nums, the numbers in the order they come in, and returns one answer per arrival: the k-th smallest of the numbers so far, with equal numbers counted separately, or -1 while fewer than k numbers have come in. The report does not say whether k can change, so we keep one k for the whole stream. The limits (up to 10^5 numbers, each from 0 to 10^9, and k from 1 to the count of numbers) are ours as well.

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

Numbers arrive one at a time, and at any moment someone may ask for the k-th smallest number seen so far.

You are given an integer k and an array nums that lists the numbers in the order they arrive. After each arrival, find the k-th smallest of all the numbers that have arrived so far. Equal numbers count separately: if the numbers so far were sorted from smallest to largest, the answer is the one at position k (counting from 1). While fewer than k numbers have arrived, the answer is -1.

Return an array answers of the same length as nums, where answers[i] is the answer right after nums[i] arrives.

Function

kthSmallestInStream(k: int, nums: int[]) → int[]

Examples

Example 1

k = 3nums = [5,1,8,3,2,9]return = [-1,-1,8,5,3,3]

With k = 3, there is no answer until the third number arrives, so the first two answers are -1. After 5, 1 and 8 arrive, the sorted numbers are [1,5,8], so the answer is 8. When 3 arrives they are [1,3,5,8] and the answer is 5; when 2 arrives they are [1,2,3,5,8] and the answer is 3. Finally 9 is larger than all three of the smallest numbers [1,2,3], so the answer stays 3.

Example 2

k = 1nums = [4,4,2]return = [4,4,2]

With k = 1 the answer is the smallest number so far: 4 after the first 4, still 4 after the second 4, and 2 after 2 arrives.

Example 3

k = 2nums = [7,7,1,0]return = [-1,7,7,1]

After the first 7 only one number has arrived, so the answer is -1. The two 7s count as two numbers, so after the second 7 the answer is 7. After 1 the sorted numbers are [1,7,7] and the second smallest is still 7. After 0 they are [0,1,7,7], so the answer becomes 1.

Constraints

  • 1 <= nums.length <= 105
  • 1 <= k <= nums.length
  • 0 <= nums[i] <= 109

More Zillow Phone Screen questions

  • Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
  • Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
  • Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
  • Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
  • Jump GamePHONE SCREEN · Seen Dec 2022
  • Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
  • Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
  • Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
  • Employee Free TimePHONE SCREEN · Seen Oct 2022
  • First Unique Character in a StringPHONE SCREEN · Seen Oct 2022

Similar questions at other companies

  • Top Ten Trades by Notional ValueCitadel · PHONE SCREENCitadel interview questions
  • Merge Three Sorted ArraysMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Top K TradesJane Street · PHONE SCREENJane Street interview questions
  • Merge Three Sorted ArraysSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Halve Values Until All Are at Most kAdobe · PHONE SCREENAdobe interview questions
  • Top K Largest Values with Constant Auxiliary SpaceBAMTECH Media · PHONE SCREEN
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
  • Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
  • Greedy Person-Cake Matching DistanceSnowflake · PHONE SCREENSnowflake interview questions
All Zillow interview questionsSee Zillow hiring insights
Saved
class Solution {
    public int[] kthSmallestInStream(int k, int[] nums) {
        // Write your code here.
    }
}
Checking account…