Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For row i (1..n): append `n-i` spaces, then `2i-1` stars, then a newline between rows.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can derive per-row counts from the row index.
Where candidates slip
Wrong space/star arithmetic producing an off-centre shape.
How to stand out
State the formulas (n−i spaces, 2i−1 stars) and why stars are odd.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Spaces + stars per row
1Hint 1Explore hintHide hint
Each row has spaces THEN stars — how many of each?
2Hint 2Explore hintHide hint
Spaces shrink as stars grow: derive both from the row index.
3Hint 3Explore hintHide hint
Stars per row are odd (2i−1) to keep the pyramid centred.
A · Core idea
A centred pyramid needs two inner loops per row: one for the leading spaces that push the row toward the centre, and one for the stars. The arithmetic linking the row index to the counts is the lesson.
Deriving the counts. For row i (1..n): leading spaces = n − i (fewer as you go down), and stars = 2i − 1 (the odd sequence 1, 3, 5, …) so the pyramid is symmetric about the centre. Plugging i into these formulas is the whole exercise.
Why 2i − 1 stars. An odd count guarantees a single centre column, giving the clean triangular silhouette; even counts wouldn't centre.
Newline discipline. As with all patterns, newlines go between rows.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3i=1
2 spaces, 1 star
" *"" *\n ***\n*****"C · Code with teaching comments
D · Why it works and complexityO(n²) timeO(n²) spaceOpen the invariant and proofExplore the proofHide the proof
The arithmetic linking row index to space/star counts is what this pattern teaches.
ComplexityO(n²) time, O(n²) space — row i emits (n−i) spaces and (2i−1) stars; summed over rows it's O(n²).
E · Edge cases and pitfalls4n = 1 — "*".Explore edge casesHide edge cases
Edge cases handled
n = 1"*".n = 2" *\n***".
Pitfalls
- Wrong space/star formula (off-centre or wrong widths).
- Forgetting the spaces entirely.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String pyramid(int n) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { for (int s = 0; s < n - i; s++) sb.append(' '); for (int j = 0; j < 2 * i - 1; j++) sb.append('*');Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Wrong space/star formula (off-centre or wrong widths).
- Forgetting the spaces entirely.
Revise
Carry the pattern into the interview
Pattern to remember
Indexed spaces + stars
Code pattern to memorize
row i: (n-i) spaces + (2i-1) starsRules to remember
- 1Spaces = n − i.
- 2Stars = 2i − 1.
- 3Two inner loops.
Use this pattern when
- Centred / index-derived patterns
Interview takeaway
Centred patterns derive counts from the row index.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Indexed spaces + stars. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups