5 Tricky Interview Questions for Java ArrayList

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:



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.

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.

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.



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.

 

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



Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Java. Tagged with , .

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *