This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROBLEM STATEMENT: You're given a string consisting solely of (, ), and *. * can represent either a (, ), or an empty string. Determine whether the parentheses are balanced. | |
SOLUTION IN JAVASCRIPT(JS): | |
var str = "((*(*)))"; // User Input. | |
var arr = str.split(''); // Make array from user string. | |
var stack = {}; // Make an empty stack. | |
var balance = true; // Let given string has balanced parentheses. | |
// Loop through the arr array. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem: Make your own reducer function in javascript. | |
//Solution: | |
// First Add MyReducer function as prototype in array. | |
Array.prototype.MyReducer = function (fn, init) { | |
let current; | |
let startIndex = 0; | |
// If initial value is not defined assign it to with this first index value. | |
if (init) { | |
current = init; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem: You have to sort the string array in chronological order without using any external sorting method. | |
//Solution: | |
A = ["ab","aa","ca",'ab']; // user input | |
for(let count=0; count< A.length; count++) { | |
for(let innerCount=0; innerCount<A.length - count -1; innerCount++) { | |
if(A[innerCount] > A[innerCount+1]) { | |
// swap the elements. | |
let temp = A[innerCount+1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Problem Statement: You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. | |
Solution: | |
// XOR operator will return 0 if it is applied even times on same number And return number itself when it is applied odd times on the same number. | |
function findDuplicate(Arr) { | |
let len = Arr.length; | |
let acutalXORResult = 0; // contains the XOR operation result for the actual values from 1 to n. | |
let arrayXORResult = 0; // contains the XOR operation result on the array values. | |
for(let count=0; count<len; count++) { | |
acutalXORResult = acutalXORResult^count; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data) { | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
class BinarySearchTree { | |
constructor() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data) { | |
this.data = data; | |
this.next = null; | |
} | |
} | |
class LinkList { | |
constructor() { | |
this.head = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Links: | |
https://indian-cities-api-nocbegfhqg.now.sh/?ref=public-apis | |
https://indian-cities-api-nocbegfhqg.now.sh/cities | |
cityStateData = [ | |
{ | |
"City": "SGM", | |
"State": "Rajasthan", | |
"District": "Ganganagar" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data) { | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
class BinaryTree { | |
constructor(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Reference: https://www.youtube.com/watch?v=4jY57Ehc14Y | |
// JS implementation | |
function KMP(arr, pat) { | |
let arrLen = arr.length; | |
let patLen = pat.length; | |
let i=0, j=0; | |
let lps = computeLPS(pat); | |
while(i < (arrLen - patLen +1 )) { | |
if(arr[i] == pat[j]) { |