Sunday, 14 October 2018

Calculate Size of binary tree Using Recusrion

public class CalculateSizeUsingRecusrion {
    public static void main(String args[]) {
             Node rootNode = new Node("4");
             rootNode.left = new Node("2");
             rootNode.left.left = new Node("1");
             rootNode.left.right = new Node("3");
             rootNode.right = new Node("6");
             rootNode.right.right = new Node("7");
             rootNode.right.left = new Node("5");
           
             System.out.println(calcSize(rootNode));
    }
   
    static int calcSize(Node node) {
        if(node==null) {
            return 0;
        }
        return calcSize(node.left)+1+calcSize(node.right);
    }
}

class BinayTree{
    static class Node{
        String value;
        Node left;
        Node right;
       
        Node(String val){
            value = val;
            left = right = null;
        }
       
    }
   
}

output: 7

Calculate Size of binary tree Using Without Recusrion

public class CalculateSizeUsingWithoutRecusrion {
    public static void main(String args[]) {
             Node rootNode = new Node("4");
             rootNode.left = new Node("2");
             rootNode.left.left = new Node("1");
             rootNode.left.left.left = new Node("9");
             rootNode.left.right = new Node("3");
             rootNode.right = new Node("6");
             rootNode.right.right = new Node("7");
             rootNode.right.left = new Node("5");
             rootNode.right.right.right = new Node("8");
           
             System.out.println(calcSize(rootNode));
    }
   
    static int calcSize(Node node) {
        Queue<Node> queue = new LinkedBlockingQueue<>();
        queue.add(node);
        int count = 0;
        while(!queue.isEmpty()) {
            Node node1 = queue.remove();
            count= count+1;
            if(null!=node1.left) {
                queue.add(node1.left);
            }
            if(null!=node1.right) {
                queue.add(node1.right);
            }
        }
        return count;
    }
}



class BinayTree{
    static class Node{
        String value;
        Node left;
        Node right;
       
        Node(String val){
            value = val;
            left = right = null;
        }
       
    }
   
}


output:9

How to revert merged commit in git

Assume i have a branch master and i have done multiple commit to that branch till  1e3a87c,
now create a new branch from master is dev , push some  commit to dev branch till b038437,

now merge dev branch changes into master as below:

# checkout to master branch
# git pull origin dev
# git push origin master

and if you want to merge with tag as below:
#create a tag on dev branch using git tag tag_name  
# checkout to master branch
#git merge tag_name
#git push origin master

Now dev branch merge into master branch

As if we want to revert commit before merge then need to use below:

we need to find out the commit id before merge like 1e3a87c in my case


#git reset 1e3a87c
#git reset --hard
#git push -f origin master



Ref: https://stackoverflow.com/questions/7099833/how-to-revert-a-merge-commit-thats-already-pushed-to-remote-branch/7100005#7100005

Wednesday, 10 October 2018

PrintExcelColumnNameFromColumnNumber

public class PrintExcelColumnName {
    public static void main(String args[]) {
        printColumnName(20000);
    }
   
    static void printColumnName(int num){
        StringBuilder sb = new StringBuilder();
        while(num>26) {
            int diff = num%26;
            num = num/26;
            sb.append((char)(diff-1+'A'));
        }
        sb.append((char)(num-1+'A'));
       
        System.out.println(sb.reverse().toString());
    }
   
   
}

output: ACOF

Tuesday, 9 October 2018

FindUniqueDigitsNumber from list


public class FindUniqueDigitsNumber {
    public static void main(String args[]) {
        int l = 10;
        int r = 30;
        findUniqueDigitNumber(l, r);
       
    }
   
    static void findUniqueDigitNumber(int l, int r){
        for(int i=l;i<=r;i++) {
            boolean[] boo = new boolean[100];
            int num = i;
            while(num!=0) {
                if(boo[num%10]) {
                    break;
                }
                boo[num%10] = true;
                num = num/10;
            }
            if(num==0)
            {
                System.out.println(i);
            }
           
        }
    }
   
}

output(11 and 22 missing):
10
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30

Monday, 8 October 2018

FindMissing number in AP log(n)

public class FindMissing {

    public static void main(String[] args) {
        int[] arr = {2,4,6,8,12,14,16,18,20,22,24,26};
        int val = findMissing(arr, 0, arr.length-1, 2);
        System.out.println(val);
    }
   
   
    static int findMissing(int[] arr, int start, int end,int diff) {
        if (end >= start) {
            int mid = (start + end) / 2;
            if (mid<end && (arr[mid+1] - arr[mid]) != diff) {
                return arr[mid] + diff;
            } else if (mid>0 && (arr[mid] - arr[mid-1]) != diff) {
                return arr[mid-1] + diff;
            }
//if difference in left and right element of mid are equal to diff then we need to decide to move either //left or right as we will check if our series is correct till mid then we will move to right otherwise left
//for checking till mid series  is correct we will use formula : arr[mid]==arr[0]+mid*diff


else if (arr[mid] == arr[0] + mid * diff) {
                return findMissing(arr, mid + 1, end, diff);
            } else {
                return findMissing(arr, start, mid - 1, diff);
            }

        }
        return 0;
    }

}

Sunday, 23 September 2018

LRU vs LFU


Least Recently Used ( LRU) 
 
The eldest element, is the Least Recently Used (LRU). The last used timestamp is updated when an element is put into the cache or an element is retrieved from the cache with a get call.

Less Frequently Used ( LFU)

For each get call on the element the number of hits is updated. When a put call is made for a new element (and assuming that the max limit is reached for the memory store) the element with least number of hits, the Less Frequently Used element, is evicted.

links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...