Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Generating the Fibonacci Sequence
00:00
5 left

Generating the Fibonacci Sequence

EasyPython

Problem

Write a program to generate the Fibonacci sequence.

Implement generate_fibonacci(n), which returns the first n values, starting with 0, 1. The sequence follows F(k) = F(k - 1) + F(k - 2) for k >= 2.

Input and Output

  • Input: A non-negative integer n.
  • Output: A list containing exactly n Fibonacci numbers.

For n = 0, return an empty list. Use an iterative approach with constant auxiliary space besides the output list.

Constraints

  • 0 <= n <= 1000
  • The output must contain exactly n integers
  • The sequence starts with F(0) = 0 and F(1) = 1

Function Signature

def generate_fibonacci(n):
Interviewer

Your question is Generating the Fibonacci Sequence. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.