Thursday, January 17, 2013

The meaning of life

Someone in my team pointed out that now I have an answer to the meaning of the life http://www.bing.com/search?q=42


Wednesday, January 16, 2013

Facebook Graph Search and Bing Web Search suggestions

You have probably seen the new Facebook Graph Search for searching Facebook internal data. Anytime you search something from the public Web, then you will use the suggestions coming from Bing Web Search. This is a feature run by my team. Congratulation to all of them for driving the amazing team who shipped the core web suggestions feature from London







Monday, January 14, 2013

Calculate the span on a stock

You are given the value of a stock during different days. For each day d, we want to know how many preceding and consecutive day the stock had a value less or equal to the one observed on d.

Thursday, November 29, 2012

Wednesday, November 28, 2012

Serialize and deserialize a generic tree

File * serialize (node * n); node * deserialize (File * f);

Monday, November 26, 2012

Isomorphic trees

Two trees can be called isomorphic if they have similar structure and the only difference amongst them can be is, that their child nodes may or may not be swaped. Write a function to detect whether two trees are isomorphic
int isIsomorphic(Node root1,Node root2) {
   if(root1==null && root2==null)
       return 1;
         
   if((root1 ==  null && root2 != null) || (root1 !=null && root2 == null ))
     return 0;
             
    if(root1.val == root2.val){
             
       if(isIsomorphic(root1.left, root2.left) == 1){
            return (isIsomorphic(root1.right, root2.right));
       } else if(isIsomorphic(root1.left, root2.right) == 1){
            return (isIsomorphic(root1.right, root2.left));
       }
             
    }
         
return 0;
}

Sunday, November 25, 2012

Pascal’s triangle is a triangular array of the binomial coefficients

Method 1 -
Each line n is the binomial coefficient C(n, k) where k is the row. This has complexity n^3. because in this case k has the same order of n

Method 2
Complexity n^2, with space n^2

Method 3
complexity n^2, with space O(1) -- this is more difficult because you need to move in the triangle storing just a constant number of locations. Can you express C(n, k) as function of C(n, k-1)?

Saturday, November 24, 2012

Compute the binomial coefficient in efficient way


C(n, k) = n ! / (n-k) ! k! = n (n-1) ... (n-k+1) / 1. 2. ... (k-1)k

C(n,k) = C(n, n-k) = n! / (n-n +k)! (n-k)!  so we can change r to r-p is r > p-r and keep the operation smaller


for (i = 0; i < k; i++)
{
    res *= (n - i);
    res /= (i+1);
}

O(k) time and (1) space

Thursday, November 22, 2012

Wednesday, November 21, 2012

BST, kth

 Find the Kth largest integer in a Binary Search Tree. 

Tuesday, November 20, 2012

Monday, November 19, 2012

Sunday, November 18, 2012

Gas stations

You have a certain number of gas stations in a circle and a car running on the circle. The car has a certain amount of gas available at the beginning. Find the right station to start for not running out of gas

This is a version of max subarray

Saturday, November 17, 2012

Creating a generic graph

Here the code based on C++, template, and boost::unordered_map and boost::unordered_multimap

Friday, November 16, 2012

Thursday, November 15, 2012

Construct BST from given preorder traversal

Construct BST from given preorder traversal (recursive solution)