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- A. 9 8
- B. 9 3
- C. 9 7
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer c = 0 for (each a from 1 to 20) for (each b from a to 20) for (each h from b…
- What will be the output of the following pseudocode? String s = "Tcs NQT 2024 Exam!" Integer v = 0, c = 0, d = 0, o = 0 for (each ch in s)…
- What will be the output of the following pseudocode? String s = "AAAAA", r = "" Integer i = 0 while (i < length(s)) if (i + 1 < length(s)…
- What will be the output of the following pseudocode? (code(c) gives the ASCII code: 'A' = 65, 'a' = 97, '0' = 48; ^ is XOR) Integer x =…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) String s = "THE QUICK BROWN FOX" Integer…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; abs gives the absolute value) Integer a[8] = {4, 3, 2, 7…