Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
A heap is stored in a 0-indexed array. What is the index of the parent of the element at index 10, and of its left child?
- A. Parent 5, left child 21
- B. Parent 4, left child 21
- C. Parent 4, left child 20
- D. Parent 5, left child 20
Correct answer: B. Parent 4, left child 21
For 0-based heaps, parent(i) = ⌊(i − 1)/2⌋ = 4 and left(i) = 2i + 1 = 21. Parent 5 and child 20 are the 1-based formulas (i/2 and 2i) applied to the wrong indexing.
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
- Using the master theorem, what is the solution of T(n) = 2T(n/2) + n?
- In the activity selection problem (choose the most non-overlapping intervals), which greedy rule always gives an optimal answer?
- Why does the greedy method (best value-per-weight first) solve the fractional knapsack problem but not the 0/1 knapsack problem?
- How many edges does a complete undirected simple graph on 10 vertices have?
- What is the time to search for a word of length L in a trie that holds n words?
- An undirected graph has 12 vertices and the sum of the degrees of all vertices is 40. How many edges does it have?