├── README.md ├── cyclic-sort ├── a-intro.js ├── b-find-missing-number.js └── c-find-all-missing-numbers.js ├── fast-pointers └── a-intro-linkedlist-cycle.js ├── in-place-reversal └── a-reversse-linkedlist.js ├── merge-intervals └── a-merge-intervals-intro.js ├── modified-binary-search └── a-binary-search.js ├── sliding-window ├── a-Intro-Sub-array-average.js └── b-max-sum-sub-array.js ├── subsets ├── a-distinct-subsets.js └── b-distinct-duplicate-subset.js ├── tree-bfs ├── a-1-queue.js ├── a-intro-print-each-level-tree.js └── b-reverse-level-order.js ├── tree-dfs └── a-binary-tree-path-sum.js └── two-pointers ├── a-intro-pair-with-target-sum.js ├── b-remove-dups-v2.js ├── b-remove-dups.js └── c-square-sorted-array.js /README.md: -------------------------------------------------------------------------------- 1 | # coding-interview-patterns 2 | Implementation of Grokking Coding patterns for interviews 3 | -------------------------------------------------------------------------------- /cyclic-sort/a-intro.js: -------------------------------------------------------------------------------- 1 | /* 2 | Cyclic in place sorting 3 | Input: [3, 1, 5, 4, 2] 4 | Output: [1, 2, 3, 4, 5] */ 5 | 6 | var arr = [3, 1, 5, 4, 2]; 7 | 8 | function cyclicSort(arr) { 9 | for (var i = 0; i < arr.length; i++) { 10 | var j = arr[i] - 1; 11 | if (arr[i] !== j) { 12 | swap(arr, i, j); 13 | } 14 | } 15 | 16 | return arr; 17 | } 18 | 19 | function swap(arr, i, j) { 20 | var temp = arr[i]; 21 | arr[i] = arr[j]; 22 | arr[j] = temp; 23 | } 24 | 25 | var result = cyclicSort(arr); 26 | console.log("result", result); 27 | -------------------------------------------------------------------------------- /cyclic-sort/b-find-missing-number.js: -------------------------------------------------------------------------------- 1 | /* 2 | We are given an array containing ‘n’ distinct numbers taken from the range 0 to ‘n’. Since the array has only ‘n’ numbers out of the total ‘n+1’ numbers, find the missing number. 3 | 4 | Example 1: 5 | 6 | Input: [4, 0, 3, 1] 7 | Output: 2 8 | Example 2: 9 | 10 | Input: [8, 3, 5, 2, 4, 6, 0, 1] 11 | Output: 7 12 | Input: [8, 3, 5, 2, 4, 6, 0, 1] 13 | Output: 7 14 | */ 15 | 16 | var arr = [8, 3, 5, 2, 4, 6, 0, 1]; 17 | 18 | function cyclicSortMissingNumber(arr) { 19 | var i = 0; 20 | 21 | while (i < arr.length) { 22 | if (arr[i] < arr.length && arr[i] != i) { 23 | swap(arr, arr[i], i); 24 | } else { 25 | i++; 26 | } 27 | } 28 | 29 | for (var j = 0; j < arr.length; j++) { 30 | if (arr[j] !== j) { 31 | return j; 32 | } 33 | } 34 | 35 | return -1; 36 | } 37 | 38 | function swap(arr, i, j) { 39 | var temp = arr[i]; 40 | arr[i] = arr[j]; 41 | arr[j] = temp; 42 | } 43 | 44 | var result = cyclicSortMissingNumber(arr); 45 | console.log("result", result); 46 | -------------------------------------------------------------------------------- /cyclic-sort/c-find-all-missing-numbers.js: -------------------------------------------------------------------------------- 1 | /* 2 | We are given an unsorted array containing numbers taken from the range 1 to ‘n’. The array can have duplicates, which means some numbers will be missing. Find all those missing numbers. 3 | 4 | Example 1: 5 | 6 | Input: [2, 3, 1, 8, 2, 3, 5, 1] 7 | Output: 4, 6, 7 8 | Explanation: The array should have all numbers from 1 to 8, due to duplicates 4, 6, and 7 are missing. 9 | Example 2: 10 | 11 | Input: [2, 4, 1, 2] 12 | Output: 3 13 | Example 3: 14 | 15 | Input: [2, 3, 2, 1] 16 | Output: 4 17 | Input: [2, 3, 2, 1] 18 | Output: 4 19 | */ 20 | 21 | var arr = [2, 3, 1, 8, 2, 3, 5, 1]; 22 | 23 | function cyclicSortMissingNumbers(arr) { 24 | var i = 0; 25 | var result = []; 26 | 27 | while (i < arr.length) { 28 | var curr = arr[i] - 1; 29 | if (curr !== i && arr[curr] !== curr) { 30 | swap(arr, i, curr); 31 | } 32 | i++; 33 | } 34 | 35 | for (var j = 0; j < arr.length; j++) { 36 | if (arr[j] - 1 !== j) { 37 | result.push(j + 1); 38 | } 39 | } 40 | 41 | return result; 42 | } 43 | 44 | function swap(arr, i, j) { 45 | var temp = arr[i]; 46 | arr[i] = arr[j]; 47 | arr[j] = temp; 48 | } 49 | 50 | var result = cyclicSortMissingNumbers(arr); 51 | console.log("result", result); 52 | -------------------------------------------------------------------------------- /fast-pointers/a-intro-linkedlist-cycle.js: -------------------------------------------------------------------------------- 1 | function Node(data) { 2 | this.data = data; 3 | this.next = null; 4 | } 5 | 6 | function printNodes(head) { 7 | var currNode = head; 8 | while (currNode !== null) { 9 | console.log(currNode.data); 10 | currNode = currNode.next; 11 | } 12 | } 13 | 14 | var head = new Node(1); 15 | head.next = new Node(2); 16 | head.next.next = new Node(3); 17 | head.next.next.next = new Node(4); 18 | head.next.next.next.next = new Node(5); 19 | head.next.next.next.next.next = new Node(6); 20 | 21 | head.next.next.next.next.next.next = head.next.next; 22 | head.next.next.next.next.next.next = head.next.next.next; 23 | // printNodes(head); 24 | 25 | function hasCycle(head) { 26 | var slow = head; 27 | var fast = head; 28 | 29 | while (slow !== null && fast !== null) { 30 | slow = slow.next; 31 | fast = fast.next && fast.next.next; 32 | console.log("Print: slow, fast", slow && slow.data, fast && fast.data); 33 | 34 | if (slow === fast) { 35 | return true; 36 | } 37 | } 38 | 39 | return false; 40 | } 41 | 42 | var hasCycleResult = hasCycle(head); 43 | console.log("Has cycle", hasCycleResult); 44 | -------------------------------------------------------------------------------- /in-place-reversal/a-reversse-linkedlist.js: -------------------------------------------------------------------------------- 1 | function Node(data) { 2 | this.data = data; 3 | this.next = null; 4 | } 5 | 6 | function printNodes(head) { 7 | var currNode = head; 8 | while (currNode !== null) { 9 | console.log(currNode.data); 10 | currNode = currNode.next; 11 | } 12 | } 13 | 14 | var head = new Node(1); 15 | head.next = new Node(2); 16 | head.next.next = new Node(3); 17 | head.next.next.next = new Node(4); 18 | head.next.next.next.next = new Node(5); 19 | head.next.next.next.next.next = new Node(6); 20 | 21 | // printNodes(head); 22 | 23 | function reverseLinkedlist(head) { 24 | var newHead = null; 25 | var currNode = head; 26 | 27 | while (currNode != null) { 28 | var nextNode = currNode.next; 29 | 30 | currNode.next = newHead; 31 | newHead = currNode; 32 | currNode = nextNode; 33 | } 34 | 35 | printNodes(newHead); 36 | } 37 | 38 | reverseLinkedlist(head); 39 | -------------------------------------------------------------------------------- /merge-intervals/a-merge-intervals-intro.js: -------------------------------------------------------------------------------- 1 | /* Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals. 2 | 3 | Example 1: 4 | 5 | Intervals: [[1,4], [2,5], [7,9]] 6 | Output: [[1,5], [7,9]] 7 | Explanation: Since the first two intervals [1,4] and [2,5] overlap, we merged them into one [1,5]. 8 | Intervals: [[1,4], [2,5], [7,9]] 9 | Output: [[1,5], [7,9]] 10 | Explanation: Since the first two intervals [1,4] and [2,5] overlap, we merged them into one [1,5]. */ 11 | 12 | function mergeIntervals(arr) { 13 | var result = []; 14 | 15 | if (arr.length < 2) { 16 | return arr; 17 | } 18 | 19 | arr = arr.sort((a, b) => a[0] - b[0]); 20 | console.log("sorted array", arr); 21 | 22 | var i = 0; 23 | var curr = arr[i]; 24 | var start = curr[0]; 25 | var end = curr[1]; 26 | 27 | while (i < arr.length) { 28 | var next = arr[i + 1]; 29 | 30 | if (!next) { 31 | result.push([start, end]); 32 | break; 33 | } 34 | 35 | if (next[0] <= end) { 36 | end = Math.max(end, next[1]); 37 | } else { 38 | console.log("push arr", start, end, i); 39 | result.push([start, end]); 40 | start = next[0]; 41 | end = next[1]; 42 | } 43 | 44 | i++; 45 | } 46 | 47 | return result; 48 | } 49 | 50 | var arr = [[1, 4], [2, 5], [7, 9]]; 51 | var result = mergeIntervals(arr); 52 | console.log("result:", result); 53 | -------------------------------------------------------------------------------- /modified-binary-search/a-binary-search.js: -------------------------------------------------------------------------------- 1 | /* Given a sorted array of numbers, find if a given number ‘key’ is present in the array. Though we know that the array is sorted, we don’t know if it’s sorted in ascending or descending order. You should assume that the array can have duplicates. 2 | 3 | Write a function to return the index of the ‘key’ if it is present in the array, otherwise return -1. 4 | 5 | Example 1: 6 | 7 | 10 8 | Input: [4, 6, 10], key = 10 9 | Output: 2 10 | Example 2: 11 | 12 | Input: [1, 2, 3, 4, 5, 6, 7], key = 5 13 | Output: 4 14 | Example 3: 15 | 16 | 10, 6, 4 17 | Input: [10, 6, 4], key = 10 18 | Output: 0 19 | Example 4: 20 | 21 | Input: [10, 6, 4], key = 4 22 | Output: 2 */ 23 | 24 | var arr = [1, 2, 3, 4, 5, 6, 7]; 25 | var arr1 = [10, 6, 4]; 26 | var key = 5; 27 | var key1 = 4; 28 | 29 | function binarySearch(arr, key) { 30 | var counter = 100; 31 | var start = 0; 32 | var end = arr.length - 1; 33 | var isAscending = arr[start] < arr[end]; 34 | // ascending 35 | while (start <= end) { 36 | var mid = parseInt((start + end) / 2, 10); 37 | 38 | if (arr[mid] === key) { 39 | return mid; 40 | } else if (key < arr[mid] && isAscending) { 41 | end = mid - 1; 42 | } else { 43 | start = mid + 1; 44 | } 45 | } 46 | 47 | return -1; 48 | } 49 | 50 | var result = binarySearch(arr, key); 51 | var result1 = binarySearch(arr1, key1); 52 | console.log("result:", result); 53 | console.log("result1:", result1); 54 | -------------------------------------------------------------------------------- /sliding-window/a-Intro-Sub-array-average.js: -------------------------------------------------------------------------------- 1 | // Given an array, find the average of all subarrays of size ‘K’ in it. 2 | 3 | var arr = [1, 3, 2, 6, -1, 4, 1, 8, 2], 4 | k = 5; 5 | 6 | function subArrayAverage(arr, k) { 7 | var windowStart = 0; 8 | var sum = 0; 9 | var result = []; 10 | for (var windowEnd = 0; windowEnd < arr.length; windowEnd++) { 11 | sum += arr[windowEnd]; 12 | 13 | if (windowEnd >= k - 1) { 14 | result.push(sum / k); 15 | sum -= arr[windowStart]; 16 | windowStart++; 17 | } 18 | } 19 | 20 | return result; 21 | } 22 | 23 | var result = subArrayAverage(arr, k); 24 | console.log("result:", result); 25 | -------------------------------------------------------------------------------- /sliding-window/b-max-sum-sub-array.js: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of positive numbers and a positive number ‘k’, find the maximum sum of any subarray of size ‘k’. 3 | 4 | Example 1: 5 | 6 | Input: [2, 1, 5, 1, 3, 2], k=3 7 | Output: 9 8 | Explanation: Subarray with maximum sum is [5, 1, 3]. 9 | Example 2: 10 | 11 | Input: [2, 3, 4, 1, 5], k=2 12 | Output: 7 13 | Explanation: Subarray with maximum sum is [3, 4]. 14 | */ 15 | -------------------------------------------------------------------------------- /subsets/a-distinct-subsets.js: -------------------------------------------------------------------------------- 1 | /* Given a set with distinct elements, find all of its distinct subsets. 2 | 3 | Example 1: 4 | 5 | Input: [1, 3] 6 | Output: [], [1], [3], [1,3] 7 | Example 2: 8 | 9 | Input: [1, 5, 3] 10 | Output: [], [1], [5], [3], [1,5], [1,3], [5,3], [1,5,3] 11 | Input: [1, 5, 3] 12 | Output: [], [1], [5], [3], [1,5], [1,3], [5,3], [1,5,3] */ 13 | 14 | var arr = [1, 5, 3]; 15 | 16 | function generateSubset(arr) { 17 | var result = [[]]; // start with empty subset 18 | 19 | for (var i = 0; i < arr.length; i++) { 20 | var size = result.length; 21 | for (var j = 0; j < size; j++) { 22 | var currSubResult = result[j]; 23 | var newSubResult = currSubResult.concat([arr[i]]); 24 | result.push(newSubResult); 25 | console.log("result", JSON.stringify(result)); 26 | } 27 | } 28 | return result; 29 | } 30 | 31 | var result = generateSubset(arr); 32 | console.log("result:", result); 33 | -------------------------------------------------------------------------------- /subsets/b-distinct-duplicate-subset.js: -------------------------------------------------------------------------------- 1 | /* Given a set of numbers that might contain duplicates, find all of its distinct subsets. 2 | 3 | Example 1: 4 | 5 | Input: [1, 3, 3] 6 | Output: [], [1], [3], [1,3], [3,3], [1,3,3] 7 | Example 2: 8 | 9 | Input: [1, 5, 3, 3] 10 | Output: [], [1], [5], [3], [1,5], [1,3], [5,3], [1,5,3], [3,3], [1,3,3], [3,3,5], [1,5,3,3] 11 | Input: [1, 5, 3, 3] 12 | Output: [], [1], [5], [3], [1,5], [1,3], [5,3], [1,5,3], [3,3], [1,3,3], [3,3,5], [1,5,3,3] */ 13 | 14 | var arr = [1, 3, 3]; 15 | 16 | function generateSubset(arr) { 17 | var result = [[]]; // start with empty subset 18 | var start = 0; 19 | var end = 0; 20 | var lastCopiedIndex = 0; 21 | 22 | for (var i = 0; i < arr.length; i++) { 23 | var size = result.length; 24 | end = result.length; 25 | if (i > 0 && arr[i] !== arr[i - 1]) { 26 | // current duplicate number 27 | lastCopiedIndex = 0; 28 | } 29 | console.log("START:Lastindex", lastCopiedIndex); 30 | for (var j = lastCopiedIndex; j < end; j++) { 31 | var currSubResult = result[j]; 32 | var newSubResult = currSubResult.concat([arr[i]]); 33 | result.push(newSubResult); 34 | lastCopiedIndex = j + 1; 35 | console.log( 36 | "lastCopiedIndex, result", 37 | lastCopiedIndex, 38 | JSON.stringify(result) 39 | ); 40 | } 41 | } 42 | return result; 43 | } 44 | 45 | var result = generateSubset(arr); 46 | console.log("result:", result); 47 | -------------------------------------------------------------------------------- /tree-bfs/a-1-queue.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yashuvit89/coding-interview-patterns/ccdbbe8f0ae88e57a0c1c4b49b93a2f6a97324d0/tree-bfs/a-1-queue.js -------------------------------------------------------------------------------- /tree-bfs/a-intro-print-each-level-tree.js: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | constructor(value) { 3 | this.value = value; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | 9 | const traverse = function(root) { 10 | var result = []; 11 | var avgResult = []; 12 | var queue = []; 13 | 14 | queue.push(root); 15 | var levelSize = 0; 16 | 17 | while (queue.length !== 0) { 18 | var levelSize = queue.length; 19 | var currentLevel = []; 20 | var currentSum = 0; 21 | for (var i = 0; i < levelSize; i++) { 22 | var currentNode = queue.shift(); 23 | var left = currentNode.left; 24 | var right = currentNode.right; 25 | 26 | currentLevel.push(currentNode.value); 27 | currentSum += currentNode.value; 28 | if (left) { 29 | queue.push(left); 30 | } 31 | 32 | if (right) { 33 | queue.push(right); 34 | } 35 | } 36 | 37 | avgResult.push(currentSum / levelSize); 38 | result.push(currentLevel); 39 | } 40 | 41 | console.log("Average", avgResult); 42 | return JSON.stringify(result); 43 | // return JSON.stringify(avgResult); 44 | }; 45 | 46 | var root = new TreeNode(12); 47 | root.left = new TreeNode(7); 48 | root.right = new TreeNode(1); 49 | root.left.left = new TreeNode(9); 50 | root.right.left = new TreeNode(10); 51 | root.right.right = new TreeNode(5); 52 | 53 | console.log(`Level order traversal: ${traverse(root)}`); 54 | -------------------------------------------------------------------------------- /tree-bfs/b-reverse-level-order.js: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | constructor(value) { 3 | this.value = value; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | 9 | const traverse = function(root) { 10 | var queue = []; 11 | var result = []; 12 | 13 | queue.push(root); 14 | 15 | while (queue.length !== 0) { 16 | var levelElements = []; 17 | var queueLength = queue.length; 18 | for (var i = 0; i < queueLength; i++) { 19 | var currNode = queue.shift(); 20 | levelElements.push(currNode.value); 21 | if (currNode.left) { 22 | queue.push(currNode.left); 23 | } 24 | 25 | if (currNode.right) { 26 | queue.push(currNode.right); 27 | } 28 | } 29 | 30 | result.unshift(levelElements); 31 | } 32 | 33 | return JSON.stringify(result); 34 | }; 35 | 36 | var root = new TreeNode(12); 37 | root.left = new TreeNode(7); 38 | root.right = new TreeNode(1); 39 | root.left.left = new TreeNode(9); 40 | root.right.left = new TreeNode(10); 41 | root.right.right = new TreeNode(5); 42 | 43 | console.log(`Reverse Level order traversal: ${traverse(root)}`); 44 | -------------------------------------------------------------------------------- /tree-dfs/a-binary-tree-path-sum.js: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | constructor(value) { 3 | this.value = value; 4 | this.left = null; 5 | this.right = null; 6 | } 7 | } 8 | const hasPath = function(root, target) { 9 | if (root == null) { 10 | return false; 11 | } 12 | 13 | if (root.left == null && root.right == null && root.value == target) { 14 | return true; 15 | } 16 | 17 | return ( 18 | hasPath(root.left, target - root.value) || 19 | hasPath(root.right, target - root.value) 20 | ); 21 | }; 22 | 23 | var root = new TreeNode(12); 24 | root.left = new TreeNode(7); 25 | root.right = new TreeNode(1); 26 | root.left.left = new TreeNode(9); 27 | root.right.left = new TreeNode(10); 28 | root.right.right = new TreeNode(5); 29 | 30 | console.log(`Has Path: ${hasPath(root, 23)}`); 31 | // console.log(`Has Path: ${hasPath(root, 16)}`); 32 | -------------------------------------------------------------------------------- /two-pointers/a-intro-pair-with-target-sum.js: -------------------------------------------------------------------------------- 1 | /* 2 | Given an array of sorted numbers and a target sum, find a pair in the array whose sum is equal to the given target. 3 | 4 | Write a function to return the indices of the two numbers (i.e. the pair) such that they add up to the given target. 5 | 6 | Example 1: 7 | 8 | Input: [1, 2, 3, 4, 6], target=6 9 | Output: [1, 3] 10 | Explanation: The number at index 1 and 3 add up to 6: 2+4=6 11 | Example 2: 12 | 13 | Input: [2, 5, 9, 11], target=11 14 | Output: [0, 2] 15 | Explanation: The number at index 0 and 1 add up to 11: 2+9=11 16 | Input: [2, 5, 9, 11], target=11 17 | Output: [0, 2] 18 | Explanation: The number at index 0 and 1 add up to 11: 2+9=11 19 | */ 20 | 21 | var arr = [1, 2, 3, 4, 6], 22 | target = 6; 23 | 24 | function pairWithTargetSum(arr, target) { 25 | var start = 0; 26 | var end = arr.length - 1; 27 | 28 | while (start < end) { 29 | var sum = arr[start] + arr[end]; 30 | if (sum === target) { 31 | return [start, end]; 32 | } else if (sum < target) { 33 | start++; 34 | } else { 35 | end--; 36 | } 37 | } 38 | return -1; 39 | } 40 | 41 | var result = pairWithTargetSum(arr, target); 42 | console.log("result:", result); 43 | -------------------------------------------------------------------------------- /two-pointers/b-remove-dups-v2.js: -------------------------------------------------------------------------------- 1 | /* Given an unsorted array of numbers and a target ‘key’, remove all instances of ‘key’ in-place and return the new length of the array. 2 | 3 | Example 1: 4 | 5 | Input: [3, 2, 3, 6, 3, 10, 9, 3], Key=3 6 | Output: 4 7 | Explanation: The first four elements after removing every 'Key' will be [2, 6, 10, 9]. 8 | Example 2: 9 | 10 | Input: [2, 11, 2, 2, 1], Key=2 11 | Output: 2 12 | Explanation: The first two elements after removing every 'Key' will be [11, 1]. 13 | Input: [2, 11, 2, 2, 1], Key=2 14 | Output: 2 15 | Explanation: The first two elements after removing every 'Key' will be [11, 1]. 16 | */ 17 | 18 | var arr = [3, 2, 3, 6, 3, 10, 9, 3]; 19 | 20 | function removeDups(arr, key) { 21 | var j = 0; // non-key index 22 | 23 | for (var i = 0; i < arr.length; i++) { 24 | if (arr[i] !== key) { 25 | arr[j] = arr[i]; 26 | j++; 27 | } 28 | } 29 | 30 | return j; 31 | } 32 | 33 | var result = removeDups(arr, 3); 34 | console.log("result:", result); 35 | -------------------------------------------------------------------------------- /two-pointers/b-remove-dups.js: -------------------------------------------------------------------------------- 1 | /* Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in-place return the new length of the array. 2 | 3 | Example 1: 4 | 5 | Input: [2, 3, 3, 3, 6, 9, 9] 6 | Output: 4 7 | Explanation: The first four elements after removing the duplicates will be [2, 3, 6, 9]. 8 | Example 2: 9 | 10 | Input: [2, 2, 2, 11] 11 | Output: 2 12 | Explanation: The first two elements after removing the duplicates will be [2, 11]. 13 | Input: [2, 2, 2, 11] 14 | Output: 2 15 | Explanation: The first two elements after removing the duplicates will be [2, 11]. 16 | */ 17 | 18 | var arr = [2, 3, 3, 3, 6, 9, 9]; 19 | 20 | function findDuplicates(arr) { 21 | var i = 0; // non-duplciate index 22 | var j = 1; // duplicate index 23 | 24 | while (j < arr.length && i <= j) { 25 | if (arr[i] !== arr[j]) { 26 | arr[i + 1] = arr[j]; 27 | i++; 28 | j++; 29 | } else { 30 | j++; 31 | } 32 | } 33 | 34 | return i + 1; 35 | } 36 | 37 | var result = findDuplicates(arr); 38 | console.log("result:", result); 39 | -------------------------------------------------------------------------------- /two-pointers/c-square-sorted-array.js: -------------------------------------------------------------------------------- 1 | /* Given a sorted array, create a new array containing squares of all the number of the input array in the sorted order. 2 | 3 | Example 1: 4 | 5 | Input: [-2, -1, 0, 2, 3] 6 | Output: [0, 1, 4, 4, 9] 7 | Example 2: 8 | 9 | Input: [-3, -1, 0, 1, 2] 10 | Output: [0 1 1 4 9] 11 | Input: [-3, -1, 0, 1, 2] 12 | Output: [0 1 1 4 9] */ 13 | 14 | var arr = [-2, -1, 0, 2, 3]; 15 | 16 | function squareNumbers(arr) { 17 | var result = []; 18 | var positiveIndex = 0; 19 | 20 | while (positiveIndex < arr.length) { 21 | if (arr[positiveIndex] >= 0) { 22 | break; 23 | } 24 | positiveIndex++; 25 | } 26 | 27 | var negativeIndex = positiveIndex - 1; 28 | 29 | while (negativeIndex >= 0 && positiveIndex < arr.length) { 30 | var postiveNum = arr[positiveIndex]; 31 | var negativeNum = arr[negativeIndex]; 32 | if (Math.abs(postiveNum) > Math.abs(negativeNum)) { 33 | result.push(negativeNum * negativeNum); 34 | negativeIndex--; 35 | } else { 36 | result.push(postiveNum * postiveNum); 37 | positiveIndex++; 38 | } 39 | } 40 | 41 | return result; 42 | } 43 | 44 | var result = squareNumbers(arr); 45 | console.log("result:", result); 46 | 47 | // TODO: last number is missing in output 48 | --------------------------------------------------------------------------------