|
| 1 | +import { TestResult } from "../../utils/test_utils"; |
| 2 | + |
| 3 | +export const codeExerciseFixedWindow = { |
| 4 | + id: 'a8d1c7e2', |
| 5 | + title: 'Module 8 - In Class Code Exercise One', |
| 6 | + sectionId: 'sliding-window', |
| 7 | + previousChapterId: null, |
| 8 | + content: ` |
| 9 | + Hi 👋, |
| 10 | +
|
| 11 | +### Group Exercise Rules |
| 12 | +- Be kind 💜 |
| 13 | +- Make sure everyone gets a chance to contribute |
| 14 | +- Ask questions out loud — don’t suffer in silence |
| 15 | +- It’s okay to be wrong, mistakes are part of the process |
| 16 | +- Focus on learning together, not just getting the answer |
| 17 | +
|
| 18 | +## Problem (Fixed Window): Maximum Sum Subarray of Size K |
| 19 | +
|
| 20 | +Given an array of integers and a number \`k\`, find the maximum sum of any contiguous subarray of size \`k\`. |
| 21 | +
|
| 22 | +### Examples |
| 23 | +- Input: \`arr = [2, 1, 5, 1, 3, 2], k = 3\` → Output: \`9\` (subarray \`[5, 1, 3]\`) |
| 24 | +- Input: \`arr = [2, 3, 4, 1, 5], k = 2\` → Output: \`7\` (subarray \`[3, 4]\`) |
| 25 | +- Input: \`arr = [1, 4, 2, 9, 5], k = 4\` → Output: \`20\` (subarray \`[4, 2, 9, 5]\`) |
| 26 | +
|
| 27 | +### Follow-up Questions |
| 28 | +- What is the time complexity of your initial (brute-force) solution? |
| 29 | +- What is the time complexity after refactoring to sliding window? |
| 30 | +- What is the space complexity in both cases?`, |
| 31 | + exercise: { |
| 32 | + starterCode:`/* |
| 33 | +Problem: Maximum Sum Subarray of Size K (Fixed Window) |
| 34 | +
|
| 35 | +Given an array of integers and a number k, find the maximum sum of any contiguous subarray of size k. |
| 36 | +
|
| 37 | +Brute-force approach (inefficient but correct): try all windows and compute their sums from scratch. |
| 38 | +Time: O(n * k), Space: O(1) |
| 39 | +*/ |
| 40 | +
|
| 41 | +function maxSumSubarray(arr, k) { |
| 42 | + if (!Array.isArray(arr) || k <= 0 || k > arr.length) return 0; |
| 43 | + let max = -Infinity; |
| 44 | + for (let start = 0; start + k <= arr.length; start++) { |
| 45 | + let sum = 0; |
| 46 | + for (let i = start; i < start + k; i++) { |
| 47 | + sum += arr[i]; |
| 48 | + } |
| 49 | + if (sum > max) max = sum; |
| 50 | + } |
| 51 | + return max === -Infinity ? 0 : max; |
| 52 | +}`, |
| 53 | + solution:`/* |
| 54 | +Refactor to Sliding Window: |
| 55 | +- Compute sum of first k elements once |
| 56 | +- Slide by removing leftmost and adding next rightmost |
| 57 | +Time: O(n), Space: O(1) |
| 58 | +*/ |
| 59 | +
|
| 60 | +function maxSumSubarray(arr, k) { |
| 61 | + if (!Array.isArray(arr) || k <= 0 || k > arr.length) return 0; |
| 62 | +
|
| 63 | + // sum of first window |
| 64 | + let windowSum = 0; |
| 65 | + for (let i = 0; i < k; i++) windowSum += arr[i]; |
| 66 | +
|
| 67 | + let maxSum = windowSum; |
| 68 | +
|
| 69 | + // slide the window |
| 70 | + for (let right = k; right < arr.length; right++) { |
| 71 | + windowSum += arr[right] - arr[right - k]; |
| 72 | + if (windowSum > maxSum) maxSum = windowSum; |
| 73 | + } |
| 74 | +
|
| 75 | + return maxSum; |
| 76 | +}`, |
| 77 | + tests:[ |
| 78 | + { |
| 79 | + name: "Basic fixed-window cases", |
| 80 | + test: (code) => { |
| 81 | + try { |
| 82 | + const maxSumSubarray = new Function(`${code}; return maxSumSubarray;`)(); |
| 83 | + const t1 = maxSumSubarray([2, 1, 5, 1, 3, 2], 3) === 9; |
| 84 | + const t2 = maxSumSubarray([2, 3, 4, 1, 5], 2) === 7; |
| 85 | + const t3 = maxSumSubarray([1, 4, 2, 9, 5], 4) === 20; |
| 86 | + return (t1 && t2 && t3) |
| 87 | + ? new TestResult({ passed: true }) |
| 88 | + : new TestResult({ passed: false, message: "Basic cases failed." }); |
| 89 | + } catch (e) { |
| 90 | + return new TestResult({ passed: false, message: `Error: ${e.message}` }); |
| 91 | + } |
| 92 | + }, |
| 93 | + message: "Function should find maximum sum subarray of size k." |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Negative numbers allowed", |
| 97 | + test: (code) => { |
| 98 | + try { |
| 99 | + const maxSumSubarray = new Function(`${code}; return maxSumSubarray;`)(); |
| 100 | + const t1 = maxSumSubarray([-1, -2, -3, -4], 2) === -3; // [-1,-2] |
| 101 | + const t2 = maxSumSubarray([1, -2, 3, -4, 5], 2) === 1; // [-4,5] |
| 102 | + const t3 = maxSumSubarray([-5, -1, -3, -2], 3) === -6; // [-1,-3,-2] |
| 103 | + return (t1 && t2 && t3) |
| 104 | + ? new TestResult({ passed: true }) |
| 105 | + : new TestResult({ passed: false, message: "Negative number cases failed." }); |
| 106 | + } catch (e) { |
| 107 | + return new TestResult({ passed: false, message: `Error: ${e.message}` }); |
| 108 | + } |
| 109 | + }, |
| 110 | + message: "Function should handle arrays with negative numbers." |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "Edge cases", |
| 114 | + test: (code) => { |
| 115 | + try { |
| 116 | + const maxSumSubarray = new Function(`${code}; return maxSumSubarray;`)(); |
| 117 | + const t1 = maxSumSubarray([5], 1) === 5; |
| 118 | + const t2 = maxSumSubarray([1, 2, 3], 3) === 6; |
| 119 | + const t3 = maxSumSubarray([], 1) === 0; |
| 120 | + const t4 = maxSumSubarray([1, 2, 3], 0) === 0; |
| 121 | + const t5 = maxSumSubarray([1, 2], 3) === 0; |
| 122 | + return (t1 && t2 && t3 && t4 && t5) |
| 123 | + ? new TestResult({ passed: true }) |
| 124 | + : new TestResult({ passed: false, message: "Edge cases failed." }); |
| 125 | + } catch (e) { |
| 126 | + return new TestResult({ passed: false, message: `Error: ${e.message}` }); |
| 127 | + } |
| 128 | + }, |
| 129 | + message: "Function should handle edge cases correctly." |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "Larger array sanity", |
| 133 | + test: (code) => { |
| 134 | + try { |
| 135 | + const maxSumSubarray = new Function(`${code}; return maxSumSubarray;`)(); |
| 136 | + const largeArr = [1,2,3,4,5,6,7,8,9,10]; |
| 137 | + const got = maxSumSubarray(largeArr, 3); |
| 138 | + return (got === 27) |
| 139 | + ? new TestResult({ passed: true }) |
| 140 | + : new TestResult({ passed: false, message: `Expected 27, got ${got}` }); |
| 141 | + } catch (e) { |
| 142 | + return new TestResult({ passed: false, message: `Error: ${e.message}` }); |
| 143 | + } |
| 144 | + }, |
| 145 | + message: "Function should work efficiently with larger arrays." |
| 146 | + } |
| 147 | + ] |
| 148 | + } |
| 149 | +}; |
| 150 | + |
0 commit comments