Skip to content

Commit 70f9642

Browse files
committed
more question practiced
1 parent 842abdb commit 70f9642

File tree

8 files changed

+309
-67
lines changed

8 files changed

+309
-67
lines changed

Easy-Questions/climbing_stairs.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// using dynamic programming (tabulation or iterative approach) bottom-up
2+
3+
// #### base case
4+
// 1. when we only have 0-th stair or 1-th stair in this case
5+
// we only have 1 choice to climb stairs dp[0] - 1 or dp[1] = 0
6+
// so the answer we got is only 1 ways can we climb to the top...
7+
8+
// 2. When we have case for (n >=2)
9+
// we iterate through the for i = 2 till n (i.e. i = 2 to i <= n)
10+
// * The number of ways to climb to step i is the sum of:
11+
// * dp[i - 1] (ways to climb to the previous step and take 1 step).
12+
// * dp[i - 2] (ways to climb to the step before last and take 2 steps).
13+
14+
// using this formula
15+
// **dp[i]=dp[i−1]+dp[i−2]** we get the total number of ways to climb stairs
16+
17+
// while returing dp[n] where n --> n-th number of stair
18+
19+
// Brute force solution
20+
21+
// TC - O(2^n)
22+
// Recursion based
23+
var climbStairs = function (n) {
24+
if (n === 0 || n === 1) return 1;
25+
return climbStairs(n - 1) + climbStairs(n - 2);
26+
};
27+
28+
29+
/**
30+
* @param {number} n
31+
* @return {number}
32+
*/
33+
var climbStairs = function (n) {
34+
if (n === 0 || n === 1) return 1;
35+
let dp = [1, 1];
36+
for (let i = 2; i <= n; i++) {
37+
dp[i] = dp[i - 1] + dp[i - 2];
38+
}
39+
return dp[n];
40+
};
41+
42+
43+
44+
// Dry run with n = 5
45+
46+
// Initialization:
47+
48+
// n = 5.
49+
// dp = [1, 1] (base cases).
50+
// Iterative Loop:
51+
52+
// Step 2 (i = 2):
53+
54+
// dp[2] = dp[1] + dp[0] = 1 + 1 = 2.
55+
// dp = [1, 1, 2].
56+
// Step 3 (i = 3):
57+
58+
// dp[3] = dp[2] + dp[1] = 2 + 1 = 3.
59+
// dp = [1, 1, 2, 3].
60+
// Step 4 (i = 4):
61+
62+
// dp[4] = dp[3] + dp[2] = 3 + 2 = 5.
63+
// dp = [1, 1, 2, 3, 5].
64+
// Step 5 (i = 5):
65+
66+
// dp[5] = dp[4] + dp[3] = 5 + 3 = 8.
67+
// dp = [1, 1, 2, 3, 5, 8].
68+
// Result:
69+
70+
// Return dp[5] = 8.
71+

Easy-Questions/happyNumber.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*
5+
* why n < 10 and n = 1 or n = 7
6+
* for n = 1 or n = 7 ---> 1 square is 1 and 7 → 49 → 97 → 130 → 10 → 1
7+
* Input: n = 4
8+
First iteration:
9+
4 → 4² = 16.
10+
Second iteration:
11+
16 → 1² + 6² = 37.
12+
Third iteration:
13+
37 → 3² + 7² = 58.
14+
Infinite loop:
15+
The sequence 58 → 89 → 145 → 42 → 20 → 4 repeats indefinitely.
16+
* All other single-digit numbers (2, 3, 4, ..., 9) lead to infinite cycles, not 1.
17+
recursivly calling the isHappy(n=1) only calls when n = 1 or n= 7 else it goes to false
18+
*/
19+
var isHappy = function (n) {
20+
// base case for n < 10
21+
if (n < 10) {
22+
if (n === 1 || n === 7) { // always true for n value --> 1 and 7
23+
return true;
24+
}
25+
return false;
26+
}
27+
let res = 0;
28+
while (n > 0) {
29+
let digit = n % 10;
30+
res = res + Math.pow(digit, 2);
31+
n = Math.floor(n / 10)
32+
}
33+
return isHappy(res)
34+
};
35+
36+

Easy-Questions/isSubsequence.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*
6+
* using two pointer approach
7+
* Traverse through t while keeping track of a pointer j for s.
8+
For every character in t, if the character matches s[j], move the pointer j forward.
9+
If j equals s.length, it means all characters in s are matched in t in order.
10+
*/
11+
var isSubsequence = function (s, t) {
12+
let len = s.length;
13+
if(len === 0) return true;
14+
let j = 0;
15+
for(let i = 0; i<t.length; i++) {
16+
if(s[j] === t[i]) {
17+
j++;
18+
}
19+
if(j === len) return true;
20+
}
21+
return false;
22+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function maxFrequencyElements(nums) {
2+
let hashMap = new Map();
3+
let maxFreq = 0;
4+
5+
for(let value of nums) {
6+
hashMap.set(value, (hashMap.get(value) || 0 ) + 1);
7+
maxFreq = Math.max(maxFreq, hashMap.get(value));
8+
}
9+
let totalFreq = 0;
10+
for(let value of hashMap.values()) {
11+
if(value === maxFreq) {
12+
totalFreq += maxFreq;
13+
}
14+
}
15+
return totalFreq;
16+
};
17+
18+
let arr = [1, 2, 2, 3, 1, 4];
19+
const res = maxFrequencyElements(arr);
20+
console.log(res);
21+
22+
23+
/**
24+
* Test Case 1: Normal Input
25+
Input: nums = [1, 2, 2, 3, 1, 4]
26+
27+
Step-by-Step Execution:
28+
29+
Frequency Calculation:
30+
31+
Initialize hashMap = new Map().
32+
Loop through nums:
33+
1 → hashMap = {1: 1}
34+
2 → hashMap = {1: 1, 2: 1}
35+
2 → hashMap = {1: 1, 2: 2}
36+
3 → hashMap = {1: 1, 2: 2, 3: 1}
37+
1 → hashMap = {1: 2, 2: 2, 3: 1}
38+
4 → hashMap = {1: 2, 2: 2, 3: 1, 4: 1}
39+
Find Maximum Frequency:
40+
41+
Loop through hashMap.values(hashMap) = [2, 2, 1, 1].
42+
Compare each value with maxFreq:
43+
maxFreq = max(0, 2) = 2
44+
maxFreq = max(2, 2) = 2
45+
maxFreq = max(2, 1) = 2
46+
maxFreq = max(2, 1) = 2
47+
Sum Frequencies with maxFreq:
48+
49+
Initialize totalFreq = 0.
50+
Loop through hashMap.values(hashMap) = [2, 2, 1, 1]:
51+
2 === maxFreq → totalFreq += 2 → totalFreq = 2
52+
2 === maxFreq → totalFreq += 2 → totalFreq = 4
53+
1 !== maxFreq → skip
54+
1 !== maxFreq → skip
55+
Output: 4
56+
57+
Test Case 2: Single Element Array
58+
Input: nums = [7]
59+
60+
Step-by-Step Execution:
61+
62+
Frequency Calculation:
63+
64+
Initialize hashMap = new Map().
65+
Loop through nums:
66+
7 → hashMap = {7: 1}
67+
Find Maximum Frequency:
68+
69+
Loop through hashMap.values(hashMap) = [1].
70+
Compare each value with maxFreq:
71+
maxFreq = max(0, 1) = 1
72+
Sum Frequencies with maxFreq:
73+
74+
Initialize totalFreq = 0.
75+
Loop through hashMap.values(hashMap) = [1]:
76+
1 === maxFreq → totalFreq += 1 → totalFreq = 1
77+
Output: 1
78+
*/

Easy-Questions/missingNumber.js

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,47 @@
11
var arr = [1, 2, 4, 5];
22

3-
// Remeber point we are taking i = actual length of array
3+
// Remeber point we are taking i = actual length of array
44
// j ---> arr.length - 1 hypothetical range
55

6-
76
/**
87
* @param arr[]
98
* @returns missing number;
10-
*
11-
* Brute solution
9+
*
10+
* Brute solution
1211
* Time complexity O(n^2) --> Worst case
1312
* Space complexity O(1)
14-
* Keeping the track of ith number exist in array with nested loop iteration i.e. arr[j] === i
13+
* Keeping the track of ith number exist in array with nested loop iteration i.e. arr[j] === i
1514
*/
1615

17-
1816
function missingNumberBrute(arr) {
19-
for(let i = 0; i < arr.length; i++) {
20-
let flag = 0;
21-
for(let j = 0; j < arr.length - 1; j++) {
22-
if(arr[j] === i) {
23-
flag = 1;
24-
break
25-
}
26-
}
27-
if(flag === 0) {
28-
return arr[i];
29-
}
17+
for (let i = 0; i < arr.length; i++) {
18+
let flag = 0;
19+
for (let j = 0; j < arr.length - 1; j++) {
20+
if (arr[j] === i) {
21+
flag = 1;
22+
break;
23+
}
3024
}
25+
if (flag === 0) {
26+
return arr[i];
27+
}
28+
}
3129
}
3230
let res1 = missingNumberBrute(arr);
3331
console.log(res1);
3432

3533
/**
3634
* 2nd Approach
3735
* @param arr[]
38-
* finding the missing number
36+
* finding the missing number
3937
* using Sum of all natural number
4038
* Time Complexity O(n)
4139
* Space Complexity O(1)
4240
*/
4341
// Explanation
4442
// Uses the formula for the sum of the first n natural numbers:
4543

46-
// Sum =𝑛 × ( 𝑛 +1) / 2
44+
// Sum =𝑛 × ( 𝑛 +1) / 2
4745

4846
// This works because n is the number of elements the array should have.
4947

@@ -53,16 +51,15 @@ console.log(res1);
5351
// Difference:
5452
// The missing number is the difference between the expected sum and the actual sum.
5553

56-
5754
function missingNumber(arr) {
58-
let sum = 0;
59-
let n = arr.length + 1;
60-
let sumOfNum = (n * (n + 1 )) / 2;
61-
for(let i = 0; i < arr.length; i++) {
62-
sum += arr[i];
63-
}
64-
let res = sumOfNum - sum;
65-
return res;
55+
let sum = 0;
56+
let n = arr.length + 1;
57+
let sumOfNum = (n * (n + 1)) / 2;
58+
for (let i = 0; i < arr.length; i++) {
59+
sum += arr[i];
60+
}
61+
let res = sumOfNum - sum;
62+
return res;
6663
}
6764

6865
let res = missingNumber(arr);
@@ -71,16 +68,13 @@ console.log(res);
7168
let xor1 = 0;
7269
let xor2 = 0;
7370

74-
for(let i = 0; i< arr.length; i++) {
75-
xor1 = xor1 ^ i; // i.e 1^2^3^4^5
71+
for (let i = 0; i < arr.length; i++) {
72+
xor1 = xor1 ^ i; // i.e 1^2^3^4^5
7673
}
7774

78-
for(let i = 0; i < arr.length - 1; i++) {
79-
xor2 = xor2 ^ i;
75+
for (let i = 0; i < arr.length - 1; i++) {
76+
xor2 = xor2 ^ i;
8077
}
8178

8279
let missingNum = xor1 ^ xor2;
8380
console.log(missingNum);
84-
85-
86-
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var firstUniqChar = function(s) {
6+
let arr = Array(26).fill(0); // Initialize an array of size 26 with 0
7+
for (let ch of s) {
8+
arr[ch.charCodeAt(0) - 'a'.charCodeAt(0)]++; // Increment the count for the character
9+
}
10+
for (let i = 0; i < s.length; i++) {
11+
if (arr[s[i].charCodeAt(0) - 'a'.charCodeAt(0)] === 1) {
12+
return i; // Return the index of the first unique character
13+
}
14+
}
15+
return -1; // Return -1 if no unique character exists
16+
};

0 commit comments

Comments
 (0)