Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A rooted tree has root 1 and children 1: [2, 3, 4], 3: [5], 4: [6, 7]. The number of nodes on the longest root-to-leaf path (including the root and the leaf) is what value?
- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
Height counted in nodes: the path 1-4-6 has three nodes, which is the longest. Counting edges along that path gives 2, one less than the node count asked for.
More Coding Interview Problems questions
- Problem: The string "abcdcba" is checked for palindromic substrings with an odd length of at least 5. How many such substrings are there?
- Problem: What is the number of distinct strings of length 4 over the alphabet {a, b} containing at least one 'a' and at least one 'b'?
- Problem: The string "aabbccddee" is compressed by keeping each character once for every run of length at least two, followed by the count…
- Problem: Calculate the number of ordered pairs (x, y) of integers with 1 ≤ x, y ≤ 20 for which x² + y² is divisible by 5.
- Problem: A lattice path goes from (0,0) to (3,3) using steps right (1,0) and up (0,1), and it must never go above the diagonal (that is…
- Problem: The array [1, 2, 4, 8, 16, 32] stores powers of two. For how many pairs (i < j) is a[i] + a[j] a power of two?