Count k-day windows whose average bookings meet a threshold

Quick Overview

Count how many windows of exactly k consecutive days have an average number of bookings at or above a given threshold. It tests the fixed-size sliding window, turning an average comparison into an exact integer sum comparison, and careful handling of window boundaries and equality.

Count k-day windows whose average bookings meet a threshold

Company: Booking.com

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `bookings` where `bookings[i]` represents the number of bookings made on day `i`, an integer `k` representing a window size in days, and an integer `threshold`. Return the number of contiguous windows of exactly `k` consecutive days whose average number of bookings is greater than or equal to `threshold`. A window starting at index `i` covers days `i, i+1, ..., i+k-1`. If `bookings.length < k`, return `0`. ### Function Signature ```python def count_windows_meeting_threshold(bookings: list[int], k: int, threshold: int) -> int: ``` ### Rules - Every start index from `0` to `len(bookings) - k` gives one window. Overlapping windows are counted separately, and two windows with the same values at different positions both count. - The average is the exact mean of the window and need not be an integer. A window qualifies when its average is at least `threshold`, equality included. ### Constraints - `1 <= len(bookings) <= 10⁵` - `1 <= k <= len(bookings)` - `0 <= bookings[i] <= 10⁴` - `1 <= threshold <= 10⁴` ### Examples ```text Input: bookings = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 ``` The six windows have sums 6, 6, 9, 12, 15 and 18. An average of at least 4 over 3 days means a sum of at least 12, so the windows [2,5,5], [5,5,5] and [5,5,8] qualify. ```text Input: bookings = [10,1,1,1,1], k = 1, threshold = 5 Output: 1 ``` Each day is its own window, and only day 0 (10 bookings) reaches 5. ```text Input: bookings = [3,4,4,1], k = 2, threshold = 4 Output: 1 ``` The windows [3,4], [4,4] and [4,1] have averages 3.5, 4 and 2.5. Only [4,4] qualifies, and it counts because its average equals the threshold.

Overview: Count how many windows of exactly k consecutive days have an average number of bookings at or above a given threshold. It tests the fixed-size sliding window, turning an average comparison into an exact integer sum comparison, and careful handling of window boundaries and equality.

Booking.com logo
Booking.com
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array bookings where bookings[i] represents the number of bookings made on day i, an integer k representing a window size in days, and an integer threshold.

Return the number of contiguous windows of exactly k consecutive days whose average number of bookings is greater than or equal to threshold.

A window starting at index i covers days i, i+1, ..., i+k-1. If bookings.length < k, return 0.

Function Signature

def count_windows_meeting_threshold(bookings: list[int], k: int, threshold: int) -> int:

Rules

  • Every start index from 0 to len(bookings) - k gives one window. Overlapping windows are counted separately, and two windows with the same values at different positions both count.
  • The average is the exact mean of the window and need not be an integer. A window qualifies when its average is at least threshold , equality included.

Constraints

  • 1 <= len(bookings) <= 10⁵
  • 1 <= k <= len(bookings)
  • 0 <= bookings[i] <= 10⁴
  • 1 <= threshold <= 10⁴

Examples

Input: bookings = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3

The six windows have sums 6, 6, 9, 12, 15 and 18. An average of at least 4 over 3 days means a sum of at least 12, so the windows [2,5,5], [5,5,5] and [5,5,8] qualify.

Input: bookings = [10,1,1,1,1], k = 1, threshold = 5
Output: 1

Each day is its own window, and only day 0 (10 bookings) reaches 5.

Input: bookings = [3,4,4,1], k = 2, threshold = 4
Output: 1

The windows [3,4], [4,4] and [4,1] have averages 3.5, 4 and 2.5. Only [4,4] qualifies, and it counts because its average equals the threshold.