Learner Level

Implement the method fibonacci that returns the nth Fibonacci number.

Return the Fibonacci number at position n. The sequence starts with F(0) = 0 and F(1) = 1.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Return 0 for n = 0.
  • Return 1 for n = 1.
  • An iterative solution avoids repeated recursive work.
Example 1
Input:
n (int) = 0
Return:
(int) 0
Example 2
Input:
n (int) = 1
Return:
(int) 1
Example 3
Input:
n (int) = 5
Return:
(int) 5
Example 4
Input:
n (int) = 10
Return:
(int) 55

Keep the previous two sequence values and update them until the requested position is reached.

Run your code to see the result.