Sliding Window Median of Response Times
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills the naive solution here is even k plus huge values. Google's October 2026 OA asks for the median of every window of size k over a stream of response times, and the obvious sort-each-window approach is where most people stall. This is the sliding window median problem, and the pattern is a sliding window backed by two heaps or an ordered structure. If you've got an invite in your inbox, you need the trick, not a lecture. If your mind goes blank mid-assessment, StealthCoder runs invisibly as a safety net.
The problem
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. Examples Example 1 responseTimes = [1,3,-1,-3,5,3,6,7] k = 3 return = [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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The naive move is sorting every window, which costs O(n k log k) and dies on large inputs. The real approach keeps two balanced halves: a max-heap for the lower half and a min-heap for the upper half. Slide by adding the new value and removing the oldest. Heaps can't delete arbitrary elements cheaply, so use lazy deletion with a hash map of pending removals, and track the logical size of each heap separately. Then rebalance so the sizes differ by at most one. The pitfall is the even k case. Average the two middle values and use floating point division, not integer division. Also watch overflow when summing two large ints, and negatives like in the example. Cast to double before adding. If the lazy deletion bookkeeping gets tangled live, StealthCoder is the hedge that can hand you a clean version without the proctor seeing it.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Sliding Window Median of Response Times cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as sliding window median. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sliding Window Median of Response Times FAQ
What's the trick to Sliding Window Median of Response Times?+
Maintain two heaps, a max-heap for the smaller half and a min-heap for the larger half, and keep them balanced as the window slides. Removal of the outgoing element uses lazy deletion. The median is the top of one heap for odd k, or the average of both tops for even k.
How hard is this problem really?+
It's a hard-tier problem because the idea is simple but the implementation is fiddly. Lazy deletion, size tracking, and rebalancing all have to be right at once. Expect most of your time to go to bugs, not to the idea itself.
What edge cases break a naive solution?+
Even k needs the average of two middles as a float. Large positive or negative values can overflow if you add them as ints before dividing. Duplicates break any approach that removes by value from a set. Also k equal to 1 or k equal to n should work cleanly.
Can I solve it without heaps?+
Yes. A sorted list with binary search insertion and deletion works, giving O(n k) worst case from shifting but simple code. An ordered multiset with two iterators also works in languages that have one. Heaps with lazy deletion give O(n log k) and are the standard answer.
How do I prepare in 48 hours?+
Write the two-heap running median first, then add the sliding removal with a delayed-deletion map. Test with even k, duplicates, and negatives like the example. Rehearse the rebalance step until it's automatic, since that's where most bugs show up.