Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A binary tree is given by a parent array where node i has parent p[i] (p[0] = −1 is the root): p = [−1, 0, 0, 1, 1, 2, 2, 5]. How many leaves does the tree have (a leaf has no children)?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
A leaf is a node that never appears as anyone's parent. Counting nodes whose parent index is larger, or nodes with the parent index 0, misidentifies leaves.
More Coding Interview Problems questions
- Problem: What is the minimum number of swaps of two neighbouring characters needed to turn "abcdef" into "fedcba"?
- Problem: Find the number of distinct strings obtained by reading the grid of letters ab cd moving right or down from the top-left, where…
- Problem: The string "ababa" has how many distinct substrings of length 2 and 3 combined?
- Problem: Count the palindromic substrings of "abcbaabc" that start at index 0 (each prefix that is a palindrome counts once).
- Problem: Maximum sum of a subarray with length exactly 3 in [4, −2, 5, 1, 0, 2, −3, 6]. Report the maximum window sum.
- Problem: The longest subarray with sum divisible by 4 in [5, 3, 7, 2, 2, 6, 1, 1]: what is its length?