Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Fibonacci Implementation
00:00
5 left

Fibonacci Implementation

EasyPython

Problem

Virtu Financial monitoring components may use predictable numeric sequences when validating data-processing behavior. Implement a function that generates the first n Fibonacci numbers and supports both recursive and iterative strategies.

The Fibonacci sequence is defined as F(0) = 0, F(1) = 1, and F(k) = F(k - 1) + F(k - 2) for k >= 2.

Formal Specification

Implement fibonacci_sequence(n, method):

  • n is a non-negative integer representing the number of terms to return.
  • method is either "recursive" or "iterative".
  • Return a list containing F(0) through F(n - 1) in order.
  • The recursive implementation must use memoization so that it remains efficient for the permitted input size.
  • Return an empty list when n == 0.

Constraints

  • 0 <= n <= 30
  • method is exactly "recursive" or "iterative"
  • The sequence starts with F(0) = 0 and F(1) = 1

Function Signature

def fibonacci_sequence(n, method):
Interviewer

Your question is Fibonacci Implementation. 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.