Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: On a rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5, 6], 3: [7], 5: [8, 9]. How many nodes have exactly 2 descendants (descendants exclude the node itself)?
- A. 1
- B. 2
- C. 0
- D. 3
Correct answer: A. 1
Count descendants recursively, excluding the node itself. Counting the subtree size (which includes the node) shifts the target by one.
More Coding Interview Problems questions
- Problem: A binary tree is encoded in preorder with '#' for a missing child: "1,2,#,#,3,4,#,#,5,#,#". How many leaves (nodes with two…
- Problem: Count the palindromic substrings of length exactly 4 in the string "aabbaabbaa".
- Problem: How many distinct two-letter substrings does "mississippi" contain?
- Problem: The smallest period of a string s is the least p ≥ 1 such that s[i] = s[i+p] for all valid i. What is the smallest period of…
- Problem: What is the length of the longest proper border (a proper prefix that is also a suffix) of "abacabacaba"? A proper border is…
- Problem: How many runs (maximal blocks of equal consecutive characters) does "aabbbaaaacc" contain?