From 39bd2594cfabb0cce4d20420e9c0307b206b89e6 Mon Sep 17 00:00:00 2001 From: afekTheMiniLearner <100536372+afekTheMiniLearner@users.noreply.github.com> Date: Thu, 11 May 2023 13:50:47 +0300 Subject: [PATCH] refactored some recursive content including tests and deleted question that is from test and not related to interview question, also its complicated to understand its description --- .../recursive/__tests__/merge-sort.test.js | 12 +-- code-questions/recursive/merge-sort.js | 29 +++++- .../recursive/merge-two-sorted-lists.js | 94 +++++++++---------- code-questions/recursive/symbole-print.js | 20 ---- 4 files changed, 77 insertions(+), 78 deletions(-) delete mode 100644 code-questions/recursive/symbole-print.js diff --git a/code-questions/recursive/__tests__/merge-sort.test.js b/code-questions/recursive/__tests__/merge-sort.test.js index 3f625d6..b9d8bb2 100644 --- a/code-questions/recursive/__tests__/merge-sort.test.js +++ b/code-questions/recursive/__tests__/merge-sort.test.js @@ -6,14 +6,14 @@ describe('merge-sort tests', () => { [1, 3, 4, 5, 6, 7, -9, 0, 2], [-9, 0, 1, 2, 3, 4, 5, 6, 7], ], - [ - [41, 3, 5, 6, 7, -9, 0, 2], - [-9, 0, 2, 3, 5, 6, 7, 41], - ], [ [-1, -9, -88, 0, -4, 2], [-88, -9, -4, -1, 0, 2], ], + [ + [3, 2, 1, 0], + [0, 1, 2, 3], + ], [ [1, 2, 3], [1, 2, 3], @@ -22,10 +22,6 @@ describe('merge-sort tests', () => { [32, -7], [-7, 32], ], - [ - [3, 2, 1, 0], - [0, 1, 2, 3], - ], [[1], [1]], [[], []], ])('function accepts arr: %s, then return result: %s', (arr, res) => { diff --git a/code-questions/recursive/merge-sort.js b/code-questions/recursive/merge-sort.js index 2ad5afb..7632145 100644 --- a/code-questions/recursive/merge-sort.js +++ b/code-questions/recursive/merge-sort.js @@ -1,4 +1,30 @@ -// Implement merge sort +/* Given an array arr[], its starting position l and its ending position r. +Sort the array using merge sort algorithm. + +Example 1: + Input: + N = 5 + arr[] = {4 1 3 9 7} + Output: + 1 3 4 7 9 + +Example 2: + Input: + N = 10 + arr[] = {10 9 8 7 6 5 4 3 2 1} + Output: + 1 2 3 4 5 6 7 8 9 10 + +Your Task: + You don't need to take the input or print anything. + Your task is to complete the function merge() which takes + arr[], l, m, r as its input parameters and modifies arr[] in-place + such that it is sorted from position l to position r, + and function mergeSort() which uses merge() to sort the array + in ascending order using merge sort algorithm. + +Expected Time Complexity: O(NLogN) +Expected Auxiliary Space: O(N) */ module.exports.mergeSort = function (initialArray) { return initialArray.length ? halving(initialArray) : initialArray; @@ -22,7 +48,6 @@ module.exports.mergeSort = function (initialArray) { case arr1[0] <= arr2[0]: sortedArray.push(arr1.shift(0, 1)); break; - case arr1.length === 0: case arr2[0] < arr1[0]: sortedArray.push(arr2.shift(0, 1)); diff --git a/code-questions/recursive/merge-two-sorted-lists.js b/code-questions/recursive/merge-two-sorted-lists.js index a02b8ac..03b30c6 100644 --- a/code-questions/recursive/merge-two-sorted-lists.js +++ b/code-questions/recursive/merge-two-sorted-lists.js @@ -1,37 +1,35 @@ -/* Given two sorted linked lists consisting of N and M nodes -respectively. The task is to merge both of the list (in-place) +/* Given two sorted linked lists consisting of N and M nodes respectively. +The task is to merge both of the list (in-place) and return head of the merged list. Example 1: - -Input: -N = 4, M = 3 -valueN[] = {5,10,15,40} -valueM[] = {2,3,20} - -Output: 2 3 5 10 15 20 40 - -Explanation: After merging the two linked -lists, we have merged list as 2, 3, 5, -10, 15, 20, 40. + Input: + N = 4 + M = 3 + valueN[] = {5,10,15,40} + valueM[] = {2,3,20} + Output: + 2 3 5 10 15 20 40 + Explanation: + After merging the two linked lists, + we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: - -Input: -N = 2, M = 2 -valueN[] = {1,1} -valueM[] = {2,4} - -Output: 1 1 2 4 - -Explanation: After merging the given two -linked list , we have 1, 1, 2, 4 as -output. + Input: + N = 2 + M = 2 + valueN[] = {1,1} + valueM[] = {2,4} + Output: + 1 1 2 4 + Explanation: + After merging the given two linked list, + we have 1, 1, 2, 4 as output. Your Task: -The task is to complete the function sortedMerge() -which takes references to the heads of two linked lists -as the arguments and returns the head of merged linked list. + The task is to complete the function sortedMerge() + which takes references to the heads of two linked lists + as the arguments and returns the head of merged linked list. Expected Time Complexity : O(n+m) Expected Auxilliary Space : O(1) */ @@ -47,28 +45,28 @@ module.exports.sortedMerge = function (head1, head2) { return head1.value < head2.value ? head1 : head2; function rec(node1, node2) { - if (node1.next === null) { - if (node2 !== null) node1.next = node2; - return; - } - - if (node1.value <= node2.value) { - prev1 = node1; - rec(node1.next, node2); - } else { - if (!prev1) { + switch (true) { + case node1.next === null: + node1.next = node2 || node1.next; + return; + case node1.value <= node2.value: prev1 = node1; - node1 = node1.next; - prev2 = node2; - node2 = node2.next; - prev2.next = prev1; - } else { - prev2 = node2; - node2 = node2.next; - prev1.next = prev2; - prev2.next = node1; - } - rec(node1, node2); + rec(node1.next, node2); + break; + default: + if (!prev1) { + prev1 = node1; + node1 = node1.next; + prev2 = node2; + node2 = node2.next; + prev2.next = prev1; + } else { + prev2 = node2; + node2 = node2.next; + prev1.next = prev2; + prev2.next = node1; + } + rec(node1, node2); } } }; diff --git a/code-questions/recursive/symbole-print.js b/code-questions/recursive/symbole-print.js deleted file mode 100644 index e0b1bfe..0000000 --- a/code-questions/recursive/symbole-print.js +++ /dev/null @@ -1,20 +0,0 @@ -/* Q: write a function that accept n (positive and odd number) -and symbol, then print the symbol in symmetric way by using recoursion -example: for input ('@',5) the function will print: -@@@@@ -@@@ -@ -@@@ -@@@@@ -*/ - -function printSymbol(symbol, n) { - if (n < 0) return; - if (n > 1) console.log(symbol.repeat(n)); - - printSymbol(symbol, n - 2); - - console.log(symbol.repeat(n)); -} - -printSymbol('$', 7);