PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • medium
  • Google
  • Coding & Algorithms
  • Data Scientist

Implement Fibonacci with efficiency constraints

Company: Google

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Write a function `fib(n)` that returns the nth Fibonacci number (0-indexed: fib(0)=0, fib(1)=1). Requirements: - Handle `n` up to at least 10^6. - Discuss time and space complexity. - If exact integers are too large for the language’s native int, explain what you would do (e.g., modulo arithmetic or big integers).

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.

Implement `solution(n)`, which computes the 0-indexed Fibonacci number where F(0)=0 and F(1)=1. Because Fibonacci numbers become extremely large, return F(n) modulo 1,000,000,007. Your solution should be efficient enough for very large n and should not use the naive exponential recursive definition. If an exact integer version were required in a language with fixed-width integers, you would use a big integer library such as Java BigInteger, Python int, or C++ multiprecision; alternatively, modulo arithmetic as used here keeps values bounded.

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.

Input: (10,)

Expected Output: 55

Explanation: The 10th Fibonacci number is 55.

Input: (50,)

Expected Output: 586268941

Explanation: F(50)=12586269025, and 12586269025 modulo 1,000,000,007 is 586268941.

Input: (100,)

Expected Output: 687995182

Explanation: F(100)=354224848179261915075, which becomes 687995182 after taking modulo 1,000,000,007.

Input: (1000000,)

Expected Output: 918091266

Explanation: This large input checks that the algorithm is logarithmic rather than linear or exponential.

Hints

  1. 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.
  2. Apply the modulo after additions and multiplications so intermediate values stay small.
Last updated: Jun 22, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
  • Boolean Expression Tree with Leaf Flips - Google (medium)
  • Streaming Points: Remove Any Pair Within a Distance - Google (medium)
  • Most Active Users in a Live Communication Stream - Google (medium)
  • Solve Rooms and Top-K Streams - Google (medium)