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?- A. 3
- B. 5
- C. 2
- 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
- Problem: How many subarrays of [1, 2, 1, 2, 3] contain exactly 2 distinct values? Run the code using the 'at most' trick. python from…
- Problem: Dijkstra from 0 with edges (0,1,1), (1,3,2), (0,2,4), (2,3,1). Which vertex sequence is the shortest path from 0 to 3? Run the…
- Problem: Is the graph with edges forming the 5-cycle 0-1-2-3-4-0 bipartite? And is the 4-cycle 0-1-2-3-0 bipartite? Run the BFS colouring…
- Problem: Tree with edges 0-1, 0-2, 1-3, 1-4, 2-5. How many nodes are in the subtree of node 1 (rooted at 0)? Run the DFS code. python…
- Problem: Which statement about Fenwick (binary indexed) trees is false?
- Problem: Items have small values (at most 1000 each, n = 100) but very large weights (up to 10^9), and capacity W = 10^9. Which DP is…