Data Structures & AlgorithmsHardAmazon · Microsoft OA
Problem: Break n into at least two positive integers to maximise their product.
Problem: Break n into at least two positive integers to maximise their product.
def brk(n):
dp = [0, 1] + [0] * (n - 1)
for i in range(2, n + 1):
dp[i] = max(max(j, dp[j]) * (i - j) for j in range(1, i))
return dp[n]
print(brk(10), brk(11))What is printed?- A. 32 48
- B. 36 48
- C. 36 54
- D. 30 54
Correct answer: C. 36 54
Use as many 3s as possible, with a 2 + 2 instead of 3 + 1: 10 = 3 + 3 + 4 → 36; 11 = 3 + 3 + 3 + 2 → 54. The DP compares keeping j whole versus splitting it further.
More Coding Interview Problems questions
- A suffix array of a string of length n is built by putting all suffixes in a list and sorting them with a comparison sort, where each…
- What is the solution of T(n) = T(n/2) + T(n/4) + n?
- Edge case: Java's int arithmetic wraps around in two's complement. The Python code below emulates exactly what this Java line computes…
- Edge case: Array sorting and parseInt in JavaScript. javascript console.log([3, 20, 100].sort().join(","), [1, 2…
- Problem: Answer sub-rectangle sum queries on a matrix in O(1) each with a 2-D prefix-sum table. python m = [[3, 0, 1, 4], [5, 6, 3, 2]…
- Problem: A van with fixed capacity picks up and drops passengers; trip [n, from, to] carries n passengers from km 'from' to km 'to' (they…