Compute the Fibonacci Value at an Index

Quick Overview

Compute the Fibonacci Value at an Index. The sequence starts with F(0)=0 and F(1)=1.

Compute the Fibonacci Value at an Index

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The sequence starts with F(0)=0 and F(1)=1. For n greater than one, F(n)=F(n-1)+F(n-2). Return F(n). ```cpp class Solution { public: int fib(int n); }; ``` ### Examples **Example 1:** ```text n = 5 Output: 5 Explanation: The values through index five are 0,1,1,2,3,5. ``` **Example 2:** ```text n = 7 Output: 13 Explanation: F(7)=8+5. ``` ### Constraints - The sequence index n is between 0 and 30, inclusive. ### Clarifying Questions Use the stated zero-based initial conditions and retain only the two preceding values. ```hint Read the boundary condition Which already-computed values are needed for the next sequence term? ``` ### Contract considerations Use the stated zero-based initial conditions and retain only the two preceding values.

Overview: Compute the Fibonacci Value at an Index. The sequence starts with F(0)=0 and F(1)=1.

Meta logo
Meta
Sep 15, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

The sequence starts with F(0)=0 and F(1)=1. For n greater than one, F(n)=F(n-1)+F(n-2). Return F(n).

class Solution {
public:
    int fib(int n);
};

Examples

Example 1:

n = 5
Output: 5
Explanation: The values through index five are 0,1,1,2,3,5.

Example 2:

n = 7
Output: 13
Explanation: F(7)=8+5.

Constraints

  • The sequence index n is between 0 and 30, inclusive.

Clarifying Questions Guidance

Use the stated zero-based initial conditions and retain only the two preceding values.

Contract considerations

Use the stated zero-based initial conditions and retain only the two preceding values.