A palindromic alphabet pyramid prints each row as letters climbing from A to a peak, then descending back to A — so every full row reads the same forward and backward.
Remember
Rule: print A..peak, then (peak-1)..A (center once)
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA ← 5 rows
Unlike Program 1 (only the left half), the mirror starts at chr(ord($peak) - 1) so the middle letter is not doubled. Row length is always odd: 2r - 1.
Approach
How to Solve It
For each row, pick a peak letter, print up to it, then mirror back without reprinting the peak.
Method
Idea
Best for
Up then down
Two loops: A..peak then (peak-1)..A
Learning, interviews, left-aligned demos
Centered
Same letters plus leading spaces
Pyramid layout under the widest row
Pseudocode
Pseudocode
for $r from 0 to $n - 1:
$peak = chr(ord('A') + $r)
for $ch from 'A' to $peak:
echo $ch
for $ch from chr(ord($peak) - 1) down to 'A':
echo $ch
echo PHP_EOL
1. Pad first. Echo $n - $r - 1 spaces so shorter palindromes sit under the widest row.
2. Same letter logic. Up to peak, then down from chr(ord($peak) - 1) — unchanged from Examples 1 and 2.
3. Alignment only. Centering is layout; the palindrome rule does not change.
Edge Cases & Pitfalls
Check these before calling the solution done.
Down from peak
Doubled center
Starting the reverse loop at the peak prints ABCCBA instead of ABCBA. Always start at chr(ord($peak) - 1).
Only up
Not a palindrome
Skipping the descending half gives Program 1 style rows (A, AB, ABC…) — not this pattern.
PHP_EOL early
Broken row
Call echo PHP_EOL only after both halves. Inside either loop, each letter lands on its own line.
$n = 1
Single A
Output is just A; the mirror loop does not run — a good sanity check.
Past Z
Clamp to 26
More than 26 rows walks the peak past Z. Cap or reject the input.
Bad parse
Use is_numeric
Casting non-numeric input to int becomes 0. Prefer is_numeric so empty or letter input does not silently fail.
Analysis
Time and Space Complexity
Program
Time
Extra space
Up/down / centered forms
O(n²)
O(1)
Row r (1-based) prints 2r - 1 letters. Over n rows the total is 1 + 3 + … + (2n - 1) = n².
Remember
Key Takeaways
Two halves: climb A..peak, then descend (peak-1)..A.
Center once: never start the reverse loop at the peak itself.
Odd lengths: each row has 2r - 1 letters; totals sum to n².
Complexity:O(n²) time; O(1) extra space.
One line: for each peak, echo A..peak then (peak-1)..A so the row is a palindrome.
Frequently Asked Questions
The peak letter was already printed by the first loop. Starting the mirror at peak-1 avoids printing the center letter twice.
Print A..peak, then print (peak-1)..A so the center letter is not duplicated.
Row r prints 2r-1 letters. Over n rows the total is 1+3+...+(2n-1)=n².
Yes. Print each letter followed by a space in both halves, and update the sample output accordingly.
echo stays on the same line for each letter. echo PHP_EOL ends the row after both halves finish.
Program 1 prints only the left half (A, AB, ABC…). This pattern mirrors back down so each full row is a palindrome.
O(n²) for n rows because each row prints O(n) characters and the sum of odd lengths is n².
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int, require n ≥ 1, and cap at 26 so peaks stay within A–Z.
🤔
Did you know?
Each row prints A up to the row peak, then prints back down starting from chr(ord($peak) - 1) so the middle letter appears only once. Row length is 2r - 1 for row r, so the total characters over n rows is n².