FastPrepSliding Window Maximum
Problems/Sliding Window Maximum

Sliding Window Maximum

PeopleStrong logoPeopleStrong● HardFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The candidate reports Sliding Window Maximum from brute force to optimal, with dry runs and follow-ups. The interface and bounds are authored; core similarity is about 95%.

Given an integer array nums and a window size k, return the maximum value in every consecutive window of exactly k elements. Return the maxima in order from the leftmost window to the rightmost.

For this exercise, assume the array is nonempty and the window size is valid. Values may be negative and may repeat.

Function

maxSlidingWindow(nums: int[], k: int) → int[]

Examples

Example 1

nums = [4,2,12,3,8,7]k = 3return = [12,12,12,8]

The four windows are [4,2,12], [2,12,3], [12,3,8] and [3,8,7]. Their maxima are 12, 12, 12 and 8.

Example 2

nums = [-4,-2,-9]k = 1return = [-4,-2,-9]

Each one-element window returns its own value, including negative values.

Constraints

  • For this exercise, assume 1 <= nums.length <= 10^5.
  • 1 <= k <= nums.length.
  • -10^9 <= nums[i] <= 10^9.

More PeopleStrong problems

  • Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
  • Four SumONSITE INTERVIEW · Seen Mar 2026
  • LRU Cache OperationsONSITE INTERVIEW · Seen Mar 2026
All PeopleStrong interview questionsSee PeopleStrong hiring insights
Saved
public int[] maxSlidingWindow(int[] nums, int k) {
    // Write your solution here.
}
Checking account…