ProgrammingModerateAmazon OA · TCS NQT

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
// list: 10 -> 20 -> 30 -> 40 -> 50 -> NULL
Node fast = head, slow = head
Integer k = 2
for (each i from 1 to k) fast = fast.next
while (fast != NULL)
    fast = fast.next
    slow = slow.next
end while
Print slow.value
  1. A. 40
  2. B. 30
  3. C. 20
  4. D. 50

Correct answer: A. 40

fast starts k = 2 nodes ahead, so when it falls off the end, slow is 2 nodes from the end: 40.

More Pseudocode and Code Tracing questions

Exams that ask this topic