Implement Fibonacci with efficiency constraints
Company: Google
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This Coding & Algorithms interview prompt for Data Scientist roles evaluates algorithmic efficiency, dynamic programming principles and numeric-handling considerations by requiring computation of the nth Fibonacci number at very large input sizes; abstraction level: implementation-level algorithmic optimization and complexity analysis.
Constraints
- 0 <= n <= 1,000,000,000
- Use MOD = 1,000,000,007
- Expected time complexity is O(log n)
Examples
Input: (0,)
Expected Output: 0
Explanation: This is the base case: F(0)=0.
Input: (1,)
Expected Output: 1
Explanation: This is the other base case: F(1)=1.
Hints
- The identities F(2k)=F(k)*(2*F(k+1)-F(k)) and F(2k+1)=F(k)^2+F(k+1)^2 can compute Fibonacci values by repeatedly doubling the index.
- Apply the modulo after additions and multiplications so intermediate values stay small.