ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode? (Arrays are indexed from 0)
What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer nxt[8] = {1, 2, 3, 4, 5, 6, 7, 3}
Integer slow = 0, fast = 0, steps = 0
do
slow = nxt[slow]
fast = nxt[nxt[fast]]
steps = steps + 1
while (slow != fast)
Print steps, slow- A. 3 3
- B. 5 5
- C. 4 6
- D. 6 3
Correct answer: B. 5 5
The path is 0 → 1 → 2 → 3 and then the cycle 3 → 4 → 5 → 6 → 7 → 3 of length 5. Tracking slow/fast: (1, 2), (2, 4), (3, 6), (4, 3), (5, 5). They meet after 5 steps at node 5.
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? (mod is applied to (i j) before adding) Integer x = 0 for (each i from 1 to 5) for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer A[2][3] = {{1, 2, 3}, {4, 5, 6}} Integer Bm[3][2]…
- 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)…
- 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…