Simplified sliding window attention: floored average of each centered window

Quick Overview

Given token scores and a window size, return the floored average of the window centered at each position, clipped at the ends of the array, as a simplified sliding window attention. It tests running sums, window bounds at the edges and floor division with negative values.

Simplified sliding window attention: floored average of each centered window

Company: Hugging Face

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of token scores `scores` of length `n` and an integer window size `w`, simulate a simplified sliding window attention mechanism. For each position `i`, compute the attention output as the average of the scores within the window centered at `i`. The window at index `i` includes indices from `max(0, i - w // 2)` to `min(n - 1, i + w // 2)` inclusive, where `w // 2` uses integer division. Return an array `result` of length `n` where `result[i]` is the average of the scores in the window centered at index `i`, rounded down (floor) to the nearest integer. ### Function Signature ```python def sliding_window_attention(scores: list[int], window_size: int) -> list[int]: ``` ### Rules - With `h = window_size // 2`, the window for index `i` is `scores[max(0, i - h) : min(n - 1, i + h) + 1]`. It holds up to `2h + 1` elements and is clipped at both ends of the array, so an even `window_size` gives the same windows as `window_size + 1`. - The average divides the window's sum by the number of elements actually in the clipped window. - Rounding is floor, toward negative infinity: an average of -1.5 becomes -2. ### Constraints - `1 <= len(scores) <= 10^5` - `-10^4 <= scores[i] <= 10^4` - `1 <= window_size <= 10^5` ### Examples ```text Input: scores = [1,2,3,4,5], window_size = 2 Output: [1,2,3,4,4] ``` Here `w // 2 = 1`. The windows are `[1,2]`, `[1,2,3]`, `[2,3,4]`, `[3,4,5]` and `[4,5]`, with averages 1.5, 2, 3, 4 and 4.5, which round down to `[1,2,3,4,4]`. ```text Input: scores = [10,20,30,40,50,60], window_size = 4 Output: [20,25,30,40,45,50] ``` Here `w // 2 = 2`, so each window reaches two positions to each side. Index 3 covers indices 1 to 5 with sum 200 over 5 elements, giving 40, and index 0 is clipped to indices 0 to 2 with sum 60 over 3 elements, giving 20. ```text Input: scores = [-5,2,-4,1], window_size = 3 Output: [-2,-3,-1,-2] ``` Here `w // 2 = 1`. The window sums are -3, -7, -1 and -3 over 2, 3, 3 and 2 elements, so the averages are -1.5, about -2.33, about -0.33 and -1.5. Rounding each one down gives `[-2,-3,-1,-2]`.

Overview: Given token scores and a window size, return the floored average of the window centered at each position, clipped at the ends of the array, as a simplified sliding window attention. It tests running sums, window bounds at the edges and floor division with negative values.

Hugging Face logo
Hugging Face
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of token scores scores of length n and an integer window size w, simulate a simplified sliding window attention mechanism. For each position i, compute the attention output as the average of the scores within the window centered at i.

The window at index i includes indices from max(0, i - w // 2) to min(n - 1, i + w // 2) inclusive, where w // 2 uses integer division.

Return an array result of length n where result[i] is the average of the scores in the window centered at index i, rounded down (floor) to the nearest integer.

Function Signature

def sliding_window_attention(scores: list[int], window_size: int) -> list[int]:

Rules

  • With h = window_size // 2 , the window for index i is scores[max(0, i - h) : min(n - 1, i + h) + 1] . It holds up to 2h + 1 elements and is clipped at both ends of the array, so an even window_size gives the same windows as window_size + 1 .
  • The average divides the window's sum by the number of elements actually in the clipped window.
  • Rounding is floor, toward negative infinity: an average of -1.5 becomes -2.

Constraints

  • 1 <= len(scores) <= 10^5
  • -10^4 <= scores[i] <= 10^4
  • 1 <= window_size <= 10^5

Examples

Input: scores = [1,2,3,4,5], window_size = 2
Output: [1,2,3,4,4]

Here w // 2 = 1. The windows are [1,2], [1,2,3], [2,3,4], [3,4,5] and [4,5], with averages 1.5, 2, 3, 4 and 4.5, which round down to [1,2,3,4,4].

Input: scores = [10,20,30,40,50,60], window_size = 4
Output: [20,25,30,40,45,50]

Here w // 2 = 2, so each window reaches two positions to each side. Index 3 covers indices 1 to 5 with sum 200 over 5 elements, giving 40, and index 0 is clipped to indices 0 to 2 with sum 60 over 3 elements, giving 20.

Input: scores = [-5,2,-4,1], window_size = 3
Output: [-2,-3,-1,-2]

Here w // 2 = 1. The window sums are -3, -7, -1 and -3 over 2, 3, 3 and 2 elements, so the averages are -1.5, about -2.33, about -0.33 and -1.5. Rounding each one down gives [-2,-3,-1,-2].