Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.getAverages(nums, k). For each index with k values on both sides, return the floor average of that length-(2k+1) window; return -1 at other indices.

Starter code

class Solution:
    def getAverages(self, nums, k):
        pass
Test cases

radius-three

{
  "args": [
    [
      7,
      4,
      3,
      9,
      1,
      8,
      5,
      2,
      6
    ],
    3
  ]
}

Expected: [-1,-1,-1,5,4,4,-1,-1,-1]

Wizard outline
  1. Step 1: Initialize Solution.getAverages

    Replace the empty starter with the first real state owned by Solution.getAverages. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Radius Zero case

    Complete the readable core algorithm for one representative Interview case. Slide a fixed-size sum and write each average at the window's center index.

  4. Step 4: Harden the Radius Three boundary

    Repair the reviewed boundary and pass the complete submission contract. The initial sum represents the first complete centered window. Every update removes its outgoing value and adds its incoming value, so each written floor average is exact; indices without a complete window remain -1.

Footguns and prerequisites
  • The output index is right-k, not the current right or left edge.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Slide a Fixed Window(opens in a new tab)

    Slide a Fixed Window isolates before appending a result, the rolling total contains exactly values[right - width + 1:right + 1]. That focused state discipline is required when implementing k radius subarray averages as a complete Interview Problem.

Recommended approach and implementation

Initialize a -1 result, return early if the window is too wide, then slide a length-(2k+1) sum and write sum//width at each center.

Why it works: The initial sum represents the first complete centered window. Every update removes its outgoing value and adds its incoming value, so each written floor average is exact; indices without a complete window remain -1.

class Solution:
    def getAverages(self, nums, k):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        n = len(nums)
        output = [-1] * n
        width = 2 * k + 1
        if width > n:
            return output
        window_sum = sum(nums[:width])
        output[k] = window_sum // width
        for right in range(width, n):
            window_sum += nums[right] - nums[right - width]
            output[right - k] = window_sum // width
        return output