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)?
- A. Adjacency list: O(V^2), same as the matrix
- B. Adjacency list: O(V + E), much better than the adjacency matrix's O(V^2) for sparse graphs
- C. Adjacency matrix: O(V + E), better than the list
- 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
- Problem: Which best explains why the greedy 'interval scheduling to maximize the number of non-overlapping intervals' problem is solved…
- Problem: Which is the correct worst-case time complexity for inserting into a balanced BST (like a Red-Black or AVL tree) with n nodes…
- Problem: What is the time complexity of the standard recursive merge sort, including the merge step, expressed via its recurrence T(n) =…
- Problem: Which statement about the time complexity of checking if two strings are anagrams is most efficient and correct?
- Problem: For the 'minimum number of platforms needed for a railway station given arrival and departure times' problem, what is the…
- Problem: Which correctly describes why a hash set gives O(1) average-case membership testing, but a balanced BST set gives O(log n)?