FrontendInterviews.dev

Loading problem…

29. Sliding Window Maximum - Analytics Windows

Hard•
Acceptance: 64.00%
•
🔓3/3 Pro unlocks today

You're building an analytics dashboard that displays maximum values in sliding time windows. For example, showing the maximum sales, page views, or user activity in each 5-minute window over the past hour.

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the maximum value in each sliding window.

Requirements

1. Basic Functionality

  • Find maximum in each sliding window of size k
  • Window slides one position at a time
  • Return array of maximums
  • Handle edge cases (k = 1, k = array length)

Example Usage

maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3);
// [3,3,5,5,6,7]
// Window positions:
// [1 3 -1] -3 5 3 6 7 -> max = 3
// 1 [3 -1 -3] 5 3 6 7 -> max = 3
// 1 3 [-1 -3 5] 3 6 7 -> max = 5
// 1 3 -1 [-3 5 3] 6 7 -> max = 5
// 1 3 -1 -3 [5 3 6] 7 -> max = 6
// 1 3 -1 -3 5 [3 6 7] -> max = 7

Real-World Context

This problem models real analytics features:

  • Analytics dashboards: Maximum values in time windows
  • Performance monitoring: Peak performance in sliding windows
  • Stock prices: Maximum price in time periods
  • Time-series analysis: Analyze trends in sliding windows

Constraints

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length
  • Must be efficient for large arrays