L1 · FoundationsNumbers & loops~4 min · 6 tests

GCD With the Euclidean Algorithm

Compute the greatest common divisor with Euclid's algorithm in O(log n) instead of trying every divisor. Includes LCM and a speed comparison you can run.

The problem

Return the greatest common divisor of two non-negative integers a and b (not both zero). Don't use math.gcd.

Examples

  1. Example 1

    Input

    gcd(48, 18)

    Expected output

    6
  2. Example 2

    Input

    gcd(7, 5)

    Expected output

    1

+ 4 hidden tests on Submit.

Edge cases to ask about

  • One argument is 0
  • Equal numbers
  • Co-prime numbers

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · gcd
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Try every divisor downwardsO(min(a, b))O(1)
    bestEuclid: gcd(a, b) = gcd(b, a mod b)O(log min(a, b))O(1)The numbers shrink at least by half every two steps.
    Walkthrough of the optimal approach (try it yourself first)

    Euclid's algorithm: gcd(a, b) = gcd(b, a % b), and gcd(a, 0) = a. The remainders shrink fast — at least halving every two steps — so it is O(log n), against O(n) for trying every divisor.

    LCM follows directly: a * b // gcd(a, b).

    Complexity: O(log n) time, O(1) space. Each step replaces (a, b) with (b, a mod b), and the remainder at least halves every two steps.

    Reveal the reference solution
    def gcd(a, b):
        while b:
            a, b = b, a % b
        return a

    The brute force, for comparison

    def gcd(a, b):
        for d in range(min(a, b), 0, -1):
            if a % d == 0 and b % d == 0:
                return d
        return max(a, b)

    Follow-ups interviewers ask

    • LCM of a list of numbers.
    • Write it recursively.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of GCD With the Euclidean Algorithm in Python?

    The optimal solution runs in O(log n) time and O(1) auxiliary space. Each step replaces (a, b) with (b, a mod b), and the remainder at least halves every two steps.

    What is the brute-force approach, and how do you optimise it?

    Try every divisor downwards: O(min(a, b)) time, O(1) space. Euclid: gcd(a, b) = gcd(b, a mod b): O(log min(a, b)) time, O(1) space. The numbers shrink at least by half every two steps.

    What follow-up questions do interviewers ask about GCD With the Euclidean Algorithm?

    LCM of a list of numbers. Write it recursively.