Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Outer row 1..n; inner loop appends j for j = 1..i; newline between rows.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reuse the nested-loop skeleton with a different cell value.
Where candidates slip
Emitting the row index instead of the column index.
How to stand out
Articulate that pattern problems share one skeleton.
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 numeric loop
1Hint 1Explore hintHide hint
Start from the star triangle — what single thing changes?
2Hint 2Explore hintHide hint
Print the inner loop variable j, not a star.
3Hint 3Explore hintHide hint
Why is n capped at 9?
A · Core idea
Same nested structure as the star triangle, but the inner loop emits the running number j (1..i) instead of a fixed star — so row 3 is 123.
The one change from the star version. Replace the constant '*' with the inner loop variable j. Everything else — outer row loop, inner bound i, newlines between rows — is identical. Recognising that a pattern problem is 'the same skeleton with a different cell value' is the transferable insight.
Why n ≤ 9. Capping n at 9 keeps each printed number a single digit, so the columns line up; beyond 9 the multi-digit numbers would misalign.
Print j, not i. Emitting the outer index i (a fixed value per row) instead of the inner j is the common slip.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3row 1
1
"1""1\n12\n123"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 digits; total is O(n²).
E · Edge cases and pitfalls4n = 1 — "1".Explore edge casesHide edge cases
Edge cases handled
n = 1"1".n = 4"1\n12\n123\n1234".
Pitfalls
- Printing i instead of the running j.
- Off-by-one in the inner bound.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String numberTriangle(int n) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) sb.append(j); 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.
- Printing i instead of the running j.
- Off-by-one in the inner bound.
Revise
Carry the pattern into the interview
Pattern to remember
Nested numeric loop
Code pattern to memorize
for i in 1..n: for j in 1..i: print(j)Rules to remember
- 1Outer = rows.
- 2Inner prints 1..i.
- 3Newlines between rows.
Use this pattern when
- Index-valued patterns
Interview takeaway
Swap the star for the loop variable to print numbers.
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 numeric loop. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups