Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: Given the parent array p = [−1, 0, 0, 1, 1, 2, 4, 4] for nodes 0–7 (node 0 is the root), how many nodes have exactly one child?
- A. 2
- B. 0
- C. 3
- D. 1
Correct answer: D. 1
Count each node's children from the parent array, then tally those with exactly one. Counting leaves (zero children) is a different quantity.
More Coding Interview Problems questions
- Problem: A binary tree in level order (None for absent nodes) is [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]. How many root-to-leaf…
- Problem: Count the odd-length palindromic substrings of "racecarxyz" that are centred at index 3 (the 'e' of "racecar"), where the centre…
- Problem: What is the lexicographically next permutation of the string "bcdea" (the next arrangement in dictionary order using the letters…
- Problem: How many substrings of "abcabc" contain all three letters a, b and c at least once?
- Problem: In how many ways can a 1 × 7 strip be tiled completely with tiles of length 1, 2 and 3 (ordered arrangements of tiles count…
- Problem: A rope of integer length 10 is cut into integer pieces (at least two pieces), and the product of the piece lengths is maximised…