Code Room
CodingMediumcod-g628
Subject Modular arithmeticLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a prime p and a list of integers, return the modular inverse of each element modulo p, i.e. for each x return the y in [0, p-1] with (x*y) % p == 1. Every input element is guaranteed coprime to p (1 <= x < p). Use Fermat's little theorem. Constraints: p is an odd prime up to 10^9+7, list length up to 1000.

Implement
mod_inverses(p: int, xs: list[int]) → list[int]
Examples
in[7,[1,2,3]]out[1,4,5]
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.