← Learning Hub
Beginner

Arrays and Two Pointers

Python's list is a dynamic array: items sit next to each other, so reading by index is O(1). Adding at the end is fast; inserting at the front moves every item (O(n)).

Arrays (Python lists) store items side by side, which makes reading any position instant. Many interview problems are about arrays: finding pairs, moving zeros, rotating, merging. Learning to scan an array once, cleverly, is the first big skill in DSA.

Example
You can edit this code.

Two pointers

Put one pointer at each end and move them towards each other. Many array problems drop from O(n²) to O(n) this way.

Example
You can edit this code.

Reverse in place

Example
You can edit this code.
Interviewers often ask you to solve without Python shortcuts like [::-1] or sorted(), to see that you understand the steps.
Key points
  • Index access is O(1); inserting in the middle is O(n).
  • Many problems can be solved in a single pass.
  • Prefix sums answer range questions quickly.

📝 Test yourself

1. What is the Big-O of reading items[i] from a list?
2. Two pointers on a sorted list: the sum is too small. What do you move?
3. Fill in the method that adds to the end of a list in O(1):
nums.(7)