Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop `i` from 1 to n, appending each `i` to a builder with spaces between.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you get inclusive bounds and separator handling right.
Where candidates slip
`i < n` dropping the last value, or a trailing space.
How to stand out
Use a builder/join, not += in a loop.
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
Counting loop
1Hint 1Explore hintHide hint
Loop a counter from 1 to n — which comparison includes n?
2Hint 2Explore hintHide hint
Use `<=` so n itself is printed.
3Hint 3Explore hintHide hint
Build with a StringBuilder/join and mind the separators.
A · Core idea
The most basic loop: run a counter i from 1 through n (inclusive) and collect each value. The whole lesson is inclusive bounds and building output without quadratic concatenation.
Why `i <= n`, not `i < n`. You want 1..n including n, so the loop condition is <=. Using < would stop at n−1 — the single most common loop off-by-one.
Why a StringBuilder / join. Appending into a growable buffer (Java StringBuilder) or joining a generator (Python) keeps the build O(n). Repeated += on an immutable String is O(n²).
Separator discipline. Add the space between numbers, not after each, so there's no trailing space — append the separator before all elements except the first.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3i=1
append 1
"1""1 2 3"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 iterations, each appending one value to a growable buffer.
E · Edge cases and pitfalls4n = 1 — Single value → "1".Explore edge casesHide edge cases
Edge cases handled
n = 1Single value → "1".n = 5"1 2 3 4 5".
Pitfalls
- Off-by-one: looping to n-1 or starting at 0.
- Trailing or leading space in the output.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String printToN(int n) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= n; i++) { if (i > 1) sb.append(' '); sb.append(i);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Off-by-one: looping to n-1 or starting at 0.
- Trailing or leading space in the output.
Revise
Carry the pattern into the interview
Pattern to remember
Counting loop
Code pattern to memorize
for i in 1..=n: append(i)Rules to remember
- 1i from 1 to n inclusive.
- 2Append each value.
- 3Mind the separators.
Use this pattern when
- Basic counting loops
Choose another pattern when
- `i < n` when you meant inclusive
Interview takeaway
Inclusive bounds (1..n) are the most common loop off-by-one.
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: Counting loop. Master it on small inputs and it transfers to harder problems.