Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: The rooted tree (root 1) has children 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 6: [8]. How many nodes have a depth divisible by 3 (the root has depth 0)?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Carry the depth through a traversal and count nodes whose depth is a multiple of three, including the root at depth 0. Forgetting the root gives one fewer.
More Coding Interview Problems questions
- Problem: In the string "abcbaxyzzyx", count the palindromic substrings of length exactly 5.
- Problem: How many adjacent positions i (0 ≤ i < n−1) of "abbaabab" form the pair "ab" or the pair "ba"?
- Problem: How many indices i satisfy s[i] = s[i+2] in the string "abcabcab" (0 ≤ i ≤ n−3)?
- Problem: How many distinct strings are obtained from "abab" by cyclic rotations (including the rotation by 0)?
- Problem: What is the minimum number of character replacements needed to make "abcdba" a palindrome?
- Problem: In how many ways can 100 be made using coins 1, 5, 10, 25 (order does not matter)?