Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: A rooted tree of 8 nodes (root 1) is given by children lists: 1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [7, 8], 6: [], 7: [], 8: []. What is the size of the subtree rooted at node 5 (node 5 counts itself)?
- A. 4
- B. 2
- C. 5
- D. 3
Correct answer: D. 3
Subtree size is 1 plus the sizes of the children's subtrees. Counting only the leaves under node 5 leaves out the node itself and the internal nodes.
More Coding Interview Problems questions
- Problem: The house-robber rule applies on a tree: you pick a set of nodes with no parent–child pair both chosen, maximising the total…
- Problem: The Z-function of a string s is defined as z[i] = the length of the longest common prefix of s and s[i:]; z[0] is conventionally…
- Problem: Count the distinct non-empty substrings of "abcabc" (two substrings are the same if their letters are identical, regardless of…
- Problem: How many windows of length 3 in the text "bacacbaab" are anagrams of the pattern "aab" (same letters with the same…
- Problem: What is the length of the longest substring of "eceebaabbccbdeeabe" that contains at most 2 distinct characters?
- Problem: Find the lexicographically smallest rotation of the string "dcbadcb". What is its starting index (0-based) in the original…