Apple Interview Question
Staff EngineersCountry: United States
- LinkedList is the best choice when your frequent operation is insertion or deletion of an element from the middle.
- LinkedList is the worst choice when your frequent operation is retrieval of objects from the list.
- ArrayList is the best choice if our frequent operation is Retrieval
- ArrayList is the worst choice if our frequent operation is insertion and deletion of element from the middle.
Linked list is used in the case where you care about fast insertions and lookups are not performed that often. Due to insertions being constant time, the linked list does not need to resize as necessary but instead just append the node at the end or beginning of the linked list. This makes them a very flexible data structure.
- prudent_programmer March 17, 2018On the other hand, an array is used when lookup via indexing is performed often. In this case, lookups give constant time but the downside is resizing or inserting elements into the array. Another advantage of arrays is that arrays offer is binary search for lookups in the case where the array is sorted. Unfortunately, with linked lists binary search can not be used.