Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compute GCD

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Compute GCD. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A Workiva Wdesk validation utility needs to reduce two integer values to their greatest common divisor. Implement the Euclidean algorithm to return the largest positive integer that divides both inputs without a remainder.

Formal Specification

Implement gcd(a, b), where a and b are non-negative integers. Return an integer representing their greatest common divisor. At least one input is guaranteed to be positive. The result must be non-negative.

You may use the identity gcd(a, b) = gcd(b, a % b) repeatedly until the second value becomes zero.

Constraints

  • 0 <= a, b <= 10^18
  • a + b > 0
  • Inputs are integers
  • Do not enumerate all possible divisors

Function Signature

def gcd(a, b):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output