Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Outer loop over rows 1..n; inner loop appends `i` stars; add a newline between rows.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you can structure nested loops with an index-dependent inner bound.
Where candidates slip
Off-by-one star count, or trailing newline.
How to stand out
State the row/column mental model for all pattern problems.
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
Nested loop
1Hint 1Explore hintHide hint
A 2-D shape needs two loops — what does each control?
2Hint 2Explore hintHide hint
Row i should have how many stars?
3Hint 3Explore hintHide hint
Place newlines between rows, not after the last.
A · Core idea
The introduction to nested loops: the outer loop selects the row, the inner loop draws that row's characters. Here row i contains i stars.
Why nested loops. A 2-D shape needs two indices — one for rows, one for columns. The inner loop's bound depends on the outer index (i stars on row i), which is what makes it a triangle rather than a rectangle.
Why O(n²). Row i does i appends, so total work is 1+2+…+n = n(n+1)/2 = O(n²) — inherent to drawing the shape.
Newline discipline. Put the newline between rows (e.g. if (i < n)), not after the last, to avoid a trailing blank line.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3row 1
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
ComplexityO(n²) time, O(n²) space — row i emits i characters, so total output is 1+2+…+n = O(n²).
E · Edge cases and pitfalls4n = 1 — "*".Explore edge casesHide edge cases
Edge cases handled
n = 1"*".n = 4Four rows, 1..4 stars.
Pitfalls
- Inner loop bound wrong (i+1 or i-1 stars).
- Trailing newline after the last row.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String triangle(int n) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) sb.append('*'); if (i < n) sb.append('\n');Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Inner loop bound wrong (i+1 or i-1 stars).
- Trailing newline after the last row.
Revise
Carry the pattern into the interview
Pattern to remember
Nested loop (rows × cols)
Code pattern to memorize
for i in 1..n: print('*' * i)Rules to remember
- 1Outer = rows.
- 2Inner = count per row.
- 3Newlines between rows.
Use this pattern when
- Any row/column pattern
Interview takeaway
Patterns = outer loop for rows, inner loop for columns.
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: Nested loop (rows × cols). Master it on small inputs and it transfers to harder problems.