Data Structures
Array vs Linked List, with everyday examples
When to use each one, and why this question comes up in almost every job interview.
·1 min read
The array: a fixed row of seats
Picture a row of seats in a cinema. You can reach any seat instantly by its number, but adding a seat in the middle means shifting everyone after it. That's exactly an array: access is fast, O(1), and inserting in the middle is slow, O(n).
The linked list: a train of carriages
Each carriage holds on to the next one. You can add a carriage anywhere easily, but to reach the tenth carriage you have to walk through the nine before it.
- Need fast access by position? Array.
- Adding and removing a lot in the middle? Linked List.
- Not sure? Start with an array; it fits most practical cases.