1785091843

This Week's Challenge: Sliding Window Maximum


Ready to test both your logic *and* your polyglot skills? This week's challenge is to solve the same problem in **3 different languages** — the idea is to compare how each one handles performance, syntax, and data structures. ## <br>Tips before you start Before you jump into code, think about the strategy — the algorithm is the same, but each language has its own traps: - **Python** — use `collections.deque` so you get O(1) pops from both ends. No plain lists, you'll kill performance with `list.pop(0)`. - **JavaScript/TypeScript** — there's no efficient native deque; either implement one with an array + indices (avoiding `shift()`), or use two manual pointers. - **Go / Rust** — here the challenge is type and ownership management (Rust) or slices (Go). A `VecDeque` in Rust is the direct equivalent of Python's deque, but the borrow checker will test you. Golden rule: if your solution runs in **O(n²)**, it doesn't count — the goal is **O(n)**, a single pass through the array. ## <br>The problem Given an array of integers `nums` and an integer `k`, return an array containing the **maximum value of each sliding window of size `k`** as it moves from left to right. **Example:** ``` Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] ``` **First window explained:** `[1,3,-1]` → max is `3` Then it slides: `[3,-1,-3]` → max is `3`, and so on. ### Constraints - `1 <= nums.length <= 10^5` - `k <= nums.length` - Your solution must run in **linear time O(n)** - No using ready-made "window max" library functions — the logic has to be yours ## <br>Reference (Python, so you're not lost) ```python from collections import deque def max_sliding_window(nums, k): dq = deque() # stores indices, keeps values in decreasing order result = [] for i, n in enumerate(nums): # remove indices outside the window while dq and dq[0] <= i - k: dq.popleft() # remove values smaller than the current one (they'll never be the max) while dq and nums[dq[-1]] < n: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result ``` This is just to get the logic into your head — the real challenge is **translating this reasoning** into the other two languages of your choice, without copying patterns one-to-one. ## <br>How to participate 1. Pick **3 languages** (could be Python + 2 of your choice, or none of them — push yourself out of your comfort zone). 2. Post the 3 snippets in an article or in the comments of [this room](https://chat-to.dev/chat?q=voat_code_guys). 3. Bonus karma for anyone who: - Includes the **measured runtime** (a simple benchmark with a large array, like 10⁵ elements). - Explains **one interesting difference** between the languages (e.g. memory management, syntax, performance). Solving it in **O(n²)** still earns participation points, but the podium is only for those who nail the right complexity. 😉 Let's see who manages all 3 languages by Friday!

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Donate
[2026 © Chat-to.dev]