Data Structures & AlgorithmsHardProduct Company SDE pattern
What is printed? (articulation points by brute force removal)
What is printed? (articulation points by brute force removal)
edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 5), (5, 3), (5, 6)]
V = 7
def comps(skip):
g = {v: [] for v in range(V) if v != skip}
for u, v in edges:
if skip not in (u, v): g[u].append(v); g[v].append(u)
seen, c = set(), 0
for s in g:
if s in seen: continue
c += 1; st = [s]; seen.add(s)
while st:
for v in g[st.pop()]:
if v not in seen: seen.add(v); st.append(v)
return c
print([v for v in range(V) if comps(v) > comps(-1)])- A. [1, 3]
- B. [1, 3, 5, 6]
- C. [1, 3, 5]
- D. [3, 5]
Correct answer: C. [1, 3, 5]
A vertex is an articulation point if removing it increases the number of components. Removing 1 separates triangle {0, 2}; removing 3 separates {0, 1, 2} from {4, 5, 6}; removing 5 isolates 6. Brute force is O(V(V + E)); Tarjan's low-link DFS finds them in O(V + E).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is printed? (strongly connected components count, Kosaraju) python g = {0: [1], 1: [2], 2: [0, 3], 3: [4], 4: [5], 5: [3], 6: [5, 7]…
- What is printed? (greedy fractional knapsack) python items = [(10, 60), (20, 100), (30, 120)] # (weight, value) cap, total = 50, 0.0 for…
- What is printed? (minimum platforms / overlapping intervals with a heap) python import heapq iv = sorted([(1, 4), (2, 3), (3, 6), (5, 7)…
- A complete binary tree is stored in an array with the root at index 1. A node at index 37 – what are its parent, left child and right…
- What is printed? (Catalan-style recursion with memo: number of ways to fully parenthesise a product of n matrices) python from functools…
- Union–find with union by rank and full path compression. What is printed? python p = list(range(8)); rank = [0] 8 def find(x): if p[x] !=…