Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A rooted tree (root 1) has children lists: 1: [2, 3, 4], 2: [5, 6], 3: [], 4: [7], 5: [], 6: [8, 9], 7: [], 8: [], 9: []. How many nodes have an even subtree size (the node itself counts toward its subtree)?
- A. 1
- B. 2
- C. 0
- D. 3
Correct answer: A. 1
Compute each subtree size recursively, then count the even values. Counting only the leaves or using depth instead of subtree size gives a different answer.
More Coding Interview Problems questions
- Problem: A tree has nodes 1–6 with weighted edges 1-2 (3), 1-3 (5), 2-4 (4), 2-5 (2), 3-6 (7). What is the weighted diameter (maximum over…
- Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in…
- Problem: Insert the words "car", "cat", "care", "dog", "do", "dot" into an empty trie (one node per character, root has no letter). How…
- Problem: Count the occurrences of the pattern "aba" in "abababa", where occurrences may overlap.
- Problem: The words "listen", "silent", "enlist", "google", "gogole", "cat", "act", "tac", "dog" are grouped by anagram class (words with…
- Problem: In the binary string "1101001110100110", what is the length of the longest contiguous substring containing equal numbers of 0s…