ProgrammingHardAmazon OA · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = "COMPUTER"
Integer i = 0, j = length(s) - 1
while (i < j)
    while (i < j AND s[i] is not a vowel) i = i + 1
    while (i < j AND s[j] is not a vowel) j = j - 1
    swap(s[i], s[j])
    i = i + 1
    j = j - 1
end while
Print s
  1. A. RETUPMOC
  2. B. CEMPUTOR
  3. C. COMPUTER
  4. D. CUMPOTER

Correct answer: B. CEMPUTOR

Two pointers swap the outermost vowels inward: O and E swap, then i and j meet at U, which stays. The vowels O, U, E become E, U, O: CEMPUTOR.

More Pseudocode and Code Tracing questions

Exams that ask this topic