ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function isPalindrome(String s)
    Integer i = 0, j = length(s) - 1
    while (i < j)
        if (s[i] != s[j])
            return false
        end if
        i = i + 1
        j = j - 1
    end while
    return true
End Function

Print isPalindrome("racecar"), isPalindrome("hello")
  1. A. false true
  2. B. true false
  3. C. true true
  4. D. false false

Correct answer: B. true false

The two-pointer check compares characters from both ends inward. 'racecar' mirrors perfectly at every step, so it returns true. 'hello' fails at the very first comparison ('h' != 'o'), returning false.

More Pseudocode and Code Tracing questions

Exams that ask this topic