FastPrepMean, Median and Mode of a Stream
Problems/Mean, Median and Mode of a Stream

Mean, Median and Mode of a Stream

Microsoft logoMicrosoft● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a review of a Microsoft software engineering internship. The review describes a running-statistics question. It asks for the mean, the median and the most frequent value (mode) of a stream of numbers that never ends, with no operations, tie rules or limits. The round was the first of two interviews.

Here the stream comes as a list of up to 10^4 operations. add x appends a whole number from -10^5 to 10^5, and the three queries (median, mean and mode) report on all numbers added so far. The first operation is always an add. For an even count the median is the average of the two middle values, a tie for the mode goes to the smallest value, and answers are decimals checked to within 10^-5.

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

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 integer x to 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^5 written without leading zeros or a plus sign, or one of "mean", "median", "mode".
  • operations[0] is an add operation.

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

Interview reports that mention this question

  • Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)
  • Microsoft SWE Intern Interviews: 244 Experiences (Part 1 of 6)

Microsoft interview experiences

  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public double[] processStream(String[] operations) {
        // Write your code here.
    }
}
Checking account…