Code Room
CodingHardcod-g643
Subject MathLevel Senior–Staff~30 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Compute S = (1 + a + a^2 + ... + a^(b-1)) mod m, the sum of a geometric series with b terms, where 0 <= a, 0 <= b <= 10^18, and 1 <= m <= 10^9. Use a divide-and-conquer recurrence so it runs in O(log b) modular operations (do NOT loop b times). If b == 0 the sum is 0.

Implement
geo_series_mod(a: int, b: int, m: int) → int
Examples
in[2,4,1000000007]out15
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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.