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
Returns (int) 0

Example 2

Input
n (int) = 1
Returns (int) 1

Example 3

Input
n (int) = 5
Returns (int) 5

Example 4

Input
n (int) = 10
Returns (int) 55

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

Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.