ITPEC FE Subject B April 2024 Question 12
ITPEC FE Subject B April 2024 — Question 12 of 20
String Processing — checking whether a string is a palindrome using two-pointer traversal with minimum iterations.
The program uses two indices: i starts at 1 (left end), j starts at len (right end). Each iteration compares str[i] with str[j], then moves them inward (i ← i + 1, j ← j - 1).
The blank is the while loop condition — it must allow exactly ⌊len / 2⌋ iterations (the minimum needed to compare all symmetric pairs).
Since len ÷ 2 is integer division, the condition i < (len ÷ 2) + 1 gives exactly the right number of passes:
- For "ABBA" (len = 4): len ÷ 2 = 2, loop runs while i < 3 → iterations at i = 1, 2 ✓
- For "MADAM" (len = 5): len ÷ 2 = 2, loop runs while i < 3 → iterations at i = 1, 2 (middle character needs no check) ✓
Why not others:
- (a) `i < j - 1` — stops one step too early; skips the innermost pair (e.g., positions 2 and 3 in "ABBA")
- (c) `i < (len ÷ 2) - 1` — too few iterations; misses multiple character pairs
- (d) `i < len ÷ j` — j changes each iteration, making the condition unstable; for len = 4, the loop never executes (1 < 4 ÷ 4 = 1 is false)
- (e) `i < j ÷ 2` — also unstable; stops too early because j shrinks each iteration
Key rule: for palindrome checking with 1-based indexing, the loop needs exactly ⌊len / 2⌋ iterations — use a fixed boundary like (len ÷ 2) + 1, not a moving pointer.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.