AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumroutine purposestrings

~90s

Identify the test

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 Function
Show the answer and the worked solution

Correct answer: B.

Whether s is a palindrome

Worked solution

  1. i walks forward from the first character while j walks back from the last, so each pass compares a character with its mirror image.
  2. Any mismatched pair returns FALSE at once; TRUE is returned only when every mirrored pair agrees.
  3. 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.

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.

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 Function

Options

  1. Whether s contains a repeated character
  2. Whether s is a palindrome
  3. Whether s is in alphabetical order
  4. Whether every character of s is distinct
Show the answer and the worked solution

Correct answer: B. Whether s is a palindrome

Worked solution

  1. i walks forward from the first character while j walks back from the last, so each pass compares a character with its mirror image.
  2. Any mismatched pair returns FALSE at once; TRUE is returned only when every mirrored pair agrees.
  3. 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.

All Pseudocode questions · placement mock tests