Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop i from 1 to n, appending `k * i`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you generate by index-times-value correctly.
Where candidates slip
Off-by-one emitting 0 or n+1 terms.
How to stand out
Mention the add-k variant to avoid multiplication.
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
Index times k
1Hint 1Explore hintHide hint
What's the i-th multiple of k in terms of i?
2Hint 2Explore hintHide hint
Multiply the index by k (or add k repeatedly).
3Hint 3Explore hintHide hint
Start at i = 1 so you don't emit 0.
A · Core idea
Produce the first n positive multiples of k. The i-th multiple is simply k·i, so a single index-driven loop generates them in order.
Why index × k. Multiples of k are k·1, k·2, …, k·n. Multiplying the loop index by k gives each directly; alternatively, keep a running sum and add k each step (no multiplication).
Why start i at 1. The 'first' multiple is k·1 = k, not k·0 = 0. Starting at 0 would emit a spurious 0.
Overflow. For large k and i, k·i can exceed int range — accumulate or produce into a long when the values can be big.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
k = 3, n = 4i=1
3·1
3[3, 6, 9, 12]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, one multiply (or add) each.
E · Edge cases and pitfalls4k = 5, n = 2 — [5, 10].Explore edge casesHide edge cases
Edge cases handled
k = 5, n = 2[5, 10].k = 1, n = 3[1, 2, 3].
Pitfalls
- Starting at i = 0 (gives a 0 multiple).
- Producing the wrong count.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic java.util.List<Long> multiples(int k, int n) { java.util.List<Long> out = new java.util.ArrayList<>(); for (int i = 1; i <= n; i++) { out.add((long) k * i); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Starting at i = 0 (gives a 0 multiple).
- Producing the wrong count.
Revise
Carry the pattern into the interview
Pattern to remember
Index × k
Code pattern to memorize
for i in 1..n: out.append(k*i)Rules to remember
- 1i from 1 to n.
- 2Multiple = k·i.
- 3Or add k each step.
Use this pattern when
- Generating arithmetic sequences
Interview takeaway
The i-th multiple is k·i — index-driven generation.
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: Index × k. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups