Compute sliding-window average excluding top k
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
## Problem
You are given an integer array `nums`, a window size `windowSize = w`, and an integer `k`.
For every contiguous subarray (window) of length `w` as the window slides from left to right by 1, compute the **average of the elements after removing the largest `k` elements in that window**.
More precisely, for each window `nums[i .. i+w-1]`:
1. Identify the `k` largest values in the window (if there are ties, removing any `k` elements among the maximum values is fine).
2. Remove those `k` elements.
3. Compute the arithmetic mean of the remaining `w - k` elements.
Return the list of averages in order, one per window.
## Input
- `nums`: array of integers
- `w`: window size (`1 <= w <= len(nums)`)
- `k`: number of largest elements to exclude (`0 <= k < w`)
## Output
- An array `ans` of length `len(nums) - w + 1`, where `ans[i]` is the average for window starting at index `i`.
## Notes / Clarifications
- Use floating-point averages (do not floor/round unless specified).
- Constraints are not provided; propose an algorithm more efficient than recomputing from scratch for each window.
## Example
If `w = 3`, `k = 1`, and `nums = [1, 3, 2, 6, 4]`:
- Window `[1,3,2]` remove max `3` → average of `[1,2]` = `1.5`
- Window `[3,2,6]` remove max `6` → average of `[3,2]` = `2.5`
- Window `[2,6,4]` remove max `6` → average of `[2,4]` = `3.0`
Return `[1.5, 2.5, 3.0]`.
Quick Answer: This question evaluates a candidate's understanding of sliding-window algorithms, order-statistics maintenance, and data-structure techniques for incremental aggregation when excluding the largest k elements from each window.