Identify the test — Pseudocode Aptitude Question
Identify the test: Characters are numbered from 1. What does Check(s) decide? Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 90 seconds
Question
Characters are numbered from 1. What does Check(s) decide?
Function Check(String s)
Integer i, j
Set i = 1
Set j = length(s)
While i < j
If s[i] != s[j] Then
Return FALSE
End If
Set i = i + 1
Set j = j - 1
End While
Return TRUE
End FunctionOptions
- Whether s contains a repeated character
- Whether s is a palindrome
- Whether s is in alphabetical order
- Whether every character of s is distinct
Show the answer and the worked solution
Correct answer: B. Whether s is a palindrome
Worked solution
- i walks forward from the first character while j walks back from the last, so each pass compares a character with its mirror image.
- Any mismatched pair returns FALSE at once; TRUE is returned only when every mirrored pair agrees.
- That is precisely the condition for the string to read the same forwards and backwards. Whether s is a palindrome is therefore exactly what the routine decides.
Answer: Whether s is a palindrome.
The idea
Two indices closing in from the ends is the signature of a symmetry test. Naming the pattern is faster than tracing it character by character.
Shortcut. Test tiny strings: Check("ab") is FALSE although its letters are distinct and in order, and Check("abca") is FALSE despite the repeated a, which leaves only the palindrome test.
Common trap. "Whether s contains a repeated character" tempts because the loop compares characters for equality, but it compares only mirror positions, so a repeat anywhere else is never noticed.