ProgrammingVery hardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function J(Integer n, Integer k)
if (n == 1)
return 0
end if
return (J(n - 1, k) + k) mod n
End Function
Print J(7, 3) + 1- A. 4
- B. 7
- C. 3
- D. 6
Correct answer: A. 4
This is the Josephus recurrence: J(1) = 0, J(2) = 1, J(3) = 1, J(4) = 0, J(5) = 3, J(6) = 0, J(7) = 3 (0-based). Adding 1 gives the 1-based survivor: 4.
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? (All variables are integers, so / is integer division) Function gcd(Integer a…
- What will be the output of the following pseudocode? Function sd(Integer n) Integer s = 0 for (each d from 1 to n - 1) if (n mod d == 0) s…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) String digits…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code and char(k) the reverse) String s =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 10) for (each j from 1 to i) if (j j > i) break…