Magicsheet logo

Find the Maximum Divisibility Score

Easy
77.4%
Updated 6/1/2025

Asked by 1 Company

Topics

Find the Maximum Divisibility Score

What is this problem about?

In the Find the Maximum Divisibility Score interview question, you are given two arrays: nums and divisors. The "divisibility score" of a divisor is the number of elements in nums that are divisible by it. You need to find the divisor with the maximum score. If multiple divisors have the same maximum score, you should return the smallest one among them.

Why is this asked in interviews?

DE Shaw uses this problem to test a candidate's ability to handle nested iterations and multiple tie-breaking conditions. It evaluations your proficiency with the Array interview pattern and your ability to track multiple metrics (score and value) simultaneously. It’s a basic test of clean coding and efficiency within O(NimesM)O(N imes M) constraints.

Algorithmic pattern used

This is a Linear Scan problem with Tie-breaking logic.

  1. Initialize maxScore = -1 and bestDivisor = Infinity.
  2. Iterate through every element in the divisors array.
  3. For each divisor, iterate through the nums array and count how many numbers are divisible by the current divisor (using num % divisor == 0).
  4. If the count is greater than maxScore, update both maxScore and bestDivisor.
  5. If the count equals maxScore, update bestDivisor = min(bestDivisor, currentDivisor).

Example explanation

nums = [12, 6, 8], divisors = [2, 3]

  1. Divisor 2: 12, 6, and 8 are all divisible by 2. Score = 3. bestDivisor = 2.
  2. Divisor 3: 12 and 6 are divisible by 3. Score = 2.
  3. Max score is 3, so result is 2.

Common mistakes candidates make

  • Inefficient Tie-break: Forgetting to return the smallest divisor when scores are equal.
  • Score Initialization: Initializing maxScore to 0, which might not correctly handle cases where no numbers are divisible.
  • Complexity: Trying to use advanced data structures like Segment Trees when a simple nested loop is sufficient for the given constraints.

Interview preparation tip

When tracking the "best" element, always identify the primary criteria (score) and secondary criteria (value). Writing a clean if condition that handles both (count > maxScore || (count == maxScore && divisor < bestDivisor)) is a sign of a disciplined programmer.

Similar Questions