Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: In a graph represented with an adjacency list, what is the space complexity, and how does it compare to an adjacency matrix for a sparse graph (E << V^2)?

  1. A. Adjacency list: O(V^2), same as the matrix
  2. B. Adjacency list: O(V + E), much better than the adjacency matrix's O(V^2) for sparse graphs
  3. C. Adjacency matrix: O(V + E), better than the list
  4. D. Both always use O(V + E)

Correct answer: B. Adjacency list: O(V + E), much better than the adjacency matrix's O(V^2) for sparse graphs

An adjacency list stores only the edges that actually exist (plus one entry per vertex), giving O(V+E); an adjacency matrix always allocates V^2 cells regardless of how many edges exist, wasting space when the graph is sparse.

More Coding Interview Problems questions

Exams that ask this topic