Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 80a12fa

Browse files
Add Recursion Basics and Interview Questions
1 parent 61e31ff commit 80a12fa

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

‎Recursion/0-recursion-basics.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function Piyush() {
2+
return Rehana();
3+
}
4+
5+
function Rehana() {
6+
return Vadant();
7+
}
8+
9+
function Vadant() {
10+
return Chirag();
11+
}
12+
13+
function Chirag() {
14+
return Ajay();
15+
}
16+
17+
function Ajay() {
18+
// base case
19+
return true;
20+
}
21+
22+
// console.log(Piyush());
23+
24+
function goToLunch(person) {
25+
if (person === 5) return true;
26+
console.log(person);
27+
return goToLunch(person + 1);
28+
}
29+
30+
// console.log("outcome:", goToLunch(1));
31+
32+
// Loops vs Recursion
33+
34+
// function multiply(arr) {
35+
// let product = 1;
36+
// for (let i = 0; i < arr.length; i++) {
37+
// product *= arr[i];
38+
// }
39+
// return product;
40+
// }
41+
42+
function multiply(arr) {
43+
console.log(arr);
44+
if (arr.length <= 0) {
45+
return 1;
46+
} else return arr[arr.length - 1] * multiply(arr.slice(0, arr.length - 1));
47+
}
48+
49+
console.log(multiply([1, 2, 3, 4]));

‎Recursion/1-factorial.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Ques 1 : Factorial of n
2+
// Input: n = 5 ----->>>>> Output: 120
3+
4+
function factorial(n) {
5+
if (n === 0) {
6+
return 1;
7+
} else return n * factorial(n - 1);
8+
}
9+
10+
console.log(factorial(5));

‎Recursion/2-range-of-numbers.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Ques 2 : Create an array with range of numbers
2+
// Input: start=1, end=5 ----->>>>> Output:
3+
4+
function rangeOfNumbers(startNum, endNum) {
5+
if (endNum < startNum) {
6+
return [];
7+
} else {
8+
const numbers = rangeOfNumbers(startNum, endNum - 1);
9+
numbers.push(endNum);
10+
return numbers;
11+
}
12+
}
13+
14+
// rangeOfNumbers(1,5) => [1,2,3,4,5]
15+
// rangeOfNumbers(1,4) => [1,2,3,4]
16+
// rangeOfNumbers(1,3) => [1,2,3]
17+
// rangeOfNumbers(1,2) => [1,2]
18+
// rangeOfNumbers(1,1) => [1]
19+
// rangeOfNumbers(1,0) => []
20+
21+
// console.log(rangeOfNumbers(0, 5));

‎Recursion/3-palindrome.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Ques 3 : Given an integer x, return true if x is a palindrome, and false otherwise.
2+
// Input: x = 121 ----->>>>> Output: true;
3+
4+
function isPalindrome(str) {
5+
str = str.replace(/[^a-z0-9]/i, "").toLowerCase();
6+
const len = str.length;
7+
8+
if (len <= 1) return true;
9+
if (str[0] !== str[len - 1]) return false;
10+
return isPalindrome(str.slice(1, -1));
11+
}
12+
13+
console.log(isPalindrome("121"));

‎Recursion/4-fibonacci.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Ques 4 - Fibonacci Number
2+
// Fibonacci Series -> 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233...
3+
// F(0) = 0, F(1) = 1
4+
// F(n) = F(n - 1) + F(n - 2), for n > 1
5+
6+
// Input: n = 3 ----->>>>> Output: 2
7+
8+
var fib = function (n) {
9+
let arr = [0, 1];
10+
for (let i = 2; i <= n; i++) {
11+
arr.push(arr[i - 2] + arr[i - 1]);
12+
}
13+
return arr[n];
14+
};
15+
16+
function fibRecursion(n) {
17+
if (n <= 1) return n;
18+
return fibRecursion(n - 1) + fibRecursion(n - 2);
19+
}
20+
21+
// console.log(fibRecursion(15));

‎Recursion/5-reverse-string.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Ques 5 - Reverse a String
2+
// Input: "hello" ----->>>>> Output: "olleh"
3+
4+
function reverseString(str) {
5+
if (str === "") {
6+
return "";
7+
} else return reverseString(str.substr(1)) + str.charAt(0);
8+
}
9+
10+
// console.log(reverseString("hello")); => olleh

‎Recursion/6-subsets.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Ques 6 - Subsets ( Backtracking Algorithm using Recursion )
2+
// Given an integer array nums of unique elements, return all possible subsets (the power set).
3+
// The solution set must not contain duplicate subsets. Return the solution in any order.
4+
5+
// Input: [1,2,3] ----->>>>> Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
6+
// Input: [0] ----->>>>> Output: [[],[0]]
7+
8+
function subsets(nums) {
9+
let result = [];
10+
let temp = [];
11+
12+
function recursiveSubsets(nums, i) {
13+
if (i === nums.length) {
14+
return result.push([...temp]);
15+
}
16+
17+
temp.push(nums[i]);
18+
recursiveSubsets(nums, i + 1);
19+
temp.pop();
20+
recursiveSubsets(nums, i + 1);
21+
}
22+
23+
recursiveSubsets(nums, 0);
24+
return result;
25+
}
26+
27+
console.log(subsets([1]));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /