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 calls = 0
Procedure msort(Integer l, Integer r)
    calls = calls + 1
    if (l >= r)
        return
    end if
    Integer m = (l + r) / 2
    msort(l, m)
    msort(m + 1, r)
    // merge step (does not call msort)
End Procedure
msort(0, 5)
Print calls
  1. A. 6
  2. B. 12
  3. C. 11
  4. D. 16

Correct answer: C. 11

The call tree splits 6 elements until single elements remain: it has 6 leaves (one per element) and 5 internal calls, so 2 × 6 − 1 = 11 calls in all.

More Pseudocode and Code Tracing questions

Exams that ask this topic