Skip to content

Instantly share code, notes, and snippets.

View abhinavnigam2207's full-sized avatar
😎

Abhinav Nigam abhinavnigam2207

😎
View GitHub Profile
@abhinavnigam2207
abhinavnigam2207 / bracket-problem.js
Created February 22, 2017 13:08
Bracket Problem (Asked in Toptal Interview )
'use strict';
function solution(S) {
var arr = [];
var K = 0;
for (var i=0; i<S.length; i++) {
if (S[i]=='(') {
arr.push(S[i]);
} else {
if(arr.length){
@abhinavnigam2207
abhinavnigam2207 / check-duplicates-in-array-by-property.js
Last active September 6, 2017 06:05
Check duplicates in an array by property and mark duplicates with a boolean flag.
const myArray = [
{id: 1, name: 'Abhinav', email: 'abhinav@gmail.com'},
{id: 2, name: 'Aviral', email: 'aviral@gmail.com'},
{id: 3, name: 'Jitendra', email: 'jitendra@gmail.com'},
{id: 4, name: 'Rajesh', email: 'rajesh@gmail.com'},
{id: 5, name: 'Abhinav', email: 'abhinav.nigam@gmail.com'},
{id: 6, name: 'Akash', email: 'akash@gmail.com'},
];
let arrayCopy = JSON.parse(JSON.stringify(myArray));
@abhinavnigam2207
abhinavnigam2207 / castle-global.js
Last active May 9, 2018 12:02
String matching time complexity problem (Asked in Castle Global)
const problemSolution = function(str){
let val = str.split(''),
stack = [],
count = 0;
stack.push(val[0]);
for(let i=1; i< val.length; i++) {
if((/[a-z]/.test(stack[stack.length-1])) && (/[a-z]/.test(val[i]))) {
console.log('1'+ val[i]);
return count;
}
@abhinavnigam2207
abhinavnigam2207 / recursive-function-return.js
Created May 8, 2018 14:36
Recursive function to return the value returned by innermost function. (Asked in Wingify Interview)
function value(funcName) {
if(typeof funcName !== 'function') {
return funcName;
let func = funcName();
let resp = typeof func === 'function' ? value(func) : func;
return resp;
}
@abhinavnigam2207
abhinavnigam2207 / flatten.js
Created May 18, 2018 23:02
Flatten an object recursively (Asked in Wingify Interview)
const flatten = function(inputObj) {
var respObj = {};
for (let i in inputObj) {
if (!inputObj.hasOwnProperty(i)) continue;
if ((typeof inputObj[i]) == 'object') {
let flatObject = flatten(inputObj[i]);
for (let x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;
@abhinavnigam2207
abhinavnigam2207 / hosting-on-github.md
Created July 12, 2018 18:28
Basic steps for hosting on Github

Steps for Hosting a Website on GitHub

  1. Create a GitHub account on github.com.
  2. Download either [GitHub for Mac][1] or [GitHub for Windows][2], depending on your operating system. Open the app and log in using the account you just created.
  3. (On Mac): After you login, click advanced and make sure that your name and email are correct. Then, click "Install Command Line Tools", just in case you want to start using the command line later in life.
  4. Create a new repository in your GitHub application. Name it your-username.github.io. The name is very important. Note the folder that GitHub is saving the repository to. Make sure the "Push to GitHub?" box is checked.
  5. Move your website's files into the folder that GitHub just created when you made the repository. IMPORTANT: Your homepage HTML file must be called "index.html", and it must exist in the top-level directory.
  6. Back in the GitHub application, you should see your files in the left column. Make sure they are all checked. If so, enter a mess
function towerOfHanoi(source, helper, target) {
function moveDisks(n, innerSource, innerHelper, innerTarget) {
if (n <= 0) {
return;
}
moveDisks(n - 1, innerSource, innerTarget, innerHelper);
innerTarget.push(innerSource.pop());
moveDisks(n - 1, innerHelper, innerSource, innerTarget);
}
moveDisks(source.length, source, helper, target);
@abhinavnigam2207
abhinavnigam2207 / findSecondLargestInArray.js
Created September 24, 2018 10:52
Find second largest In an array
function findSecondLargest(arr) {
let max = arr[0];
let secondMax = arr[0];
arr.forEach((elem) => {
if(elem > max) {
secondMax = max;
max = elem;
} else if(elem<max && elem>secondMax) {
secondMax = elem;
}
@abhinavnigam2207
abhinavnigam2207 / deep-copy-object.js
Created February 19, 2019 01:31
Create a deep copy of an object.
// Method 1
let obj = {
a: 1,
b: {
c: 2,
},
}
let newObj = JSON.parse(JSON.stringify(obj));
obj.b.c = 20;
@abhinavnigam2207
abhinavnigam2207 / Browser Life Cycle.md
Created February 19, 2019 06:52
How browser renders a web page (Browsers Life cycle)
  1. You type an URL into address bar in your preferred browser.
  2. The browser parses the URL to find the protocol, host, port, and path and it forms a HTTP request (that was most likely the protocol).
  3. To reach the host, it first needs to translate the human readable host into an IP number, and it does this by doing a DNS lookup on the host
  4. Then a socket needs to be opened from the user’s computer to that IP number, on the port specified (most often port 80)
  5. When a connection is open, the HTTP request is sent to the host
  6. The host forwards the request to the server software (most often Apache) configured to listen on the specified port
  7. The server inspects the request (most often only the path), and launches the server plugin needed to handle the request (corresponding to the server language you use, PHP, Java, .NET, Python?)
  8. The plugin gets access to the full request, and starts to prepare a HTTP response.
  9. To construct the response a database is (most likely) accessed. A database search is