Practice Discrete Math

Algorithms / Modular Arithmetic Interview

Least You Need to Know: Modular Arithmetic Patterns, Inverses, and Residue Reasoning

Interview modular arithmetic is about replacing large values by equivalent residues without breaking the operation you care about. The key checkpoints are congruence rules, negative residues, and knowing when division is legal through inverses.

The least you need to know

Key notation

a ≡ b (mod m) `m` divides `a - b`
a^{-1} mod m value with `a * a^{-1} ≡ 1 (mod m)` when it exists
[a]_m the residue class of `a` modulo `m`

Tiny worked example

  • Suppose you need `(10^9 + 9)^2 mod 7`.
  • First reduce `10^9 + 9` modulo `7`.
  • Once you replace the large value by its residue, squaring the residue gives the same final answer modulo `7`.

Common mistakes

How to recognize this kind of problem

Start practice