diff --git a/src/sections/03-algorithmic-thinking/_b032e981/index.jsx b/src/sections/03-algorithmic-thinking/_b032e981/index.jsx index d008605..fb114e3 100644 --- a/src/sections/03-algorithmic-thinking/_b032e981/index.jsx +++ b/src/sections/03-algorithmic-thinking/_b032e981/index.jsx @@ -36,19 +36,20 @@ Leave 10–15 minutes to reflect, share feedback, and then switch roles. Best of luck, and enjoy the practice! 🚀 -## Problem: Reverse Array In-Place +## Problem: Reverse an Array -Write a function that reverses an array in-place without using any built-in reverse methods or creating a new array.`, +Write a function that reverses an array without using any built-in reverse methods. +You don't need to reverse it in-place (modifying the input array without creating a new one). This is advanced and we will cover this next week. But you can try, if you like. +After the mock interview, review and discuss the solution provided, including the version that does reverse it in-place, to preview next week's material.`, exercises: [{ starterCode:`/* -Problem: Reverse Array In-Place +Problem: Reverse Array -Write a function that reverses an array in-place without using any built-in reverse methods or creating a new array. +Write a function that reverses an array without using any built-in reverse methods. Follow-up Questions: - What is the time complexity of your solution? - What is the space complexity? -- Can you solve this using the two-pointer technique? */ function reverseArray(arr) { @@ -67,27 +68,24 @@ function reverseArray(arr) { }`, solution:`/* -Problem: Reverse Array In-Place +Problem: Reverse an Array -Write a function that reverses an array in-place without using any built-in reverse methods or creating a new array. +function reverseArray(arr) { + + let arr2 = []; -Follow-up Questions: -- What is the time complexity of your solution? -- What is the space complexity? -- Can you solve this using the two-pointer technique? -*/ + for (let i=arr.length-1; i>=0; i++){ + arr2.push(arr[i]); + } + + // Return the array + return arr2; + + // Time Complexity: O(n) - we visit each element at most once + // Space Complexity: O(n) - we add a new array that is as large as the current one +} -function reverseArray(arr) { - // Reverse an array in-place without using built-in methods - // Requirements: - // - Modify the original array directly (in-place) - // - Do not use built-in reverse() method - // - Do not create a new array - // - Use only constant extra space - // - // Example: - // Input: [1, 2, 3, 4, 5] - // Output: [5, 4, 3, 2, 1] (original array is modified) +function reverseArray_inPlace(arr) { // Use two-pointer technique let left = 0; @@ -118,12 +116,12 @@ function reverseArray(arr) { try { const reverseArray = new Function(`${code}; return reverseArray;`)(); const arr1 = [1, 2, 3, 4, 5]; - reverseArray(arr1); - const test1 = JSON.stringify(arr1) === JSON.stringify([5, 4, 3, 2, 1]); + const arr1a = reverseArray(arr1); + const test1 = JSON.stringify(arr1a) === JSON.stringify([5, 4, 3, 2, 1]); const arr2 = [10, 20, 30]; - reverseArray(arr2); - const test2 = JSON.stringify(arr2) === JSON.stringify([30, 20, 10]); + const arr2a = reverseArray(arr2); + const test2 = JSON.stringify(arr2a) === JSON.stringify([30, 20, 10]); if (test1 && test2) { return new TestResult({ passed: true }); @@ -142,83 +140,83 @@ function reverseArray(arr) { }, message: "Function should reverse arrays correctly." }, - { - name: "Edge case: single element", - test: (code) => { - try { - const reverseArray = new Function(`${code}; return reverseArray;`)(); - const arr = [42]; - reverseArray(arr); + // { + // name: "Edge case: single element", + // test: (code) => { + // try { + // const reverseArray = new Function(`${code}; return reverseArray;`)(); + // const arr = [42]; + // reverseArray(arr); - if (JSON.stringify(arr) === JSON.stringify([42])) { - return new TestResult({ passed: true }); - } else { - return new TestResult({ - passed: false, - message: `Expected reverseArray([42]) to return [42], got ${JSON.stringify(arr)}` - }); - } - } catch (error) { - return new TestResult({ - passed: false, - message: `Error: ${error.message}` - }); - } - }, - message: "Function should handle single element arrays." - }, - { - name: "Edge case: empty array", - test: (code) => { - try { - const reverseArray = new Function(`${code}; return reverseArray;`)(); - const arr = []; - reverseArray(arr); + // if (JSON.stringify(arr) === JSON.stringify([42])) { + // return new TestResult({ passed: true }); + // } else { + // return new TestResult({ + // passed: false, + // message: `Expected reverseArray([42]) to return [42], got ${JSON.stringify(arr)}` + // }); + // } + // } catch (error) { + // return new TestResult({ + // passed: false, + // message: `Error: ${error.message}` + // }); + // } + // }, + // message: "Function should handle single element arrays." + // }, + // { + // name: "Edge case: empty array", + // test: (code) => { + // try { + // const reverseArray = new Function(`${code}; return reverseArray;`)(); + // const arr = []; + // reverseArray(arr); - if (JSON.stringify(arr) === JSON.stringify([])) { - return new TestResult({ passed: true }); - } else { - return new TestResult({ - passed: false, - message: `Expected reverseArray([]) to return [], got ${JSON.stringify(arr)}` - }); - } - } catch (error) { - return new TestResult({ - passed: false, - message: `Error: ${error.message}` - }); - } - }, - message: "Function should handle empty arrays." - }, - { - name: "In-place modification check", - test: (code) => { - try { - const reverseArray = new Function(`${code}; return reverseArray;`)(); - const arr = [1, 2, 3, 4]; - const originalRef = arr; - reverseArray(arr); + // if (JSON.stringify(arr) === JSON.stringify([])) { + // return new TestResult({ passed: true }); + // } else { + // return new TestResult({ + // passed: false, + // message: `Expected reverseArray([]) to return [], got ${JSON.stringify(arr)}` + // }); + // } + // } catch (error) { + // return new TestResult({ + // passed: false, + // message: `Error: ${error.message}` + // }); + // } + // }, + // message: "Function should handle empty arrays." + // }, + // { + // name: "In-place modification check", + // test: (code) => { + // try { + // const reverseArray = new Function(`${code}; return reverseArray;`)(); + // const arr = [1, 2, 3, 4]; + // const originalRef = arr; + // reverseArray(arr); - // Check if the original array reference was modified - if (originalRef === arr) { - return new TestResult({ passed: true }); - } else { - return new TestResult({ - passed: false, - message: `Function should modify the array in-place. Original array: ${JSON.stringify(originalRef)}` - }); - } - } catch (error) { - return new TestResult({ - passed: false, - message: `Error: ${error.message}` - }); - } - }, - message: "Function should modify the array in-place." - } + // // Check if the original array reference was modified + // if (originalRef === arr) { + // return new TestResult({ passed: true }); + // } else { + // return new TestResult({ + // passed: false, + // message: `Function should modify the array in-place. Original array: ${JSON.stringify(originalRef)}` + // }); + // } + // } catch (error) { + // return new TestResult({ + // passed: false, + // message: `Error: ${error.message}` + // }); + // } + // }, + // message: "Function should modify the array in-place." + // } ] }] }; \ No newline at end of file diff --git a/src/sections/03-algorithmic-thinking/_b892f062/index.jsx b/src/sections/03-algorithmic-thinking/_b892f062/index.jsx index 4bca03d..f7c5678 100644 --- a/src/sections/03-algorithmic-thinking/_b892f062/index.jsx +++ b/src/sections/03-algorithmic-thinking/_b892f062/index.jsx @@ -38,7 +38,10 @@ Best of luck, and enjoy the practice! 🚀 ## Problem: Move Zeros to End -Write a function that moves all zeros in an array to the end while maintaining the relative order of non-zero elements.`, +Write a function that moves all zeros in an array to the end while maintaining the relative order of non-zero elements. +You can do this in place with a swapping algorithm, but that's not necessary. We will cover that next week. +You can just create and return a new array. +After the exercise, please discuss the solution, including the one that modifies the array with swapping, as a preview of next week's material.`, exercises: [{ starterCode:`/* Problem: Move Zeros to End @@ -48,17 +51,12 @@ Write a function that moves all zeros in an array to the end while maintaining t Follow-up Questions: - What is the time complexity of your solution? - What is the space complexity? -- Can you solve this using the two-pointer technique? -- How does swapping compare to the two-pass approach? */ function moveZeroes(nums) { // Move all zeros to the end while maintaining relative order // Requirements: - // - Modify the array in-place // - Maintain the relative order of non-zero elements - // - Do not use extra space for another array - // - Use efficient swapping technique // // Example: // Input: [0, 1, 0, 3, 12] @@ -70,26 +68,26 @@ function moveZeroes(nums) { solution:`/* Problem: Move Zeros to End -Write a function that moves all zeros in an array to the end while maintaining the relative order of non-zero elements. +function moveZeroes(nums) { + + const nums2 = []; + let zeroesToAdd = 0; -Follow-up Questions: -- What is the time complexity of your solution? -- What is the space complexity? -- Can you solve this using the two-pointer technique? -- How does swapping compare to the two-pass approach? -*/ + for (let i = 0; i < nums.length; i++) { + if (nums[i]===0) zeroesToAdd++; + else nums2.push(nums[i]); + } + for (let i = 0; i < zeroesToAdd; i++) { + nums2.push(0); + } + + return nums2 + + // Time Complexity: O(n) - single pass through the array + // Space Complexity: O(1) - only using constant extra space +} -function moveZeroes(nums) { - // Move all zeros to the end while maintaining relative order - // Requirements: - // - Modify the array in-place - // - Maintain the relative order of non-zero elements - // - Do not use extra space for another array - // - Use efficient swapping technique - // - // Example: - // Input: [0, 1, 0, 3, 12] - // Output: [1, 3, 12, 0, 0] +function moveZeroes_inPlace(nums) { // Use single-pass approach with swapping let lastNonZero = 0; @@ -112,12 +110,12 @@ function moveZeroes(nums) { try { const moveZeroes = new Function(`${code}; return moveZeroes;`)(); const arr1 = [0, 1, 0, 3, 12]; - moveZeroes(arr1); + const arr1a = moveZeroes(arr1); const arr2 = [0, 0, 1]; - moveZeroes(arr2); + const arr2a = moveZeroes(arr2); - const test1 = JSON.stringify(arr1) === JSON.stringify([1, 3, 12, 0, 0]); - const test2 = JSON.stringify(arr2) === JSON.stringify([1, 0, 0]); + const test1 = JSON.stringify(arr1a) === JSON.stringify([1, 3, 12, 0, 0]); + const test2 = JSON.stringify(arr2a) === JSON.stringify([1, 0, 0]); if (test1 && test2) { return new TestResult({ passed: true }); @@ -142,9 +140,9 @@ function moveZeroes(nums) { try { const moveZeroes = new Function(`${code}; return moveZeroes;`)(); const arr = [1, 2, 3, 4, 5]; - moveZeroes(arr); + const arr2 = moveZeroes(arr); - if (JSON.stringify(arr) === JSON.stringify([1, 2, 3, 4, 5])) { + if (JSON.stringify(arr2) === JSON.stringify([1, 2, 3, 4, 5])) { return new TestResult({ passed: true }); } else { return new TestResult({ @@ -167,9 +165,9 @@ function moveZeroes(nums) { try { const moveZeroes = new Function(`${code}; return moveZeroes;`)(); const arr = [0, 0, 0, 0]; - moveZeroes(arr); + const arr2 = moveZeroes(arr); - if (JSON.stringify(arr) === JSON.stringify([0, 0, 0, 0])) { + if (JSON.stringify(arr2) === JSON.stringify([0, 0, 0, 0])) { return new TestResult({ passed: true }); } else { return new TestResult({ @@ -192,12 +190,12 @@ function moveZeroes(nums) { try { const moveZeroes = new Function(`${code}; return moveZeroes;`)(); const arr1 = [0]; - moveZeroes(arr1); + const arr1a = moveZeroes(arr1); const arr2 = [5]; - moveZeroes(arr2); + const arr2a = moveZeroes(arr2); - const test1 = JSON.stringify(arr1) === JSON.stringify([0]); - const test2 = JSON.stringify(arr2) === JSON.stringify([5]); + const test1 = JSON.stringify(arr1a) === JSON.stringify([0]); + const test2 = JSON.stringify(arr2a) === JSON.stringify([5]); if (test1 && test2) { return new TestResult({ passed: true });