Fibonacci Series

Learn how to generate the Fibonacci Series in Java with multiple approaches, dry run, complexity analysis, interview follow-up questions, and best practices.

The Fibonacci Series is one of the most frequently asked Java coding interview questions. It tests your understanding of loops, variables, recursion, optimization, and problem-solving skills.


What is a Fibonacci Series?

The Fibonacci Series is a sequence where each number is the sum of the previous two numbers.

The series starts with:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34...

Formula:

F(0) = 0
F(1) = 1

F(n) = F(n-1) + F(n-2)

Real Interview Question

Write a Java program to print the first N Fibonacci numbers.


Example

Input

10

Output

0 1 1 2 3 5 8 13 21 34

Approach 1 — Using Iteration (Recommended)

This is the most efficient and commonly expected interview solution.


Algorithm

Step 1

Initialize

first = 0
second = 1

Step 2

Print first number.

Step 3

Repeat N times

  • Print current number
  • Calculate next number
  • Shift previous values

Dry Run

Suppose

n = 7
Iteration First Second Print
1 0 1 0
2 1 1 1
3 1 2 1
4 2 3 2
5 3 5 3
6 5 8 5
7 8 13 8

Output

0 1 1 2 3 5 8

Complete Java Solution

public class FibonacciSeries {

    public static void main(String[] args) {

        int n = 10;

        int first = 0;
        int second = 1;

        for (int i = 1; i <= n; i++) {

            System.out.print(first + " ");

            int next = first + second;

            first = second;

            second = next;
        }

    }

}

Output

0 1 1 2 3 5 8 13 21 34

Step-by-Step Explanation

Initially

first = 0
second = 1

Iteration 1

Print

0

Calculate

next = 0 + 1 = 1

Update

first = 1

second = 1

Iteration 2

Print

1

Calculate

next = 1 + 1 = 2

Update

first = 1

second = 2

Iteration 3

Print

1

Calculate

next = 1 + 2 = 3

Update

first = 2

second = 3

The same process repeats until N numbers are printed.


Approach 2 — Using Recursion

A recursive solution is elegant but less efficient.


Java Solution

public class FibonacciRecursion {

    static int fibonacci(int n) {

        if (n <= 1) {
            return n;
        }

        return fibonacci(n - 1) + fibonacci(n - 2);

    }

    public static void main(String[] args) {

        int n = 10;

        for (int i = 0; i < n; i++) {

            System.out.print(fibonacci(i) + " ");

        }

    }

}

Output

0 1 1 2 3 5 8 13 21 34

Approach 3 — Dynamic Programming

Avoid repeated calculations by storing previous values.

public class FibonacciDP {

    public static void main(String[] args) {

        int n = 10;

        int[] dp = new int[n];

        dp[0] = 0;
        dp[1] = 1;

        for (int i = 2; i < n; i++) {

            dp[i] = dp[i - 1] + dp[i - 2];

        }

        for (int num : dp) {

            System.out.print(num + " ");

        }

    }

}

Time Complexity

Iterative

Operation Complexity
Time O(n)
Space O(1)

Recursive

Operation Complexity
Time O(2ⁿ)
Space O(n)

Dynamic Programming

Operation Complexity
Time O(n)
Space O(n)

Comparison

Approach Time Space Recommended
Iteration O(n) O(1) ✅ Yes
Recursion O(2ⁿ) O(n) ❌ No
Dynamic Programming O(n) O(n) Good

Common Mistakes

Forgetting to update variables

Wrong

next = first + second;

Without updating

first = second;
second = next;

The program will print incorrect values.


Incorrect loop condition

Wrong

i < n

when expecting exactly N numbers.


Forgetting base cases in recursion

Always handle

if (n <= 1)

Otherwise recursion never ends.


Integer Overflow

After around the 46th Fibonacci number,

int

overflows.

Use

long

or

BigInteger

for larger values.


Interview Follow-up Questions

Q1. Print the Nth Fibonacci number.

Q2. Check whether a number belongs to the Fibonacci Series.

Q3. Generate Fibonacci using recursion.

Q4. Generate Fibonacci using Dynamic Programming.

Q5. Print Fibonacci without using a temporary variable.

Q6. Print Fibonacci in reverse order.

Q7. Find the sum of the first N Fibonacci numbers.

Q8. Find the largest Fibonacci number less than N.

Q9. Print only even Fibonacci numbers.

Q10. Print Fibonacci using Java Streams.


Related Coding Problems

  • Factorial
  • Prime Number
  • Reverse Integer
  • Armstrong Number
  • Power of a Number
  • Climbing Stairs
  • Tribonacci Sequence

Key Takeaways

  • Fibonacci is a classic interview problem for testing logical thinking.
  • The iterative solution is the preferred approach because it is simple and efficient.
  • Recursive solutions are easy to understand but inefficient due to repeated calculations.
  • Dynamic Programming improves recursion by storing intermediate results.
  • Always discuss Time Complexity, Space Complexity, and possible optimizations during interviews.

Interview Tip

If an interviewer asks:

"Print the Fibonacci Series."

Start with the iterative solution, explain why it has O(n) time and O(1) space complexity, and then mention recursion and dynamic programming as alternative approaches. This demonstrates both practical coding skills and a deeper understanding of algorithmic trade-offs.