Data Structures & AlgorithmsHardProduct Company SDE · DP round

Problem: What is the size of the largest divisible subset of [1, 2, 4, 8, 3] (every pair must satisfy one dividing the other)? Run the O(n^2) DP.

Problem: What is the size of the largest divisible subset of [1, 2, 4, 8, 3] (every pair must satisfy one dividing the other)? Run the O(n^2) DP.
a = sorted([1, 2, 4, 8, 3])
n = len(a)
dp = [1] * n
for i in range(n):
    for j in range(i):
        if a[i] % a[j] == 0:
            dp[i] = max(dp[i], dp[j] + 1)
print(max(dp))
What is printed?
  1. A. 3
  2. B. 5
  3. C. 2
  4. D. 4

Correct answer: D. 4

The subset {1, 2, 4, 8} is a chain of divisors of size 4, and 3 cannot join it. Sorting first makes the divisibility check one-directional.

More Coding Interview Problems questions

Exams that ask this topic