Mean, Median and Mode of a Stream
Updated
Problem statement
Numbers arrive one at a time from a stream that may never end, so you cannot wait to see them all. Process a list of operations in order:
"add x"adds the integerxto the numbers seen so far. It produces no answer."mean"answers with the average of all numbers seen so far."median"answers with the middle value of the numbers seen so far in sorted order. If there is an even count, it is the average of the two middle values."mode"answers with the value that has been added the most times. If several values tie, it is the smallest of them.
Return the answers of all mean, median and mode operations in order, as decimal numbers. An answer is accepted if it is within 10^-5 of the exact value.
Function
processStream(operations: String[]) → double[]Examples
Example 1
operations = ["add 3","add 1","mean","median","mode","add 1","mean","median","mode"]return = [2.0,2.0,1.0,1.6666666666666667,1.0,1.0]After adding 3 and 1: the mean is (3 + 1) / 2 = 2; the median of the sorted list [1, 3] is the average of the two middle values, 2; both values appear once, so the mode is the smaller one, 1. After adding another 1, the list is [1, 1, 3]: the mean is 5 / 3 = 1.666..., the median is the middle value 1, and the mode is 1 (twice).
Example 2
operations = ["add -4","add 10","add 10","add -4","add 7","mode","median","mean"]return = [-4.0,7.0,3.8]The list is [-4, -4, 7, 10, 10] when sorted. -4 and 10 both appear twice, so the mode is the smaller value, -4. The median is the middle value 7, and the mean is 19 / 5 = 3.8.
Constraints
1 <= operations.length <= 10^4- Each
operations[i]is"add x"with an integer-10^5 <= x <= 10^5written without leading zeros or a plus sign, or one of"mean","median","mode". operations[0]is anaddoperation.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Design Authentication ManagerOracle · ONSITE INTERVIEWOracle interview questions
- Lowest-Bid Auction SystemGoogle · ONSITE INTERVIEWGoogle interview questions
- Mutable Stock Price Top KBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Notification Deduplication WindowGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Helpdesk Ticket ProcessingThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Execute Tasks by PrioritySnowflake · ONSITE INTERVIEWSnowflake interview questions
- Highest-Earning Experience TrackerRoblox · ONSITE INTERVIEWRoblox interview questions
- Cancellable Priority Task QueueCitadel · ONSITE INTERVIEWCitadel interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions