Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: In the directed graph on vertices 1–6 with arcs 1→2, 1→3, 2→4, 3→4, 4→5, 3→6, 6→5, what is the length of the longest directed path (number of arcs)?
- A. 4
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
Longest paths in a DAG come from memoised DP over out-neighbours. Using a BFS from the source gives the shortest-path length rather than the longest.
More Coding Interview Problems questions
- Problem: A binary tree is given in preorder (root first, then left subtree, then right subtree) as 8, 3, 1, 6, 4, 7, 10, 14, 13 and…
- Problem: For the binary tree with root value 2 and children: 2 has left child 3 and right child 9; 3 has left child 1 and right child 5; 9…
- Problem: The string "banana" has its longest repeated substring (a substring occurring at least twice, overlaps allowed) of length L. What…
- Problem: How many subsequences equal to the string "abc" are contained in "aabbcbcac" (a subsequence picks positions in increasing order…
- Problem: The string "aabcccccaaa" is run-length encoded (each run becomes its letter followed by its run length, e.g. "a2b1..."). What is…
- Problem: Find the length of the shortest substring of "xaybzcxyabc" that contains all the letters a, b and c at least once.