Skip to content

Commit

Permalink
refactored some recursive content including tests
Browse files Browse the repository at this point in the history
and deleted question that is from test and not
related to interview question, also its complicated to understand its description
  • Loading branch information
Afek-Sakaju committed May 11, 2023
1 parent 6f0588c commit 39bd259
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 78 deletions.
12 changes: 4 additions & 8 deletions code-questions/recursive/__tests__/merge-sort.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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) => {
Expand Down
29 changes: 27 additions & 2 deletions code-questions/recursive/merge-sort.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
Expand Down
94 changes: 46 additions & 48 deletions code-questions/recursive/merge-two-sorted-lists.js
Original file line number Diff line number Diff line change
@@ -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) */
Expand All @@ -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);
}
}
};
20 changes: 0 additions & 20 deletions code-questions/recursive/symbole-print.js

This file was deleted.

0 comments on commit 39bd259

Please sign in to comment.