Skip to content

Instantly share code, notes, and snippets.

@xun-gong
xun-gong / quantiles
Created January 31, 2016 23:21
quantiles
import java.util.*;
class Pair {
public int val;
public int cnt;
public Pair(int v, int c) {
this.val = v;
this.cnt = c;
}
}
@xun-gong
xun-gong / Compile Error
Created September 17, 2014 05:20
Compile Error
xun@xun-DVM:~/CS6253-Distributed-OS/TutorialTest/gen-cpp$ g++ -DHAVE_INTTYPES_H -DHAVE_NETINET_IN_H -Wall -I/usr/local/include/thrift *.cpp -L/usr/local/lib -lthrift -o Calculator
Calculator_server.skeleton.cpp: In member function ‘virtual int32_t CalculatorHandler::add(int32_t, int32_t)’:
Calculator_server.skeleton.cpp:33:3: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
Calculator_server.skeleton.cpp: In member function ‘virtual int32_t CalculatorHandler::calculate(int32_t, const tutorial::Work&)’:
Calculator_server.skeleton.cpp:38:3: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
Calculator_server.skeleton.cpp: In function ‘int main(int, char**)’:
@xun-gong
xun-gong / CareerCup4.9.cpp
Created July 19, 2014 15:23
CareerCup4.9.cpp
/*
Chapter 4
4.9 You are given a binary tree in which each node contains an integer
value (which might be positive or negative). Design an algorithm to
print all paths which sum to a given value. The path does not need
to start or end at the root or a leaf, but it must go in a straight
line down.
*/
@xun-gong
xun-gong / CareerCup4.8.cpp
Created July 19, 2014 15:18
CareerCup4.8.cpp
/*
Chapter 4
4.8 You have two very large binary tree: T1, with millions of nodes, and T2, with hundreds of nodes.
Create an algorithm to decide if T2 is a subtree of T1.
A tree T2 is a subtree of T1 if there exist a node n in T1 such that the subtree of n is identical
to T2. That is, if you cut off the tree at node n, the tow trees would be identical.
*/
/*
@xun-gong
xun-gong / CareerCup4.7.cpp
Created July 19, 2014 15:00
CareerCup4.7.cpp
/*
Chapter 4
4.7 Design an algorithm and write code to find the first common ancestor
of two nodes in a binary tree. Avoid storing additional nodes in a
data structure. NOTE: This is not necessarily a binary search tree.
*/
// Assumption: when p is one ancestor of q's, we say p is the first common
// ancestor of p, q
@xun-gong
xun-gong / CareerCup4.6.cpp
Created July 19, 2014 14:49
CareerCup4.6.cpp
/*
Chapter 4
4.6 Write an algorithm to find the 'next' node (i.e., in-order successor)
of a given nodes in a binaru search tree. You may assume that each node
has a link to its parent.
*/
#include <iostream>
@xun-gong
xun-gong / CareerCup4.5.cpp
Created July 19, 2014 14:42
CareerCup4.5.cpp
/*
Chapter 4
4.5 Implement a function to check if a binary tree is a binary search tree.
*/
#include <iostream>
#include <stack>
@xun-gong
xun-gong / CareerCup4.4.cpp
Created July 19, 2014 14:22
CareerCup4.4.cpp
/*
Chapter 4
4.4 Given a binary tree, design an algorithm which creates a linked
list of all the nodes at each depth. (e.g., if you have a tree with
depth D, you'll have D linked lists.)
*/
#include <iostream>
@xun-gong
xun-gong / CareerCup4.3.cpp
Created July 19, 2014 14:15
CareerCup4.3.cpp
/*
* Chapter 4
* 4.3 Given a sorted (increasing order) array with unique integer elements,
* write an algorithm to create a binary search tree with minimal height.
*/
/* Recursive way to solve this problem */
#include <iostream>
using namespace std;
@xun-gong
xun-gong / CareerCup4.2.cpp
Created July 19, 2014 14:00
CareerCup4.2.cpp
/*
* Chapter 4
* 4.2 Given a directeed graph, design an algorithm to find out
* whether there is a route between two nodes.
*/
#include <iostream>
#include <queue>
#include <list>