Monthly Archives: November 2014

Check if a tree is a sum tree

This is the solution to here: link

  1. public class BuildSumTree {
  2.     public static void main(String[] args) {
  3.         Tree tree = Tree.getSumTree3();
  4.         System.out.println(ifSumTree(tree, new MyInteger(0)));
  5.     }
  6.     /*
  7.      * http://www.geeksforgeeks.org/check-if-a-given-binary-tree-is-sumtree/
  8.      */
  9.     public static boolean ifSumTree(Tree tree, MyInteger sum){
  10.         if(tree==null||(tree.left==null&&tree.right==null)){    //return true if tree is null or leaf
  11.             sum.i = 0;
  12.             return true;
  13.         }
  14.         int lvalue = (tree.left==null)?0:tree.left.value;
  15.         int rvalue = (tree.right==null)?0:tree.right.value;
  16.         MyInteger lsum = new MyInteger(0);
  17.         MyInteger rsum = new MyInteger(0);
  18.         if(ifSumTree(tree.left, lsum)&&ifSumTree(tree.right, rsum)){
  19.             sum.i = lsum.i + rsum.i + lvalue+rvalue;
  20.             boolean result = sum.i==tree.value;
  21.             return result;
  22.         }
  23.         return false;
  24.     }
  25.     /*
  26.      * This is a sum tree. This tree will be checked if it is a sum tree
  27.      */
  28.     public static Tree getSumTree3(){
  29.         Tree t1 = new Tree(34);
  30.         Tree t2 = new Tree(12);
  31.         Tree t3 = new Tree(5);
  32.         Tree t4 = new Tree(3);
  33.         Tree t5 = new Tree(3);
  34.         Tree t6 = new Tree(2);
  35.         Tree t7 = new Tree(3);
  36.         Tree t8 = new Tree(1);
  37.         Tree t9 = new Tree(2);
  38.         Tree t10 = new Tree(1);
  39.         Tree t11 = new Tree(1);
  40.         Tree t12 = new Tree(1);
  41.         t1.left = t2;
  42.         t1.right = t3;
  43.         t2.left = t4;
  44.         t2.right = t5;
  45.         t4.left = t8;
  46.         t4.right = t9;
  47.         t5.left = t10;
  48.         t5.right = t11;
  49.         t11.right = t12;
  50.         t3.left = t6;
  51.         t3.right = t7;
  52.         return t1;
  53.     }
  54. }

Convert a given tree to sum tree

This is a solution to here: link

  1. public class BuildSumTree {
  2.     public static void main(String[] args) {
  3.         Tree tree = getSumTree();
  4.         tree = convertToSumTree(tree);
  5.     }
  6.     /*
  7.      * http://www.geeksforgeeks.org/convert-a-given-tree-to-sum-tree/
  8.      */
  9.     public static int convertToSumTree(Tree tree){
  10.         if(tree==null){
  11.             return 0;
  12.         }
  13.         int lvalue = (tree.left==null)?0:tree.left.value;
  14.         int rvalue = (tree.right==null)?0:tree.right.value;
  15.         tree.value = convertToSumTree(tree.left) + convertToSumTree(tree.right) + lvalue + rvalue;
  16.         return tree.value;
  17.     }
  18.     /*
  19.      * This tree will be converted to a sum tree.
  20.      */
  21.     public static Tree getSumTree(){
  22.         Tree t1 = new Tree(1);
  23.         Tree t2 = new Tree(2);
  24.         Tree t3 = new Tree(3);
  25.         Tree t4 = new Tree(4);
  26.         Tree t5 = new Tree(5);
  27.         Tree t6 = new Tree(6);
  28.         t1.left = t2;
  29.         t1.right = t3;
  30.         t2.left = t4;
  31.         t2.right = t5;
  32.         t3.left = t6;
  33.         return t1;
  34.     }
  35. }

Page visit problem

This is for the amazon interview Round 3, Question 2. http://www.geeksforgeeks.org/amazon-interview-experience-set-149-campus-internship/
Design an efficient data structure which supports queries like the following:
Which page was visited by exactly 2 users in day?
Which page was visited by only one user exactly 2 times in a day?
Which page was visited by ‘User 3? more than 5 times in a day?

The basic idea is to use cross linkedlist to store the data. There are 3 types of class: User, Page, InfoNode.
Initial User[] user, Page[] page. user[] and page[] are the entries to find infoNode. Each element in user[], page[] will point to a infonode by the nextNodeByUser pointer or nextNodeByPage pointer.

The cross linkedlist are contructed by the following rules:
1. Elements in page[] are ordered by visitedUserNum parameter. In this way, it can use binary search to find “Which page was visited by exactly 2 users in day?”
2. For those page element with same visitedUserNum, they are sorted by visitedTimes.  In this way, it can use binary search to find “Which page was visited by only one user exactly 2 times in a day?”
3. Elements in user[] are sorted by their userId. For InfoNodes which are under same user, they are linked by nextNodeByUser. They are sorted by descended visit times. In this way, it can easily find “Which page was visited by ‘User 3? more than 5 times in a day?”

Please correct me if I’m wrong!

  1. /*
  2.  * Q2. Given a log file of page visits of a website by different users for a day.
  3.  * Entry in the log file is like this:
  4.  * User 1 visited Page 4
  5.  * User 3 visited Page 2
  6.  * User 7 visited Page 9
  7.  */
  8. class User{
  9.     int userId;
  10.     InfoNode firstUserNode;
  11. }
  12. class Page{
  13.     int pageId;
  14.     int visitedUserNum;    //indicate this page is visited by how many users;    pages are sorted by visitedUser.
  15.     int visitedTimes;    //indicate this page is visited by how many times in total;    pages with same visitedUser are internally sorted by visitTimes.
  16.     InfoNode firstPageNode;
  17. }
  18. /*
  19.  * UserNode LinkList is sorted by visiting times. From large to less. To solve question 3.
  20.  */
  21. class InfoNode{
  22.     int pageId;    //indicate the page it belongs
  23.     int userId;    //indicate the user it belongs
  24.     int times;    //indicate how many times has this user visit page_belong
  25.     InfoNode nextNodeByUser;
  26.     InfoNode nextNodeByPage;
  27. }

Find longest substring without repeating

http://www.geeksforgeeks.org/amazon-interview-experience-set-149-campus-internship/
Round3, Q1
Given a string, find the longest substring without repeating characters. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”.

Instead of using a array for all 26 alphabets, I use hashmap to store the char which are already read.

/*
* http://www.geeksforgeeks.org/amazon-interview-experience-set-149-campus-internship/
* Round3, Q1
* Given a string, find the longest substring without repeating characters.
* For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA” and “DEFGAB”.
*/
import java.util.HashMap;

public class LengthOfLongestSubstringWithoutRepeating {

public static int findLongestSubstringWithoutRepeating(String s){
if(s==null){
return -1;
}
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
int max_start = 0;        //Record the position where the max substring starts
int max_len = 1;        //Record the length of max substring
int curr_len = 1;        //Record the length of current substring
hm.put(s.charAt(0), 0);
for(int i=1;i<s.length();i++){
if((hm.get(s.charAt(i))==null)||(i-curr_len>hm.get(s.charAt(i)))){
//Current char hasn’t been read, or position of previous char c is not in the range of current substring
curr_len++;
hm.put(s.charAt(i), i);
}
else{
if(curr_len>max_len){    //Find a new max length, update the max_len
max_len = curr_len;
max_start = i – curr_len;
}
curr_len = i – hm.get(s.charAt(i));    //Calculate the new length of current substring
hm.put(s.charAt(i), i);
}
}
//        System.out.println(“max start:” + max_start);
//        System.out.println(“max len:” + max_len);
return max_len;
}

public static void main(String[] args) {
//String str = “abcdefeczqposc”;
String str = “GEEKSFORGEEKS”;
int max_len = findLongestSubstringWithoutRepeating(str);
System.out.println(max_len);
}

}

Find max difference two elements

http://www.geeksforgeeks.org/amazon-interview-experience-set-149-campus-internship/

  1. /*
  2.  * Q2. Given an array arr[] of integers, find out the maximum difference between any
  3.  * two elements such that larger element appears after the smaller number in arr[].
  4.  * Print the indices of the two elements also.
  5.  * Example: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (difference between 10 and 2).
  6.  * If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (difference between 7 and 9).
  7.  */
  8. public class MaxDifferenceTwoElement {
  9.     public static void main(String[] args) {
  10.         int[] array = {7, 9, 5, 6, 3, 2};
  11.         findMaxDifference(array);
  12.     }
  13.     public static void findMaxDifference(int[] array){
  14.         if(array==null){
  15.             return;
  16.         }
  17.         int min_pos = 0;    //position of min value
  18.         int max_pos = 0;    //position of max value
  19.         int max_gap = Integer.MIN_VALUE;
  20.         for(int i=1;i<array.length;i++){
  21.             if(array[i]-array[min_pos]>max_gap){
  22.                 max_pos = i;
  23.                 max_gap = array[i] – array[min_pos];
  24.             }
  25.             else if(array[i]<min_pos){
  26.                 min_pos = i;
  27.             }
  28.         }
  29.         System.out.println(max_gap);
  30.     }
  31. }

Find TopK value, by small heap

This is the code to get top k values from a input.

  1. /*
  2.  * Get the Top Largest K values from small heap
  3.  */
  4. public class TopK {
  5.     public static void main(String[] args) {
  6.         int[] array = {5, 13,10,12,15,11, 8,23, 40, 16, 83, 23, 31, 73, 59, 25, 75};
  7.         findTopK(array, 5);
  8.     }
  9.     public static void findTopK(int[] array, int k){
  10.         int[] heap = new int[k+1];
  11.         heap[0] = k;     //use heap[0] to record the length of heap
  12.         for(int i=0;i
  13.             heapPut(heap, array[i]);
  14.         }
  15.         printArray(heap);
  16.     }
  17.     /*
  18.      * Small heap, put value in heap
  19.      */
  20.     public static void heapPut(int[] heap, int value){
  21.         if(heap==null){
  22.             return;
  23.         }
  24.         if(value>heap[1]){
  25.             heap[1] = value;
  26.             heapAdjust(heap, 1);
  27.         }
  28.     }
  29.     /*
  30.      * Small heap, put value in heap
  31.      */
  32.     public static void heapAdjust(int[] heap, int pos){
  33.         if(heap==null||pos*2>heap[0]){    //heap is empty, or the position is not correct
  34.             return;
  35.         }
  36.         if(heap[pos]<=heap[pos*2]&&(heap[0]==pos*2||heap[pos]<=heap[pos*2+1])){
  37.         //the heap[pos] is already smallest
  38.      //heap[0]==pos*2 is to check if pos has right child. If it doesn’t has right child, then don’t compare with right child.
  39.             return;
  40.         }
  41.         int minPos = (heap[0]==pos*2)?pos*2:heap[pos*2]//heap[pos] is not the smallest, get the min pos
  42.         arrayExchange(heap, pos, minPos);
  43.         heapAdjust(heap, minPos);
  44.     }
  45.     /*
  46.      * Exchange pos1 and pos2 in array[]
  47.      */
  48.     public static void arrayExchange(int[] array, int pos1, int pos2){
  49.         if(array==null||pos1>array.length-1||pos2>array.length-1){
  50.             return;
  51.         }
  52.         int tmp = array[pos1];    array[pos1] = array[pos2]; array[pos2] = tmp;
  53.         return;
  54.     }
  55.     public static void printArray(int[] array){
  56.         if(array==null){
  57.             return;
  58.         }
  59.         for(int i=1;i
  60.             System.out.print(array[i]+” “);
  61.         }
  62.         System.out.println();
  63.     }
  64. }

find the path between nodes in binary tree

I made this code, it is because I saw there is a problem in geeksforgeeks: link
But it only gets the distance of 2 nodes. I’m thinking how to get the path of the 2 nodes. The idea is inspired by Pavel Fusu’s code. link

The process is below:
1. Find the lca node. link
2. Find the path1 and path2 from the lca to the designated nodes.
3. Merge path1 and path2

  1. public class LCA {
  2.     public static void main(String[] args) {
  3.         Tree tree = Tree.getTree();
  4.         ArrayList<Integer> path = findNodesPath(tree, 9, 18);
  5.         printPath(path);
  6.     }
  7.     /*
  8.      * Find the LCA of node1 and node2
  9.      */
  10.     public static Tree findLca(Tree tree, MyInteger lcaNum, int node1, int node2){
  11.         if(tree==null){
  12.             return null;
  13.         }
  14.         MyInteger lLca = new MyInteger(0);
  15.         Tree ltree = findLca(tree.left, lLca, node1, node2);
  16.         if(lLca.i==2){
  17.             lcaNum.i=2;
  18.             return ltree;
  19.         }
  20.         MyInteger rLca = new MyInteger(0);
  21.         Tree rtree = findLca(tree.right, rLca, node1, node2);
  22.         if(rLca.i==2){
  23.             lcaNum.i=2;
  24.             return rtree;
  25.         }
  26.         int selfLca = 0;
  27.         if(node1==tree.value||node2==tree.value){
  28.             selfLca = 1;
  29.         }
  30.         lcaNum.i = lLca.i + rLca.i + selfLca;
  31.         return tree;
  32.     }
  33.     /*
  34.      * Find the path of node under root.
  35.      */
  36.     public static boolean findPath(Tree root, int node, ArrayList<Integer> path){
  37.         if(root==null){
  38.             return false;
  39.         }
  40.         boolean lHas = findPath(root.left, node, path);
  41.         if(lHas){
  42.             path.add(root.value);
  43.             return true;
  44.         }
  45.         boolean rHas = findPath(root.right, node, path);
  46.         if(rHas){
  47.             path.add(root.value);
  48.             return true;
  49.         }
  50.         if(root.value==node){
  51.             path.add(root.value);
  52.             return true;
  53.         }
  54.         return false;
  55.     }
  56.     /*
  57.      * Find the path of node1(or node2) under root.
  58.      */
  59.     public static boolean findPath(Tree root, int node1, int node2, ArrayList<Integer> path){
  60.         if(root==null){
  61.             return false;
  62.         }
  63.         if(findPath(root.left, node1, node2, path)){
  64.             path.add(root.value);
  65.             return true;
  66.         }
  67.         if(findPath(root.right, node1, node2, path)){
  68.             path.add(root.value);
  69.             return true;
  70.         }
  71.         if(root.value==node1||root.value==node2){
  72.             path.add(root.value);
  73.             return true;
  74.         }
  75.         return false;
  76.     }
  77.     /*
  78.      * Find the path between node1 and node2
  79.      */
  80.     public static ArrayList<Integer> findNodesPath(Tree tree, int node1, int node2){
  81.         MyInteger lcaNum = new MyInteger(0);
  82.         Tree lca = findLca(tree, lcaNum, node1, node2);
  83.         if(lcaNum.i<2){
  84.             return null;
  85.         }
  86.         ArrayList<Integer> path1 = new ArrayList<Integer>();
  87.         ArrayList<Integer> path2 = new ArrayList<Integer>();
  88.         findPath(lca.left, node1, node2, path1);
  89.         findPath(lca.right, node1, node2, path2);
  90.         path1.add(lca.value);
  91.         while(!path2.isEmpty()){
  92.             path1.add(path2.remove(path2.size()-1));
  93.         }
  94.         return path1;
  95.     }
  96.     public static void printPath(ArrayList<Integer> path){
  97.         if(path==null){
  98.             return;
  99.         }
  100.         Iterator<Integer> itr = path.iterator();
  101.         while(itr.hasNext()){
  102.             System.out.print(itr.next()+” “);
  103.         }
  104.         System.out.println();
  105.     }
  106. }
  107. public class MyInteger{
  108.     public int i = 0;
  109.     public MyInteger(int i){
  110.         this.i = i;
  111.     }
  112.     public String toString(){
  113.         return String.valueOf(i);
  114.     }
  115. }

find lca(lowest common ancestry) in a binary tree

public static Tree findLca(Tree tree, MyInteger lcaNum, int node1, int node2){
   if(tree==null){
      return null;
   }
   MyInteger lLca = new MyInteger(0);
   Tree ltree = findLca(tree.left, lLca, node1, node2);
   if(lLca.i==2){
      lcaNum.i=2;
      return ltree;
   }
   MyInteger rLca = new MyInteger(0);
   Tree rtree = findLca(tree.right, rLca, node1, node2);
   if(rLca.i==2){
      lcaNum.i=2;
      return rtree;
   }
   int selfLca = 0;
   if(node1==tree.value||node2==tree.value){
      selfLca = 1;
   }
   lcaNum.i = lLca.i + rLca.i + selfLca;
   return tree;
}

 

PostOrderTraverse

  1. public class PostOrderNonTraverse {
  2.     public static void main(String[] args){
  3.         Tree t17 = new Tree(17);
  4.         Tree t16 = new Tree(16);
  5.         Tree t15 = new Tree(15);
  6.         Tree t14 = new Tree(14);
  7.         Tree t13 = new Tree(13);
  8.         Tree t12 = new Tree(12);
  9.         Tree t11 = new Tree(11);
  10.         Tree t10 = new Tree(10);
  11.         Tree t9 = new Tree(9);
  12.         Tree t8 = new Tree(8);
  13.         Tree t7 = new Tree(7);
  14.         Tree t6 = new Tree(6);
  15.         Tree t5 = new Tree(5);
  16.         Tree t4 = new Tree(4);
  17.         Tree t3 = new Tree(3);
  18.         Tree t2 = new Tree(2);
  19.         Tree t1 = new Tree(1);
  20.         t8.left = t9;
  21.         t6.left = t8;
  22.         t5.right = t7;
  23.         t4.left = t5;
  24.         t4.right = t6;
  25.         t2.left = t4;
  26.         t1.left = t2;
  27.         t1.right = t3;
  28.         t6.right = t10;
  29.         t3.left = t11;
  30.         t3.right = t12;
  31.         t12.left = t13;
  32.         t12.right = t14;
  33.         t13.left = t15;
  34.         t13.right = t16;
  35.         PostTraverse(t1);
  36.     }
  37.     public static void PostTraverse(Tree tree){
  38.         ArrayList s = new ArrayList();
  39.         s.add(tree);
  40.         Tree prev = null;
  41.         Tree curr = null;
  42.         Tree next = null;
  43.         while(!s.isEmpty()){
  44.             curr = s.get(s.size()-1);
  45.             if(prev==null||prev.left==curr||prev.right==curr||(curr.left != prev)&&(curr.right != prev)){
  46.                 //新进来的
  47.                 if(curr.left==null&&curr.right==null){    //是叶子
  48.                     System.out.println(curr.value);
  49.                     s.remove(s.size()-1);
  50.                 }
  51.                 else{
  52.                     if(curr.right!=null){
  53.                         s.add(curr.right);
  54.                     }
  55.                     if(curr.left!=null){
  56.                         s.add(curr.left);
  57.                     }
  58.                 }
  59.             }
  60.             else if(curr.left == prev){    //从左子树返回的
  61.                 if(curr.right!=null){
  62.                     s.add(curr.right);
  63.                 }
  64.                 else{
  65.                     System.out.println(curr.value);
  66.                     s.remove(s.size()-1);
  67.                 }
  68.             }
  69.             else{    //从右子树返回
  70.                 System.out.println(curr.value);
  71.                 s.remove(s.size()-1);
  72.             }
  73.             prev = curr;
  74.         }
  75.     }
  76. }
  77. class Tree{
  78.     public Tree(int value){
  79.         this.value = value;
  80.     }
  81.     public Tree left;
  82.     public Tree right;
  83.     public int value;
  84. }

Solve the problem that hive is pending for a long time for MapReduce select

I have partition_table in Hive. It run the select command well:
Hive>select * from partition_table;

When I ran
Hive>select count(*) from partition_table;
Starting Job = job_1414213419655_0001, Tracking URL = http://centmaster:8088/proxy/application_1414213419655_0001/
Kill Command = /home/hadoop/hadoop-2.3.0/bin/hadoop job  -kill job_1414213419655_0001

After that, Hive is pending there for 40 minutes, until I pressed Ctrl+c to stop it.

I checked http://centmaster:8000/logs/yarn-root-nodemanager-centmaster.log, I found the Exception information:
mapreduce.shuffle set in yarn.nodemanager.aux-services is invalid.The valid service name should only contain a-zA-Z0-9_

and can not start with numbers

I googled it. I found out it is because the yarn.nodemanager.aux-services in yarn-site.xml is not correctly set.
I changed it from mapreduce.shuffle into mapreduce_shuffle.
mapreduce.shuffle is used in hadoop2.1, 2.2. mapreduce_shuffle shall be used in hadoop2.3.