Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Fibonacci in Python

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Fibonacci in Python. Start with the requirements on the right.

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.

Problem

DeltaV control applications may use generated numeric sequences for simulation and testing. Given a non-negative integer n, return the first n terms of the Fibonacci sequence, where the sequence begins with 0, 1 and each later term is the sum of the previous two terms.

Formal Specification

Implement fibonacci(n):

  1. Input: an integer n, representing the number of Fibonacci terms to generate.
  2. Output: a list containing exactly n integers in Fibonacci order.
  3. For n = 0, return an empty list.
  4. Use an iterative approach rather than recursive recomputation.

Constraints

  • 0 <= n <= 10^4
  • The output contains exactly n integers
  • The sequence starts with 0, 1
  • Python integer arithmetic may be used for large Fibonacci values

Function Signature

def fibonacci(n):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output