Fibonacci Number

EasyDynamic Programming1d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

The Fibonacci sequence starts with F(0) = 0 and F(1) = 1, and every later term is the sum of the two terms before it: F(n) = F(n - 1) + F(n - 2) for n >= 2. Given a non-negative integer n, return F(n). A naive recursive version repeats work exponentially. Aim for O(n) time and O(1) extra space.

Examples

Input: n = 2
Output: 1
F(2) = F(1) + F(0) = 1 + 0 = 1.
Input: n = 4
Output: 3
The sequence runs 0, 1, 1, 2, 3, so F(4) = 3.
Input: n = 0
Output: 0
F(0) is defined as 0.

Constraints

  • 0 <= n <= 30

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 1d dp problems