Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Euclidean GCD proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Compute a nonnegative greatest common divisor by replacing a divisor pair with a smaller equivalent pair. Follow remainder states, simultaneous assignment, zero inputs, and the arithmetic cost of large Python integers.
1 practice exercise1 interview problem
Recognize it when A fraction or integer ratio needs normalization by the largest shared factor.
Checking your account…
The common divisors of a and b are exactly the common divisors of b and a modulo b. This identity lets Euclid replace the pair with a strictly smaller second value.
Repeat a, b = b, a mod b until b is zero. The terminal a is the greatest common divisor. Strict decrease proves termination; the stronger halving argument gives a logarithmic iteration bound: for r = a % b, either r <= b/2, or the next remainder is b % r = b - r < b/2. Thus within two reductions the current positive divisor b at least halves. If the original a is below b, first allow one swap-like reduction (a,b) -> (b,a); after that, b is the original smaller operand. This preliminary step and zero-input terminal work explain the constant term in the bound.
def gcd_trace(a,b):
a,b=abs(a),abs(b);trace=[]
while b:trace.append([a,b,a%b]);a,b=b,a%b
return traceImplement gcd_trace(a,b). Normalize signs and return [a,b,a%b] until b becomes zero.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def gcd_trace(a,b):
a,b=abs(a),abs(b);trace=[]
while b:trace.append([a,b,a%b]);a,b=b,a%b
return tracePredict the existing trace Lab for (35, 15). Its returned rows are the old pair and its remainder, not the gcd itself. The second call shows why an empty trace can still represent a valid nonzero gcd.
| Step | Executed statement | State | Why |
|---|---|---|---|
| Normalize | a,b=abs(a),abs(b);trace=[] |
a=35, b=15, trace=[] |
Signs cannot change the set of positive common divisors. |
| First remainder | trace.append([a,b,a%b]) |
trace=[[35, 15, 5]] |
35 = 2*15 + 5, so the next pair has the same common divisors. |
| Replace old pair | a,b=b,a%b |
a=15, b=5 |
Both right-hand expressions use the original pair. |
| Final remainder | trace.append([a,b,a%b]);a,b=b,a%b |
trace=[[35, 15, 5], [15, 5, 0]], a=5, b=0 |
A zero remainder means 5 divides 15; the loop terminates before another modulo. |
| Zero-input call | while b |
a=7, b=0, trace=[] |
The normalized terminal pair requires no recorded reductions. |
| Output | |
|---|---|
print(gcd_trace(35, 15)) |
[[35, 15, 5], [15, 5, 0]] |
print(gcd_trace(-7, 0)) |
[] |
Use the trace to justify each replacement, then transfer the same invariant to gcd_many: each completed input extends the shared-divisor requirement. For a direct production calculation, Python math.gcd already supplies the integer contract.
Return a nonnegative gcd by taking absolute values. gcd(a, 0) is abs(a), and gcd(0, 0) is conventionally zero in programming libraries.
def gcd_many(values):
result=0
for value in values:
a,b=result,abs(value)
while b:a,b=b,a%b
result=a
return resultImplement gcd_many(values). Return the nonnegative greatest common divisor, with 0 for empty input.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
def gcd_many(values):
result=0
for value in values:
a,b=result,abs(value)
while b:a,b=b,a%b
result=a
return resultUse lcm(a,b) = abs(a // gcd(a,b) * b), guarding the all-zero case and dividing before multiplication. GCD also reduces fractions and normalizes ratios.
Say: “Replacing (a,b) with (b,a mod b) preserves every common divisor while shrinking the second argument.” Trace one remainder sequence, normalize signs, and state the logarithmic remainder-iteration bound and the additional bit cost for large integers.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Euclidean GCD proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| One gcd pair: remainder iterations | O(1 + log(1 + min(abs(a), abs(b)))) | O(1 + log(1 + min(abs(a), abs(b)))) | Every two nonterminal reductions halve the current positive divisor b. If initial a<b, one preliminary reduction makes b the original smaller operand; zero cases and initialization add constant work. |
O(1) scalar slots for a single gcd; the trace Lab deliberately retains O(1 + log(1 + min(abs(a), abs(b)))) rows.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
With (-9, 0), returning a directly gives -9 even though the gcd convention is nonnegative.
Prevent it: Normalize both inputs before the loop; an already terminal pair still needs sign normalization.
result = aresult = abs(a)Where you will hit this: Compute GCD with Euclid(opens in a new tab)
For (35, 15), sequential assignment changes a to 15, so a % b becomes zero and discards the actual remainder 5.
Prevent it: Compute both right-hand values from the old pair using simultaneous assignment.
a = b
b = a % ba, b = b, a % bWhere you will hit this: Compute GCD with Euclid(opens in a new tab)
When b is zero there is no next Euclidean transition. Taking a % b raises ZeroDivisionError.
Prevent it: Check the divisor before calculating the next remainder. The Lab uses while b for this reason.
remainder = a % bremainder = None if b == 0 else a % bWhere you will hit this: Insert GCD Nodes in a Linked List(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27