Home › Company-wise questions › PayPal › Sliding Window Maximum ← PayPal bank
PayPal · Question 70 · Queues
Sliding Window Maximum Queues Java code ⏱ O(n) 💾 O(k)
Product-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Find the maximum in each sliding window of size k in an array.
Approach Use a deque to maintain max candidates.
Time O(n)Space O(k)
How it works 1 Evict stale indices, then beaten values. 2 Head always holds the window maximum. Sample input / output Input:
nums = [1, 3, -1, -3, 5, 3, 6, 7], k = 3
Output:
[3, 3, 5, 5, 6, 7] Constraints 1 <= nums.length <= 10^5.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(k)Loops 3Branches 2Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat yes ↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE static int[] maxSlidingWindow(int[] nums, int k) if (nums == null || k <= 0) return new int[0] DEFINE n = nums.length DEFINE [] result = new int[n - k + 1] Deque<Integer> deque = new LinkedList<>() for (int i = 0; i < nums.length; i++) while (!deque.isEmpty() && deque.peekFirst() < … deque.pollFirst() while (!deque.isEmpty() && nums[deque.peekLast()… deque.pollLast() deque.offerLast(i) if (i >= k - 1) result[i - k + 1] = nums[deque.peekFirst()] RETURN result DEFINE static void main(String[] args) System.out.println(Arrays.toString(maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3))) END start / end statement condition loop returns definition← Q69 · Kth Largest Element in an Array Q71 · Longest Increasing Path in a Matrix → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →