5 Tricky Interview Questions for Java ArrayList

0

I have been involved in many a java interviews and following are five tricky ones where I found several junior to mid-level Java developers faltering once in a while. Thus, I thought to put an article around these questions to help junior Java developers make familiar with these questions. The article presents the 5 tricky interview questions in relation with Java ArrayList, I believe,  could get the interviewee score some browny points, if answered well. Let me know if you agree and would like to add another set of questions to the list below:

  1. How does the size of Arraylist increases automatically? Could you share the code?
    This is the most trickiest one and many were unable to answer this one. The trick here is the fact that when someone tries to add an object to the arraylist, Java checks to ensure that there is enough capacity in the existing array to hold the new object. If not, a new array of a greater size is created, the old array is copied to new array using Arrays.copyOf and the new array is assigned to the existing array. Look at the code below (taken from Java ArrayList Code at GrepCode.com).
    ArrayList Add Method

    ArrayList Add Method


    ensureCapacity Method to Handle ArrayList Size

    ensureCapacity Method to Handle ArrayList Size


    Pay attention to the fact that a new array is created; Objects from old array is copied to the new array and the new array is assigned to the existing array member variable.

     

  2. When would you use ArrayList and when LinkedList?
    This is again one of the question where many of the interviewee got confused. Primarily, one would want to use ArrayList in the cases where there is a greater need to access the element rather than insertion or deletion. On the other hand, one would want to use the LinkedList when there is a greater need for insertion and deletion and not much use of the accessing the element at a particular index. This is mainly because the worst time complexity of accessing an element/object in an ArrayList is always “1″ whereas in LinkedList it can be “n”. In case of addition or deletion of element in an ArrayList, there is an operation such as System.arraycopy is involved. This is quite an expensive operation and thus, in usecases where frequent insertion and deletion of elements are involved, LinkedList is preferred where it is all about adding or removing the node and re-linking the existing node.

     

  3. While passing an ArrayList to a method or returning an ArrayList from a method, when is it considered to be a security violation? How to fix this problem?
    When one passes the array to a method, if array is assigned to the member variable directly without making a copy of it, it could lead to a scenario that the original array when changed by the caller will also end up changing the array passed to the method. Look at the code below to see when it is security violation and subsequently how to fix the same.
    Array Stored Directly - Considered as Security Violation

    Array Stored Directly – Considered as Security Violation

     

    Copy the Array as a Fix for the Security Violation

    Copy the Array as a Fix for the Security Violation

    Pay attention the fact that a copy of newMyArray is created and assigned to the member variable, myArray.

  4. How do you copy one ArrayList to another? Could you share the code?
    Following are different techniques for copying an ArrayList object to another ArrayList:
    1. Use clone() method such as following: ArrayList myObject = new ArrayList<Object>(myTempObject);
    2. Use ArrayList constructor object such as following:
    3. use Collection.copy method
    Note that option 1 and 2 creates a shallow copy.

     

  5. How does the addition and deletion of an object at any index happens in ArrayList? Is it expensive? Explain?

    In case of addition/deletion of elements in an ArrayList, there is an operation such as System.arraycopy is involved. This is quite an expensive operation and for usecases like these where frequent insertion and deletion are required, one may want to choose other Java collection such as LinkedList. Take a look at the code below:

    Adding an element in ArrayList at index i

    Adding an element in ArrayList at index i

     

    Removing an element in an ArrayList at index i

    Removing an element in an ArrayList at index i

 

[adsenseyu1]

Ajitesh Kumar

Ajitesh is passionate about various different technologies including programming languages such as Java/JEE, Javascript, PHP, .NET, C/C++, mobile programming languages etc and, computing fundamentals such as application security, cloud computing, API, mobile apps, google glass, big data etc.

Follow him on Twitter and Google+.
Share.

Leave A Reply


× one = 1