Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Outer loop n rows; inner loop appends n stars each; newline between rows.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you keep the inner bound constant for a rectangle/square.
Where candidates slip
Accidentally indexing the inner bound by the row.
How to stand out
Contrast with the triangle to show you understand the bound's role.
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
How does the inner bound differ from the triangle's?
2Hint 2Explore hintHide hint
Both loops run n times — a constant inner bound.
3Hint 3Explore hintHide hint
Newlines between rows only.
A · Core idea
The simplest nested pattern: both dimensions are fixed at n, so every one of the n rows has exactly n stars.
Why a constant inner bound. Unlike the triangle (inner bound = row index), a square's inner loop always runs n times. Getting this constant bound right — not accidentally making it triangular — is the whole exercise.
Why O(n²). n rows × n columns = n² characters.
Newline discipline. Separate rows with a newline placed between them, not trailing the last row.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 2row 1
2 stars
**"**\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 — n rows each emitting n characters → n² total.
E · Edge cases and pitfalls4n = 1 — "*".Explore edge casesHide edge cases
Edge cases handled
n = 1"*".n = 33×3 block.
Pitfalls
- Using a triangular inner bound by accident.
- Trailing newline.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String square(int n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) sb.append('*'); if (i < n - 1) sb.append('\n');Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Using a triangular inner bound by accident.
- Trailing newline.
Revise
Carry the pattern into the interview
Pattern to remember
Fixed nested loop
Code pattern to memorize
for i in 1..n: print('*' * n)Rules to remember
- 1n rows × n cols.
- 2Constant inner bound.
- 3Newlines between rows.
Use this pattern when
- Rectangular patterns
Interview takeaway
A square is the nested loop with both bounds equal to n.
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: Fixed nested loop. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups