Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: An undirected graph on 9 vertices (0–8) has edges 0-1, 1-2, 3-4, 4-5, 5-3, 6-7. Vertex 8 is isolated. Count the unordered pairs of distinct vertices that are NOT connected to each other.
- A. 30
- B. 21
- C. 7
- D. 29
Correct answer: D. 29
Components have sizes 3, 3, 2 and 1, so connected pairs are 3 + 3 + 1 = 7 and unconnected pairs are C(9,2) − 7 = 29. Counting non-adjacent pairs (36 − 6 edges) gives 30, forgetting isolated vertex 8 gives 28 − 7 = 21, and 7 counts the connected pairs.
More Coding Interview Problems questions
- Problem: A rooted tree (root 1) has children lists: 1: [2, 3, 4], 2: [5, 6], 3: [], 4: [7], 5: [], 6: [8, 9], 7: [], 8: [], 9: []. How…
- Problem: A tree has nodes 1–6 with weighted edges 1-2 (3), 1-3 (5), 2-4 (4), 2-5 (2), 3-6 (7). What is the weighted diameter (maximum over…
- Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in…
- Problem: Insert the words "car", "cat", "care", "dog", "do", "dot" into an empty trie (one node per character, root has no letter). How…
- Problem: Count the occurrences of the pattern "aba" in "abababa", where occurrences may overlap.
- Problem: The words "listen", "silent", "enlist", "google", "gogole", "cat", "act", "tac", "dog" are grouped by anagram class (words with…