Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Maintain `a = 0`, `b = 1`; n times, append `a` then advance `(a, b) → (b, a+b)`. O(n).
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you collect-while-rolling with correct ordering and count.
Where candidates slip
Off-by-one on the count, or skipping F(0).
How to stand out
Reuse the O(1)-state roll rather than recursion or an array of all prior terms.
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
Two-variable roll + collect
1Hint 1Explore hintHide hint
It's nth-Fibonacci, but collect every value, not just the last.
2Hint 2Explore hintHide hint
Append the current value before or after rolling? (It changes F(0).)
3Hint 3Explore hintHide hint
Loop exactly n times for n terms.
A · Core idea
Generate the first n Fibonacci numbers by rolling two variables forward and collecting each value — nth-Fibonacci, but emitting the whole series.
Why append before rolling. Append the current a first, then advance (a, b) → (b, a+b). Appending after the roll would skip F(0) = 0 and start the series at 1. Order matters.
Why O(1) extra state (besides output). Like nth-Fibonacci, each term needs only the previous two, so two variables suffice regardless of n. The output list is O(n), which is unavoidable since you return n values.
Exactly n terms. Loop n times to produce n numbers; producing n−1 or n+1 is the common off-by-one. Overflow caps the usable range near n = 90 for long.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 5append 0
roll → (1,1)
[0][0, 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 of one addition; output list is O(n), state is O(1).
E · Edge cases and pitfalls4n = 1 — [0] — F(0) only.Explore edge casesHide edge cases
Edge cases handled
n = 1[0] — F(0) only.n = 2[0, 1].
Pitfalls
- Appending after rolling (skips F(0)).
- Producing n+1 or n-1 terms.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic java.util.List<Long> fibSeries(int n) { java.util.List<Long> out = new java.util.ArrayList<>(); long a = 0, b = 1; for (int i = 0; i < n; i++) { out.add(a);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Appending after rolling (skips F(0)).
- Producing n+1 or n-1 terms.
Revise
Carry the pattern into the interview
Pattern to remember
Roll + collect
Code pattern to memorize
a,b=0,1; repeat n: out.append(a); a,b=b,a+bRules to remember
- 1Append a before rolling.
- 2(a,b)→(b,a+b).
- 3Exactly n appends.
Use this pattern when
- Generating a recurrence series
Interview takeaway
Collect-while-rolling turns the nth-value loop into a series.
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: Roll + collect. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups