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
//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;
}
if (my_value && typeof my_value === 'object' && my_value.constructor === Array) {
// my_value is an array!
}
//Augmenting the JS objects (NOTE : in JS function are objects)
//Ugly
function makePerson(first, last) {
return {
first: first,
last: last
};
}
function personFullName(person) {
//T(n) = O(n^2)
int maxDepth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftDepth = 0;
int rightDepth = 0;
leftDepth = maxDepth(root->left);
rightDepth = maxDepth(root->right);
return 1 + max(leftDepth, rightDepth);
//T(n) = O(n)
int dfsHeight (TreeNode *root) {
if (root == NULL) return 0;
int leftHeight = dfsHeight (root -> left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight (root -> right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) {
List<Integer> list = new LinkedList<Integer>();
//The Good
public List<Integer> preorderTraversalRec(TreeNode root) {
List<Integer> ans = new ArrayList<Integer>();
if (root == null) {
return ans;
}
ans.add(root.val);
ans.addAll(preorderTraversalRec(root.left));
@rootid
rootid / LRUCache.java
Created June 6, 2015 20:03
LRU cache design with DLL and HashMap
public class LRUCache {
//Keep track of least recently used elements
class Node {
String key_;
String val_;
Node next;
Node prev;
Node (String key,String val) {
this.key_ = key;
class HashTable {
class Entry {
//private final String key; Deleting the Entry ?
private String key;
private String value;
private Entry next;
Entry (String key,String value) {
this.key = key;
this.value = value;
@rootid
rootid / Queue.java
Created June 6, 2015 20:28
Queue using DLL
class Queue<T> {
/* In Queue updates made at the both ends
*/
class Node {
T data;
Node prev;
Node next;
Node (T data) {
this.data = data;
@rootid
rootid / functor.cpp
Created June 6, 2015 21:48
Functor template
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class X {
public :
void op() {
cout << "X::op() " << endl;
}