Skip to content

Instantly share code, notes, and snippets.

View PulkitAgg's full-sized avatar

Pulkit Aggarwal PulkitAgg

  • Nagarro Software Pvt. Ltd.
  • Gurgaon
View GitHub Profile
@PulkitAgg
PulkitAgg / BalancedParenthesesProblem.txt
Last active December 5, 2018 10:57
BALANCED PARENTHESES PROBLEM ASKED BY GOOGLE
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.
@PulkitAgg
PulkitAgg / MyReducer.js
Created December 17, 2018 06:40
Make Own Reducer function in js
// 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;
@PulkitAgg
PulkitAgg / chronologicalSorting.js
Created December 17, 2018 07:15
Chronological Order Sorting Using Bubble Sort.
//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];
@PulkitAgg
PulkitAgg / FindDuplicate.js
Created December 27, 2018 06:27
Find Duplicate number problem asked by GOOGLE
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;
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
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"
},
@PulkitAgg
PulkitAgg / Binary Tree Implementation
Last active December 26, 2020 14:18
Binary Tree Implementation From Array
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor(){
@PulkitAgg
PulkitAgg / KMP Algo
Created July 17, 2021 06:30
KMP (Knuth-Morris-Pratt) String matching program
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]) {