ProgrammingHardAmazon OA · TCS Digital

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer a[8] = {4, 9, 1, 7, 3, 8, 2, 6}, comps = 0
Function mx(Integer l, Integer r)
    if (l == r)
        return a[l]
    end if
    Integer m = (l + r) / 2
    Integer x = mx(l, m), y = mx(m + 1, r)
    comps = comps + 1
    if (x > y) return x else return y
End Function
Print mx(0, 7), comps
  1. A. 9 8
  2. B. 9 3
  3. C. 9 7
  4. D. 9 14

Correct answer: C. 9 7

Divide and conquer still needs one comparison per merge, and a tree with 8 leaves has 7 merges: 9 7. Divide and conquer is no cheaper than a plain left-to-right scan, which also needs 7 comparisons.

More Pseudocode and Code Tracing questions

Exams that ask this topic