A diamond-shaped alphabet pattern prints only the outline of a diamond: tip A at the top, letters that open to a wide waist, then close back to tip A.
Remember
Rule: upper $i = 0..$n, then lower $i = $n-1..0
On each row, print $alpha[$col] where $i == $col
A
B B
C C
D D
E E
D D
C C
B B
A ← A–E (9 rows, width 9)
Build it by stacking Program 33 (upper inverted V) and mirroring back from $n - 1 so the waist prints once. Geometry matches the hollow diamond in Star Pattern 9, with letters instead of *.
Approach
How to Solve It
Print one hollow alphabet row with two inner loops, then call that idea twice — ascending, then descending past the waist.
Method
Idea
Best for
Dual outer loops
Upper 0..$n, lower $n-1..0, same $j/$k bodies
Learning, interviews, clearest stack of P33
Helper + print_row
One print_row($i) called from both halves
Less duplication once the geometry clicks
Pseudocode
Pseudocode
$n = ord($end) - ord('A')
printHollowRow($i, $n):
for $j from $n down to 0:
echo $alpha[$j] if $i == $j else " "
for $k from 1 to $n:
echo $alpha[$k] if $i == $k else " "
echo PHP_EOL
for $i from 0 to $n: // upper half
printHollowRow($i, $n)
for $i from $n - 1 down to 0: // lower half (skip waist)
printHollowRow($i, $n)
1. One row recipe.print_row owns the $j/$k diagonal logic and the row break.
2. Call it twice. Upper half walks $i up; lower half walks $i down from $n - 1.
3. Same shape, less copy-paste. Learn the expanded loops first (Example 1), then refactor when the geometry feels familiar.
Edge Cases & Pitfalls
Check these before calling the solution done.
Lower starts at $n
Double waist
If the second outer loop starts at $i = $n, the widest line prints twice. Use $n - 1.
$k = 0
Duplicate tip A
Right loop must start at $k = 1. Starting at 0 prints two As on the tip rows.
PHP_EOL inside
Broken outline
If echo PHP_EOL sits inside $j or $k, the outline collapses into a column. Call it only after both inner loops.
end = A
Single tip
Upper prints one line; lower never runs. Output is just A — a good sanity check.
Proportional font
Looks skewed in the IDE
Spaces and letters need a monospace font. Proportional fonts make diagonals look uneven.
Bad input
Validate one letter
Empty lines and multi-character input break naive $line[0] indexing — require a single A–Z character.
Analysis
Time and Space Complexity
Program
Time
Extra space
Dual loops (Examples 1–2)
O(n²)
O(1) beyond the alphabet string
Helper function (Example 3)
O(n²)
O(1) beyond the alphabet string
Lines printed = 2n + 1. Each line walks about 2n + 1 positions across the two inner loops, so work is still quadratic in n. Outline letters grow as 4n for n ≥ 1 (and 1 when n = 0).
Remember
Key Takeaways
Compose halves: upper 0..$n then lower $n-1..0 — Program 33 with one seam fix.
Diagonal rule: letter only when $i == $j or $i == $k; everything else is a space.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space beyond the alphabet string.
One line: print hollow alphabet rows for $i = 0..$n, then again for $i = $n-1..0, starring only the diagonals.
Frequently Asked Questions
The first outer loop grows i from 0 through n (A through the end letter) using the inverted-V inner loops. The second outer loop shrinks i from n-1 through 0 with the same inner loops, mirroring the top without repeating the widest row.
Because the widest row is already printed at the end of the first part. Starting the second part at n again would duplicate that waist line.
Yes. Program 34 is Program 33 printed for A..end and then mirrored back for (end-1)..A using the same inner loops.
echo stays on the same line. echo PHP_EOL ends the current line. Letters and spaces use echo; the row break uses echo PHP_EOL after both inner loops.
For n = ord(end) - ord('A'), total rows are (n+1) + n = 2n+1. For A..E, n is 4 and you get 9 rows.
Width is also 2n+1: n+1 columns from the left block and n columns from the right block.
O(n²) because there are Theta(n) rows and each row scans Theta(n) positions across both blocks.
Use trim(fgets(STDIN)), require a single A–Z character after strtoupper, and reject empty or multi-character input.
🤔
Did you know?
Two phases with identical inner loops. Phase 1 prints A..E using two diagonal scans (left: E..A, right: B..E). Phase 2 prints D..A to mirror the top without repeating the widest E row. For $n = ord($end) - ord('A'), total rows and width are both 2n + 1.