This file contains hidden or 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
if (my_value && typeof my_value === 'object' && my_value.constructor === Array) { | |
// my_value is an array! | |
} |
This file contains hidden or 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
//add the given argument and throw exception object {name,message} | |
var add = function (a, b) { | |
if (typeof a !== 'number' || typeof b !== 'number') { | |
throw { | |
name: 'TypeError', | |
message: 'add needs numbers' | |
}; | |
} | |
return a + b; | |
} |
This file contains hidden or 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
//sync v/s async | |
//Count # of lines | |
var fs = require('fs') | |
var fileName = process.argv[2] | |
//Async call | |
fs.readFile(fileName,function (err, data) { | |
lst_ = data.toString(); |
This file contains hidden or 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
//I/P [1, 2, 4, ,6, 3, 7, 8] | |
//O/P 5 | |
//NOTE : array is sorted | |
//1 .total = n*(n+1)/2 leads to overflow if total can't be represented by the number O(n) | |
//2. XOR (given array) ^ (numbers from 1-n) = missing number O(n) | |
//3. Binary search (Find out the hole wheather in first half/second half) (O log n) | |
//Array: [1,2,3,4,5,6,8,9] |
This file contains hidden or 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
//If the given arrays is {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0} | |
//,it should be changed to {1, 9, 8, 4, 2, 7, 6, 0, 0, 0, 0}. | |
//The order of all other elements should be same. | |
//Expected time complexity is O(n) and extra space is O(1). | |
public static void swap (int a[],int start,int end) { | |
int tmp = a[start]; | |
a[start] = a[end]; | |
a[end] = tmp; | |
} |
This file contains hidden or 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 Queue<T> { | |
//Queue using DLL | |
class Node { | |
T data; | |
Node prev; | |
Node next; | |
Node (T data) { | |
this.data = data; | |
this.next = null; |
This file contains hidden or 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
//Find Idx difference | |
//Input: {34, 8, 10, 3, 2, 80, 30, 33, 1} | |
//Output: 78 | |
//Input: {9, 2, 3, 4, 5, 6, 7, 8, 18, 0} | |
//Output:16 | |
//Input: {1, 2, 3, 4, 5, 6} | |
//Output: 5 |
This file contains hidden or 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
public static void reverse (int nums[],int start,int end) { | |
while(start < end) { | |
int tmp = nums[start]; | |
nums[start] = nums[end]; | |
nums[end] = tmp; | |
start++; | |
end--; | |
} | |
} |
This file contains hidden or 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
//find RAGA journey | |
//US->Thila | |
//Thid->Bang | |
//India->Us | |
void topologicalSortUtil (string src,unordered_map<string,vector<string> >& travelG | |
,unordered_set<string>& visited,stack<string>& iStack) { | |
visited.insert(src); |
This file contains hidden or 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
//:pancake sorting | |
//:insert the spatuala to max item idx | |
//:flip the max item idx to first | |
//:flip all items to size - 1 | |
//:reduce size | |
void flip (int a[],int n) { | |
int start = 0; | |
int end = n; | |
while (start < end) { |