A reverse alphabet with diagonal star prints each row as letters from the top letter down to A, but replaces exactly one cell with * where the row key equals the column letter ($i == $j).
Remember
Rule: print j from top..A; when i == j print '*' instead
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA ← top = E (star slides right → left)
Outer $i walks A..E (which letter becomes the star). Inner $j walks E..A (what would print in each column). The match slides the star one column left each row.
Approach
How to Solve It
Nested char loops plus one equality test — print a star on the diagonal, the column letter everywhere else.
Method
Idea
Best for
Reverse columns
Inner $j from top..A; star slides right → left
Main pattern, interviews
Forward columns
Inner $j from A..top; star slides left → right
Comparing diagonal directions
Pseudocode
Pseudocode
for $i from 'A' to $top:
for $j from $top down to 'A':
if $i == $j:
echo '*'
else:
echo $j
echo PHP_EOL
Cheat sheet
Goal
Pattern
Row key (which letter is *)
for ($i = 'A'; $i <= $top; $i++)
Reverse columns
for ($j = $top; $j >= 'A'; $j--)
Diagonal test
echo ($i == $j) ? '*' : $j;
Forward columns
for ($j = 'A'; $j <= $top; $j++)
End the row
echo PHP_EOL;
A–Z-safe top
Require a single letter A–Z
Printing Letters vs Starting a New Line
API
Effect
Use for
echo
Stays on the same line
Each cell (* or letter)
echo PHP_EOL
Ends the current line
After the inner column loop
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the reverse diagonal-star grid updates instantly — including size and star path.
Enter one letter A–Z. Grid size is n × n where n = top - 'A' + 1.
Live resulttop E · 5×5 · star right→left
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
Trace
Worked Walkthrough — $top = 'E'
Trace each row key $i, where $i == $j fires, and the printed line.
$i
Columns $j
Star at
Printed row
A
E D C B A
rightmost (A)
EDCB*
B
E D C B A
column B
EDC*A
C
E D C B A
column C
ED*BA
D
E D C B A
column D
E*CBA
E
E D C B A
leftmost (E)
*DCBA
Exactly one * per row. As $i climbs A→E, the match moves left through the reverse letter scan.
Code
PHP Programs
Three complete programs: fixed top E, top-letter input, and a forward-column contrast. Use View Output to reveal sample results.
1. Same diagonal test.$i == $j still marks exactly one cell per row.
2. Column order flips. Printing A..E instead of E..A reverses letter order and sends the star left → right.
3. Compare with Example 1. Inner-loop direction controls both the letter sequence and which way the star travels.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong compare
No diagonal
Comparing row index to column index (ints) instead of $i == $j (letters) will not mark the letter diagonal correctly.
Always echo $j
Missing star
Forgetting the $i == $j branch prints a plain reverse (or forward) letter square with no asterisk.
PHP_EOL early
Broken row
Call echo PHP_EOL only after the inner loop. Inside it, each cell lands on its own line.
$top = 'A'
Single *
Output is just * on one line — a good sanity check.
Lowercase
Unexpected range
Normalize input with strtoupper so e behaves like E.
Empty input
Offset crash
$raw[0] fails on empty input. Validate length before taking the character.
Analysis
Time and Space Complexity
Program
Time
Extra space
Reverse / forward forms
O(n²)
O(1)
For top letter with offset n = ord($top) - ord('A') + 1, the nested loops print an n × n grid — quadratic in n.
Remember
Key Takeaways
Diagonal rule: echo * when $i == $j; otherwise echo the column letter.
Reverse scan: inner $j from top..A sends the star right → left.
Direction flip: A..top columns reverse the letter order and the star path.
Complexity:O(n²) time for an n × n grid; O(1) extra space.
One line: for each row key $i, echo reverse letters and replace the cell where $i == $j with *.
Frequently Asked Questions
i walks A..E down the rows while j scans E..A across columns. The condition i == j marks one diagonal position per row, which gets replaced by '*'.
Because i increases each row, but the printed letters go from E down to A across the row. The match i==j occurs at a different column each time, sliding the star left.
The star prints when i == j, so each row replaces exactly one character at the matching column.
Yes. Replace '*' with any symbol (like '#' or '@') in the conditional branch.
echo stays on the same line for each cell. echo PHP_EOL ends the row after the inner loop finishes.
You print forward letters instead of E..A, and the star slides the other way (left to right on the main diagonal).
O(n²) for an n×n letter grid because every row prints n characters.
Use trim(fgets(STDIN)), take the first character, require A–Z (or a–z), and reject empty input. Cap at Z if you only want alphabetic ranges.
🤔
Did you know?
The diagonal is defined by i == j while the row prints letters from E down to A. Since i increases A..E each row, the star moves one position left each line: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.