Data Structures & AlgorithmsModerateTCS NQT · graph basics
Problem: Which graph algorithm checks whether a directed graph is a DAG in O(V + E)?
- A. Prim's algorithm
- B. Kahn's algorithm (or DFS with three colours)
- C. Bellman-Ford
- D. Floyd-Warshall
Correct answer: B. Kahn's algorithm (or DFS with three colours)
A DAG is exactly a graph where Kahn's algorithm processes all vertices or a DFS finds no back edge.
More Coding Interview Problems questions
- Problem: What is the expected number of comparisons for a binary search on a sorted array of n = 1000 elements (successful search, worst…
- Problem: What is the time complexity of finding the articulation points and bridges together with one DFS?
- Problem: How many pairs (i < j) of integers in [1, 30] have i + j divisible by 6? Run the code. python print(sum(1 for i in range(1, 31)…
- Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code. python print(sum(1 for x in range(1, 10001) if x…
- Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code. python n = 1000 c = 0 p = 2 while p <= n: c += n // p p…
- Problem: How many distinct values does (i j) mod 7 take for 1 <= i, j <= 7? Run the code. python print(len({(i j) % 7 for i in range(1, 8)…