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?
- A. Adjacency matrix – O(1) edge lookup outweighs memory
- B. Adjacency lists – O(V + E) memory; an adjacency matrix would need V² = 10¹² cells
- C. Edge list only – BFS needs no neighbour lookup
- 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
- Problem: Given 'a/b = k' equations (e.g. a/b = 2, b/c = 3), answer queries like a/c. Which model and algorithm fit?
- Problem: Evaluate division queries on the ratio graph a/b = 2, b/c = 3, d/e = 4. python from collections import defaultdict g =…
- Problem: Reverse a word after prepending a marker using Java's StringBuilder. java StringBuilder sb = new StringBuilder("interview")…
- Problem: Find the longest substring in which every character appears at least k times (divide and conquer on characters that are too…
- Problem: Return the minimum number of characters to insert to make a string a palindrome. python def mininsert(s): t = s[::-1]; n = len(s)…
- Problem: Count the number of islands after each 'add land' operation on an initially all-water grid. python def islandsonline(R, C, ops)…