Sliding Window Max

Python data engineering interview problem. Difficulty: intermediate. Pattern: Sliding Window. About 14 minutes. Part of the Pro drill bank.

Maximum of every contiguous window of size k. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

A monitoring dashboard often needs the largest value in each recent time window. Recomputing every window from scratch works on tiny data but wastes work when windows overlap. For every contiguous window of length k, return its maximum value. The windows start at positions 0, 1, 2 and so on until the list no longer has k values. Write the function max_sliding_window(nums: list, k: int) -> list. The first window [1, 3, -1] has maximum 3, and all windows together produce [3, 3, 5, 5, 6, 7]. The starter file already has the function name and the harness print lines. Keep them, and fill only the function body.

Requirements

  • Return one maximum for each complete window.
  • Keep the output in window start order.
  • Do not include a partial window at the end.

Constraints

  • k is a positive window size and the input can contain negative numbers.
  • Return a list of values, with len(nums) - k + 1 results when the input is long enough.

Examples

Input: max_sliding_window([1, 3, -1, -3, 5, 3, 6, 7], 3) Output: [3, 3, 5, 5, 6, 7] Deque keeps candidates.

Topics: lakebench, python, deque, max.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Sliding Window Max

Interview-style drill: Maximum of every contiguous window of size k.

Implement `max_sliding_window(nums: list, k: int) -> list`. Example: `[1, 3, -1, -3, 5, 3, 6, 7], 3` → `[3, 3, 5, 5, 6, 7]`. Keep the harness.