K-th Smallest Number in a Stream
Updated
Problem statement
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 <= 1051 <= k <= nums.length0 <= 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