Skip to content

Instantly share code, notes, and snippets.

View rootid's full-sized avatar
💭
Keep Learning+Coding+Building

Vikram rootid

💭
Keep Learning+Coding+Building
View GitHub Profile
if (my_value && typeof my_value === 'object' && my_value.constructor === Array) {
// my_value is an array!
}
//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;
}
//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();
//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]
//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;
}
class Queue<T> {
//Queue using DLL
class Node {
T data;
Node prev;
Node next;
Node (T data) {
this.data = data;
this.next = null;
//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
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--;
}
}
//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);
//: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) {