Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
A complete binary tree is stored in an array with the root at index 1. A node at index 37 – what are its parent, left child and right child indices, and at which depth (root = 0) is it?
- A. Parent 18, children 75 and 76, depth 5
- B. Parent 19, children 74 and 75, depth 6
- C. Parent 18, children 74 and 75, depth 5
- D. Parent 18, children 74 and 75, depth 6
Correct answer: C. Parent 18, children 74 and 75, depth 5
With 1-based indexing, parent = ⌊i/2⌋ = 18, children 2i = 74 and 2i + 1 = 75, and depth = ⌊log₂ i⌋ = 5 (indices 32–63 are on depth 5). With 0-based indexing the formulas shift to (i − 1)/2, 2i + 1 and 2i + 2 – mixing the two conventions is the usual error.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is printed? (Catalan-style recursion with memo: number of ways to fully parenthesise a product of n matrices) python from functools…
- Union–find with union by rank and full path compression. What is printed? python p = list(range(8)); rank = [0] 8 def find(x): if p[x] !=…
- 0-1 BFS on a grid: stepping into '#' costs 1 (a wall to break), stepping onto '.' or 'T' costs 0. What is the minimum number of walls to…
- How many topological orders does this DAG on 6 vertices have? (counted by DP over bitmasks of already-placed vertices) python from…
- Fenwick (binary indexed) tree, 1-based, built by point additions; then a[3] += 10. What is printed (the internal array, a range sum and a…
- Segment tree for range-minimum with lazy range-add (the pending add stays at a node and is applied on the way up instead of being pushed…