Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop `i` from 1 to 10, building one line per multiplier with the product `n*i`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you handle fixed bounds and output formatting cleanly.
Where candidates slip
Off-by-one on the 10 bound, or trailing newline.
How to stand out
Note the loop is O(1) since the count is fixed.
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
Fixed-count loop
1Hint 1Explore hintHide hint
How many iterations does a 1..10 table need?
2Hint 2Explore hintHide hint
Format each line as 'n x i = product'.
3Hint 3Explore hintHide hint
Mind the inclusive 1..10 bound and newline placement.
A · Core idea
Print n's times-table from 1 to 10: a fixed-count loop of exactly ten iterations, formatting each line as n x i = n*i.
Why it's effectively O(1). The loop always runs ten times regardless of n, so the work is constant — there's no input-size dependence. The 'algorithm' is really just string formatting.
Bound discipline. Loop i from 1 to 10 inclusive; stopping at 9 or running to 11 is the obvious slip. Place the newline between lines so there's no trailing blank line.
Negative n. The product n*i is fine for negative n; the format string handles the sign automatically.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3i=1
3 x 1 = 3
line 1"3 x 1 = 3\n...\n3 x 10 = 30"C · Code with teaching comments
D · Why it works and complexityO(1) (always 10 iterations) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(1) (always 10 iterations) time, O(1) space — exactly ten iterations regardless of n — constant work.
E · Edge cases and pitfalls5n = 1 — 1 x 1 = 1 … 1 x 10 = 10.Explore edge casesHide edge cases
Edge cases handled
n = 11 x 1 = 1 … 1 x 10 = 10.n = 0All products 0.n = -2Products are negative; format handles the sign.
Pitfalls
- Looping to 9 or 11 instead of 10.
- Multiplying by the wrong operand.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String table(int n) { StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 10; i++) { sb.append(n).append(" x ").append(i).append(" = ").append(n * i); if (i < 10) sb.append('\n');Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Looping to 9 or 11 instead of 10.
- Multiplying by the wrong operand.
Revise
Carry the pattern into the interview
Pattern to remember
Fixed-count loop
Code pattern to memorize
for i in 1..10: line = f'{n} x {i} = {n*i}'Rules to remember
- 1i from 1 to 10.
- 2Line = n x i = n*i.
- 3Mind newline placement.
Use this pattern when
- Fixed-count formatting loops
Interview takeaway
A multiplication table is a ten-step formatting loop.
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-count loop. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups