Wednesday, 29 June 2016

Java Interview : Usage : ArrayList and LinkedList

Very Common Java Interview Question : When to use ArrayList and when LinkedList ?

Short is answer is : Use ArrayList for better traversal or search and use LinkedList for better insertions and deletions.

Explanation :

1) Linked List is a Doubly Linked List implementation of List Interface, where as ArrayList is an re sizable Array implementation of List Interface. LinkedList can act as both List and Queue where as ArrayList can only act as List.

2) LinkedList has a reverse iterator which it uses to reach an element from end if the path is shorter.

3) A search operation is faster in ArrayList  : it has a complexity of O(1) where as in an LinkedList it is O(n) as you have to iterate over number of records in LinkedLIst.

4) Insertions are faster in LinkedList than ArrayList. In LinkedList we just have to break the nodes and put our entry. But in case of arrayList, there is an overhead of moving the array indexes and then re sizing the array if the size has increased.

4) Deletions are almost the same in ArrayList and Linked List because we have to traverse the node to delete. And in ArrayList they have to update to a new Array. ArrayList is slower than LinkedList because it has to free up a slot in the middle of the array which means moving some references here and there and in the worst case reallocating the entire array. LinkedList just has to manipulate some references.

5) For Manipulation of data use LinkedList and for search operations use ArrayList.

Wednesday, 22 June 2016

Classic Singleton in Java

Q: How to create a Simple singleton class in Java.
A:
public class SimpleSingleton {

   private static SimpleSingleton instance = null;
   private SimpleSingleton () {
      // we declare it private so that you cant do below:
      //NonSynchronizedSingleton t = new NonSynchronizedSingleton(); 
   }
   public static SimpleSingleton getInstance() {
      if(instance == null) {
         instance = new SimpleSingleton();
      }
      return instance;
   }
}


public class SimpleSingletonDemo {
   public static void main(String[] args) {
      SimpleSingleton tmp1 = SimpleSingleton.getInstance( ); //Line no 1
      SimpleSingleton tmp1 = SimpleSingleton.getInstance( ); //Line no 2
   }
}


It is super easy to do this.
1) Declare the constructor as private  which is a must so that the no object can be initialized.
2) From Line no 1 :  In the public static getInstance method which returns SimpleSingleton we check if instance has already been initialized which is null as of now, so it goes ahead and initialize it.
3) Next time it will come to the getInstance method at Line No 2, it will find the instance as already initialize, so it will return the same object.

Enjoy Singleton.


Java Interview SQL Directive : Order of operation (SQL Questions)

Q: Define the order in operation in which SQL directive gets executed.

A:
  1. FROM clause
  2. WHERE clause
  3. GROUP BY clause
  4. HAVING clause
  5. SELECT clause
  6. ORDER BY clause
First it is going to run the From clause to get the tables.
Then the WHERE clause to filter it. That is why FROM AS names are available in WHERE. EX: (select st.name  as newName from student st where st.age > 30 order by newName)
Then the GROUP BY AND HAVING to sum the groups and filter groups.
Then comes the SELECT clause.
In the last the ORDER BY , and the AS names from Select are available in ORDER BY clause.
More interview questions to follow.