N-th Fibonacci Number
Updated
Problem statement
The Fibonacci sequence starts with 0 and 1, and each term after that is the sum of the two terms just before it: 0, 1, 1, 2, 3, 5, 8, ...
Number the terms from 0, so F(0) = 0, F(1) = 1 and F(n) = F(n - 1) + F(n - 2) for n >= 2. Given n, return F(n).
For the larger values of n the answer does not fit in a 32-bit integer, so return it as a 64-bit integer.
Function
fibonacci(n: int) → longExamples
Example 1
n = 9return = 34The terms from F(0) to F(9) are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. The last one is 34.
Example 2
n = 0return = 0F(0) is the first term, 0.
Example 3
n = 2return = 1F(2) = F(1) + F(0) = 1 + 0 = 1.
Constraints
0 <= n <= 50
More Bank of America Onsite Interview questions
- Centered Star PyramidONSITE INTERVIEW · Seen Nov 2025
- Employees Without a ManagerONSITE INTERVIEW · Seen Nov 2025
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2025
- Reverse Each Word in a SentenceONSITE INTERVIEW · Seen Nov 2025
- Second Highest SalaryONSITE INTERVIEW · Seen Nov 2025
- Three Numbers That Add Up to a TargetONSITE INTERVIEW · Seen Apr 2025
- Duplicate RowsONSITE INTERVIEW · Seen Oct 2023
- Factorial of a NumberONSITE INTERVIEW · Seen Oct 2023
- Palindrome String CheckONSITE INTERVIEW · Seen Sep 2023
- Second Largest Element in an ArrayONSITE INTERVIEW · Seen Jun 2023
Similar questions at other companies
- Fibonacci with Input ValidationMorgan Stanley · ONSITE INTERVIEWMorgan Stanley interview questions
- Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Fibonacci NumberMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Climbing StairsAmazon · ONSITE INTERVIEW
- Climbing StairsAMD · ONSITE INTERVIEWAMD interview questions
- Climb Stairs with One, Two, or Three StepsAgoda · ONSITE INTERVIEWAgoda interview questions
- Fibonacci NumberJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- Climbing StairsExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- The Nth Number in the Fibonacci SequenceCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Count Complete Tic-Tac-Toe GamesDecagon · PHONE SCREEN · ONSITE INTERVIEWDecagon interview questions