Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A rooted tree (root 1) has children lists: 1: [2, 3], 2: [4, 5, 6], 3: [7], 4: [], 5: [8], 6: [], 7: [9], 8: [], 9: []. How many leaves lie at an even depth (the root has depth 0)?
- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
Walk the tree tracking depth and count leaves whose depth is even. Counting internal nodes at even depth is a different quantity.
More Coding Interview Problems questions
- Problem: For the same rooted tree as the previous question (root 1: children 2, 3; 2: children 4, 5, 6; 3: child 7; 5: child 8; 7: child…
- Problem: In a binary tree with 7 nodes in level order [10, 5, 15, 2, 7, 12, 20] (missing children are shown as absent, so the array…
- Problem: Three sorted rows are [1, 5, 9, 14], [2, 6, 10, 15] and [3, 7, 11, 16]. Merging all rows into one sorted sequence, what is the…
- Problem: What is the length of the longest substring of "abcabcbb" with all characters distinct?
- Problem: The shortest palindrome obtainable by inserting characters only at the front of "abacd" has what length?
- Problem: How many distinct palindromic substrings does "abacaba" contain (single characters count as palindromes, and distinct strings…