Skip to content
CodeKruPro

The first n Fibonacci numbers

Coding problemEasyAsked in: Capgemini

The Fibonacci sequence starts with 0 and 1, and every later number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, and so on.

Given n, return the first n numbers of the sequence in order. For n = 0 return an empty list. The numbers grow fast: the last one allowed here is above 4,000,000,000,000,000,000, so use 64-bit integers.

Examples

Example 1
Input: n = 7
Output: [0,1,1,2,3,5,8]
Example 2
Input: n = 1
Output: [0]
Example 3
Input: n = 0
Output: []

Constraints

  • n is between 0 and 92

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in