ProgrammingHardGoldman Sachs OA · Amazon OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
// circular list of 6 people: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> back to 1
Node p = node 1
while (p.next != p)
p.next = p.next.next // remove the person after p
p = p.next
end while
Print p.value- A. 1
- B. 5
- C. 3
- D. 6
Correct answer: B. 5
Every second person is removed: 2, 4, 6, then 3, then 1, leaving 5 (the Josephus survivor for n = 6, k = 2: 2 × (6 − 4) + 1 = 5).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the time complexity of the following pseudocode, as a function of n? Function f(Integer n) if (n <= 1) return 1 end if return f(n…
- What is the time complexity of the following pseudocode, as a function of n? Procedure g(Integer n) if (n <= 1) return for (each i from 1…
- What will be the output of the following pseudocode? Integer m[3][3] = {{11, 2, 4}, {4, 5, 6}, {10, 8, -12}} Integer d1 = 0, d2 = 0 for…
- What will be the output of the following pseudocode? (/ truncates toward zero and mod keeps the sign of the dividend (as in C)) Integer x…
- What will be the output of the following pseudocode? Boolean seen[0..6] = all false Integer c = 0 for (each i from 1 to 20) Integer r = (i…
- What will be the output of the following pseudocode? Function gcd(Integer a, Integer b) if (b == 0) return a return gcd(b, a mod b) End…