Skip to the solution
EasyDSA12 min read

What is the difference between an array and a linked list?

1.1k views

The problem

Question presented to candidate: "Explain the difference between an array and a linked list. When would you pick one over the other, and what does each look like in Python? If there is time, sketch a small linked list in Python."

What a strong answer should cover:

  • Memory layout: one contiguous block versus separate nodes joined by references.
  • Costs: O(1) indexing versus O(n) walking; O(n) front inserts versus O(1) relinking at a node you already hold (and that finding the node is still O(n)).
  • Dynamic arrays and amortized O(1) appends; the extra memory per node in a linked list.
  • Cache locality as the practical reason arrays usually win.
  • Python specifics: list is a dynamic array (of references), the standard library has no linked list class, and collections.deque is the tool for O(1) work at both ends.

Code / implementation expected: Optional โ€” a small Node class with insert, delete and traversal in Python is a common follow-up.

  • arrays
  • linked-list

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

12 min read ยท 14 sections

Python code

Edit the code and run it here. Each printed line with an expected value becomes a test.

PythonCounting the work: array moves vs list hops
PythonA singly linked list from scratch

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 5 of 154 in DSA

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All DSA questions
Up nextEasy

What is the fast and slow pointer technique?

Continue