Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Write Code for Fibonacci Variants
00:00
5 left

Write Code for Fibonacci Variants

EasyPython

Problem

Zomato may use sequence calculations in experimentation and ranking components. Implement a function that returns the nth Fibonacci number and supports recursive, iterative, and memoized strategies.

Use the definition F(0) = 0, F(1) = 1, and F(n) = F(n - 1) + F(n - 2) for n >= 2.

Formal Specification

Implement fibonacci(n, method), where n is a non-negative integer and method is one of "recursive", "iterative", or "memoized". Return the integer F(n). Do not use external libraries or floating-point arithmetic.

The implementation must make the strategy choice explicit. The recursive version should directly reflect the recurrence, the iterative version should use constant auxiliary space, and the memoized version should avoid recomputing subproblems.

Constraints

  • 0 <= n <= 100000
  • method is exactly "recursive", "iterative", or "memoized"
  • Recursive test inputs use n <= 30
  • Return the exact integer result

Function Signature

def fibonacci(n, method):
Interviewer

Your question is Write Code for Fibonacci Variants. 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.