Code RoomFibonacci modulo m
HardPrep Room Coding #1390

Fibonacci modulo m

CodingAlgorithms & data structuresSenior–Staff~30 min

Return the n-th Fibonacci number modulo m, where F(0) = 0 and F(1) = 1, for n that can be very large (up to 10^18). Linear iteration is too slow, so use 2x2 matrix exponentiation of [[1,1],[1,0]] by fast (binary) exponentiation, taking every product modulo m. Return 0 when m == 1.

Implement
fib_mod(n: int, m: int) → int
Examples
in[10,1000]out55
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 30 min
InputExpectedGot
[10,1000]55not run yetsample