Code Room
CodingHardcod-g843
Subject Modular arithmeticLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Solve a system of two congruences: find the smallest non-negative integer x such that x is congruent to r1 modulo m1 and x is congruent to r2 modulo m2. The moduli are NOT assumed coprime, so a solution exists only when (r2 - r1) is divisible by gcd(m1, m2); in that case the answer is unique modulo lcm(m1, m2). Return that smallest x, or -1 if no solution exists. Use the extended Euclidean algorithm to combine the two congruences.

Implement
crt_two(r1: int, m1: int, r2: int, m2: int) → int
Examples
in[2,3,3,5]out8
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.