Sliding Window Median of Response Times
Updated
Problem statement
A stream of integer response times is supplied in arrival order as responseTimes. Given a window size k, return the median of every complete window containing the most recent k response times.
The first output corresponds to responseTimes[0..k - 1]. Each later output removes the oldest value and includes the next arrival.
- For an odd-sized window, the median is the middle value after sorting.
- For an even-sized window, the median is the arithmetic mean of the two middle values.
- Duplicate response times are separate records and must be retained with their full multiplicity.
Return the medians from left to right as a double[].
Function
medianSlidingWindow(responseTimes: int[], k: int) → double[]Examples
Example 1
responseTimes = [1,3,-1,-3,5,3,6,7]k = 3return = [1.0,-1.0,-1.0,3.0,5.0,6.0]The first sorted window is [-1, 1, 3], whose median is 1. Sliding one position at a time produces the remaining medians.
Example 2
responseTimes = [1,2,3,4]k = 2return = [1.5,2.5,3.5]Each window has two values, so its median is the mean of that pair.
Example 3
responseTimes = [5,5,5]k = 2return = [5.0,5.0]Equal values remain separate records. Removing one expired 5 and adding another keeps both window medians equal to 5.
Constraints
1 <= k <= responseTimes.length <= 200000.-10^9 <= responseTimes[i] <= 10^9.- Your solution should use
O(k)auxiliary memory.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Sliding Window MedianNotion · ONSITE INTERVIEWNotion interview questions
- Sliding Window MedianBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Smallest Range Covering Elements from K ListsSnap Inc. · PHONE SCREENSnap Inc. interview questions
- Auction Sniping Window MinimumCisco · OACisco interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Longest Subarray with K Distinct ValuesAmazon · ONSITE INTERVIEW