Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: A tree with 5 nodes is given by the parent array p = [−1, 0, 0, 1, 1] (node 0 is the root). How many leaves does it have?
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Leaves are nodes that are no node's parent: 2, 3 and 4, so there are three leaves.
More Coding Interview Problems questions
- Problem: The bit pattern 10110 is shifted left by 2 positions and then XOR-ed with the original pattern. What is the decimal value of the…
- Problem: Vertices 0–4 have undirected edges 0-1 (3), 1-2 (1), 2-3 (4), 3-4 (2), 0-4 (6), 1-4 (5). A path's cost is the sum of its edge…
- Problem: A directed graph has vertices 0–3 with arcs 0→1, 1→2, 2→1, 2→3. Using DFS from vertex 0 with children visited in increasing…
- Problem: Tree with root 1 and children 1: [2, 3], 2: [4, 5], 4: [6]. What is the number of nodes in the subtree of node 2 (node 2 counts…
- Problem: The string "abracadabra" contains the substring "abra" at several positions. How many occurrences of "abra" are there (overlaps…
- Problem: The string "abcdefghij" is split into blocks of length 4 from the left (the last block may be shorter). How many blocks have…