What is the difference between an array and a linked list, and when would you choose each?
ReportedSource: GeeksforGeeks interview experience: Wipro Elite
Array: contiguous, O(1) index access, O(n) middle insert/delete, cache-friendly. Linked list: O(n) access, O(1) insert/delete at a known node, pointer overhead. Arrays win for most workloads; lists for frequent splicing, queues, LRU.
Array. Contiguous memory, fixed size (dynamic arrays such as ArrayList resize by copying).
- Access by index: O(1).
- Insert/delete in the middle: O(n) (shift elements).
- Cache-friendly, low memory overhead.
Linked list. Nodes scattered in memory, each pointing to the next (and previous, in a doubly linked list).
- Access by index: O(n) (walk from the head).
- Insert/delete at a known node: O(1) (update pointers); at the head: O(1).
- Extra memory per node for pointers; poor cache locality.
Choose an array for frequent random access, iteration speed and compact storage (most real-world cases). Choose a linked list for frequent inserts/deletes at known positions, queues/deques, LRU caches (combined with a hash map), or when you cannot afford occasional resize copies.
Java note. ArrayList vs LinkedList: in practice ArrayList is faster for most workloads because of locality.