Ad by Google
In BFSI domain mostly questions are comes from the Class loader, Thread, Concurrency api, Array, Collection, JVM architecture, Java Memory Model, String pool, String, StringBuilder, StringBuffer, and Puzzle.
Normally interviewer will ask you to rate yourself in the above given API's, it is again depends on interviewer state of mind.
Interviewer: Ok lets start with Collection, What is the different between ArrayList and LinkedList?
The demand of this question is to just make you comfort while in a interview ground.
Smart Candidate :)
ArrayList and LinkedList both are implementation of List interface. ArrayList implementation is based on dynamically resizing array whereas LinkedList implementation is based on doubly linked list. ArrayList and LinkedList both implements Cloneable and Serializable interface apart from that ArrayList also implements RandomAccess interface and LinkedList implements Deque interface.
As you know that ArrayList implements RandomAccess interface, Just because of that
listIterator() these methods are run in constant time.
But adding and removing of element from anywhere(random) requires shifting of all the elements means time consuming process(performance issue), also if you add more element other than the initial capacity of the array, a new array will be created and old array elements will copied to newly created array. Means adding element to an ArrayList is O(n) in the worst case but constant on average, here random insertion and deletion cause performance issue. ArrayList has a capacity to store the elements inside the ArrayList, the capacity of the ArrayList automatically increased using ensureCapacity(int minCapacity) method of the ArrayList.
LinkedListAs LinkedList is based on Doubly-Linked which will helps you to insert or remove elements from anywhere with constant time. Means insertion and deletion operations are very fast as comparing to ArrayList. But iteration is very slow(o(n)). In LinkedList there is no any concept of initialCapacity means there is no any overhead of shifting elements again the performance issue reduce here while adding elements.
We all know about time and space tradoff, adding elements inside LinkedList is faster than ArrayList but memory usage is high as comapring to ArrayList, because LinkedList is based on doubly linked List so each element of LinkedList has previous and next element are also need to be stored.One more thing about LinkedList as LinkedList implements Deque interface It will provides the Queue implementation.
The above will really impressed the interviewer and with smile he will ask you the next question. Ok now tell me, We have 10 millions of data and want store inside the collection from the ArrayList and LinkedList which one is better?
Before answering to this question you must have to ask Is there any specific things to keep in mind while selecting one of them(ArrayList or LinkedList) For example random insertion/deletion, time or space based on the these questions you have to give the answer, see the answer of this question on my next post Which collection is better to store 10 millions of data
Few very popular java interview question:
Why String is immutable?
What are the methods of java.lang.Object class
Why Hash Code method in java?
Serializable Vs Externalizable
What are the key differences between Comparable and Comparator.
Java Program to find all missing numbers from an array
Don't forgot to post your comments/feedback/suggestion about this post or if you have something to add on this.