Data Structures & AlgorithmsHardProduct Company SDE · DP round
Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP.
Problem: What is the minimum number of perfect squares (1, 4, 9, ...) that sum to 12? Run the DP.
INF = 10 ** 9
dp = [0] + [INF] * 12
for v in range(1, 13):
for s in range(1, 5):
if s * s <= v:
dp[v] = min(dp[v], dp[v - s * s] + 1)
print(dp[12])What is printed?- A. 2
- B. 3
- C. 4
- D. 1
Correct answer: B. 3
12 = 4 + 4 + 4 with three squares, and 12 is not a sum of two squares, so 3 is minimal.
More Coding Interview Problems questions
- Problem: How many non-decreasing strings of length 3 over the digits {0, 1, 2} exist? Run the code. python from itertools import product…
- Problem: What is the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5]? Run the DP. python a = [1, 101, 2, 3…
- Problem: How many unordered pairs of indices (i < j) of [5, 5, 5, 5] have a sum of 10? Run the code. python a = [5, 5, 5, 5] print(sum(1…
- Problem: What does the following print? python print(list(map(lambda x: x 2, filter(lambda x: x % 2, range(6))))) What is printed?
- Problem: What does the following print? python print(bool([]), bool([0]), bool(0.0), bool('False')) What is printed?
- Problem: What does the following print? python a = (1,) print(a 3, len(a 0)) What is printed?