Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE
Problem: A tree has 8 nodes 0–7 and edges: 0–1, 1–2, 1–3, 3–4, 4–5, 3–6, 6–7. What is the diameter of the tree, measured as the number of edges on the longest simple path?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Two BFS passes: the farthest node from any start is an endpoint of a diameter, and the second BFS from it gives the length. Paths 0-1-3-4-5 and 2-1-3-6-7 both have 4 edges, the longest possible here.
More Coding Interview Problems questions
- Problem: A directed graph has vertices 0–6 and arcs: 0→1, 1→2, 2→0, 2→3, 3→4, 4→5, 5→3, 5→6, 6→6. A vertex lies on a cycle if it can reach…
- Problem: Count how many of these four undirected graphs are bipartite (2-colourable so that every edge joins different colours): G1: edges…
- Problem: Find the length of the longest palindromic substring of the string "abacdfgdcabazyx".
- Problem: Using insert, delete and replace, each costing 1, what is the minimum number of operations to convert "dragon" into "lantern"?
- Problem: How many palindromic substrings does "aabbaa" contain? Two substrings count as different if they occupy different index ranges…
- Problem: Compute the KMP prefix function (the longest proper prefix that is also a suffix, for each prefix) of the pattern "abacabab"…