FastPrepGreatest Common Divisor
Problems/Greatest Common Divisor

Greatest Common Divisor

JP Morgan Chase logoJP Morgan Chase● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a JP Morgan Chase full-time onsite interview. The report asks for the greatest common divisor of two numbers. We chose nonnegative inputs, the rule for zero and the limits on the numbers. Our version is about 90% the same as the reported question.

Return the greatest common divisor of two nonnegative integers a and b.

For this exercise, assume gcd(0, x) = x, including gcd(0, 0) = 0. When at least one value is positive, the result is the largest positive integer that divides both values.

Function

greatestCommonDivisor(a: int, b: int) → int

Examples

Example 1

a = 48b = 18return = 6

The common positive divisors are 1, 2, 3 and 6. The largest is 6.

Example 2

a = 0b = 25return = 25

The zero-input convention gives gcd(0, 25) = 25.

Example 3

a = 7b = 13return = 1

The two distinct primes have no common positive divisor other than 1.

Constraints

  • 0 <= a, b <= 10^9

More JP Morgan Chase Onsite Interview questions

  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Employees Above Average SalaryONSITE INTERVIEW · Seen Sep 2026
  • Highest-Earning Employees By DepartmentONSITE INTERVIEW · Seen Sep 2026
  • K Largest Integers in Descending OrderONSITE INTERVIEW · Seen Aug 2026
  • Tile a Three-Row Board with DominoesONSITE INTERVIEW · Seen Jan 2026
  • Two SumONSITE INTERVIEW · Seen Nov 2025
  • Intersection of Two ListsONSITE INTERVIEW · Seen Jan 2025
  • Maximum Subarray SumONSITE INTERVIEW · Seen Jan 2025
  • Fibonacci NumberONSITE INTERVIEW · Seen Nov 2024

Similar questions at other companies

  • Plus OneDruva · ONSITE INTERVIEW
  • Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
  • Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
  • Climbing StairsAmazon · ONSITE INTERVIEW
  • Maximum 69 NumberHSBC · ONSITE INTERVIEWHSBC interview questions
  • Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
  • Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
  • Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
  • Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions

Interview reports that mention this question

  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)
  • JPMorgan Code for Good India: 10 Experiences (Part 4 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public int greatestCommonDivisor(int a, int b) {
        // Write your code here.
    }
}
Checking account…