Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The rooted tree (root 0) has children 0: [1, 2], 1: [3, 4], 2: [5], 4: [6, 7], 5: [8]. Nodes are numbered in DFS pre-order with children visited in list order. What is the pre-order index (0-based) of node 6?
- A. 5
- B. 3
- C. 6
- D. 4
Correct answer: D. 4
Pre-order lists a node before its children, and the whole subtree of node 1 (including nodes 3 and 4) is finished before node 6 is reached. Counting only the siblings of node 6 misses those earlier descendants.
More Coding Interview Problems questions
- 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…
- 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.