Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A social graph has V = 10⁶ users and E = 10⁷ friendships. You must store it to run BFS. Which representation is appropriate and why?

  1. A. Adjacency matrix – O(1) edge lookup outweighs memory
  2. B. Adjacency lists – O(V + E) memory; an adjacency matrix would need V² = 10¹² cells
  3. C. Edge list only – BFS needs no neighbour lookup
  4. D. Incidence matrix – O(V·E) memory is fine

Correct answer: B. Adjacency lists – O(V + E) memory; an adjacency matrix would need V² = 10¹² cells

Sparse graphs (E ≪ V²) belong in adjacency lists; BFS iterates each node's neighbours once, O(V + E). A matrix is 10¹² bits ≈ 125 GB even as a bitset. An edge list forces scanning all edges for each node.

More Coding Interview Problems questions

Exams that ask this topic