Sunday, September 30, 2018

LinkedList -- find nth element

    public Node findNthFromLast(int nth){
    
    Node firstPointer=first;
    Node secondPointer=first;
    
    for(int i=0;i<nth;i++){
    firstPointer=firstPointer.next;
    }
    while(firstPointer!=null){
    firstPointer=firstPointer.next;
    secondPointer=secondPointer.next;
    }
    
    return secondPointer;

    }

1) The basic concept of this program is to create 2 pointers , first and second.
2) Now, say we have to find nth element from the last.
3) Idea is to create a gap of n between first pointer and second pointer. That is some thing we do by moving the first pointer to nth position from starting.
4) Now iterate the first and second pointer together till first pointer becomes null.

Length of linkedList

    public int findLengthLinkedList(Node nd){
    
    int size=0;
    if(nd==null)
    return 0;
    
    while(nd!=null){
    nd = nd.next;
    size++;
    }
    
    return size;
    }
    
    public int findLengthLinkedListRecursion(Node nd){
    
    if(nd==null)
    return 0;
    
    int size = findLengthLinkedListRecursion(nd.next)+1;
    return size;

    }

Reverse Linked-list in Pairs (Iteration and recursions)

Reverse LinkedList in Pairs:-

Iterative Approach


//1->2->3->4->5->6
public Node reverseTheLinkedListInPairs(Node nd){
Node current = nd;
Node temp=null;
Node head=null;
while(current!=null && current.next!=null){
if(temp!=null)
temp.next.next=current.next; //this step joins 2 to 3 when 3 & 4 exchange happens
temp = current.next;//This takes 2 in temp variable
current.next = temp.next; //this step joins 1 to 3
temp.next=current; //this step join 2 to 1
current=current.next; // this steps makes current as 3
if(head==null)
head=temp;// this is basically maintaining the root
}
return head;
}

Recursive Approach

    public Node reverseTheLinkedListInPairRecursion(Node nd){
    
    if(nd==null || nd.next==null)
    return nd;
    
    Node temp = nd.next;
    nd.next=temp.next;
    temp.next=nd;
    nd.next= reverseTheLinkedListInPairRecursion(nd.next);    
    return temp;
    }

Thursday, September 27, 2018

LinkedList - Reverse (recursion and iterative)

Recursive Approach

//2->4->5->6->7->8
   public Node reverseTheLinkedlist(Node nd){    
  if(nd.nextNd==null)
  return nd;
   
  Node remaining = reverseTheLinkedlist(nd.nextNd);
  System.out.println("The value of remaining is : "+remaining.value);
       nd.nextNd.nextNd=nd;
       nd.nextNd=null;
  return remaining;

   }

1) In the above program, first we check if the node sent in the program argument is null or the next value to that node is null. If so, then we return the node sent in the function argument.

2) remaining -- The value of this node in this program will basically become the last node in the list. Here remaining will become 8.

3) In each return cycle of recursion (except the last node), node is swipped with its next value and the node is then set to null.

The program will work in the following way:-

remaining = 8
Cycle 1 : 8->7->null

remaining = 8
Cycle 2 : 7->6, so result will become 8->7->6

remaining = 8
Cycle 3 : 6->5, so result will become 8->7->6->5

likewise, it will work for node 2 & 4 also.

Iterative Approach

This is the iterative approach:-

public Node reverseTheLinkedList(Node nd){
Node current = nd;
Node previous=null;
Node next=null;
while(current!=null){
next=current.next;
current.next=previous;
            previous=current;
            current=next;
}
return previous;

}

Tuesday, July 24, 2018

Improve performance of web app

Suppose we have a spring boot app which is hosting some of our web services, best way to optimize and improve performance using below:-

1) We can use gatlin tool which can work with an http app and give us a good picture of whats going in terms of request and response time.

Some of the code level optimization:-

1) Use string builder/buffer instead of string. All though with Java 8, there has been some optimization on string pool.
2) Avoid recursive programming as it leads to stack overflow exception at times.
3) When using regular expressions, avoid building patterns every time , better create a static pattern and use it for each validation.
4) Avoid creating/destroying too many threads as they are costly and heavy for JVM to create and destroy.

5) Heap size tuning:-

There are number of factors which should be taken into account while we are tuning the heap size

     (a) On a single instance of the server, how many war to ear files are going to live ?
     (b) How many class files will be loaded (including the third party) ?
     (c) Caching in terms of database and app etc
     (d) How many threads will be opened per app.

One good way to know this is by running Gatlin on the app with proper load and get the metrics and based on that , it can help us decide the numbers to configure for heap size (xms xmx), stack size(xss) etc.

6) Fine tune the sql queries
7) Connection pooling is also a good way to improve the performance of the app.
8) Use JDBC batching instead of executing single query which will also improve performance (We can do that with prepared statement and hibernate in java).
9) Use caching solutions like memcache, redis etc
10) Have a load balancer to scale up your system.

JVM notes

What is stack and heap in JVM ?






How to avoid memory leaks:-

1) Do not hold heavy objects or collections in static variables as they prevent GC to kill the object.
2) Use StringBuffer or builder to concatenate the strings instead of using string as the later will keep filling the string pool which can prevent heap size to exhaust.
3) String is no more stored in permgen space as with java 8 it has been discontinued, so because of string pool, there will be no memory out of exception.
4) Unclosed streams, although with Java 8/7 we can use try with resource and it will auto close.
5) Unclosed connections
6) Start adding objects in HashSet without implementing the hashcode and equals method for these objects, as a result, all objects , even though some of them being equal will keep getting added.

How to inspect memory leakage:-

1) Good code review
2) -verbose:gc parameter to the JVM configuration 
3) Eclipse memory analyzer