Grokking Meta Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
time o(n) - space 0(1)

Shane

Jul 20, 2023

Without heaps of course.

function find_sliding_window_median(nums, k) {     let windowSum = 0, start = 0;     for(let i=0; i<nums.length; i++){       windowSum += nums[i]       if(i >= k-1){         nums[i-1] = (windowSum / k)         windowSum -= nums[start]         start++       }     }     nums.length -=1     return nums   }

1

1

Comments
Comments
L
Lee 2 years ago

This looks like an attempt at sliding window average rather than median. Furthermore, I don't think the code is correct for average anyway because you're updating the nums array in place, and then subtracting the average from the window sum, which is not correct. Everyt...

On this page