Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Dynamic programming

Split an integer for the largest product

Medium · Target 15 minutes · Dynamic programming · Type asked atGoogleAmazon

Given an integer n ≥ 2, write it as a sum of at least two positive integers and return the largest possible product of those integers.

Examples

InputOutputWhy
n = 211 + 1.
n = 10363 + 3 + 4.
n = 8183 + 3 + 2.

Constraints

  • 2 ≤ n ≤ 58

Hints

Hint 1

best[t] = the largest product of parts summing to t, where t itself may be one part.

Hint 2

best[t] = max over the first part p of p · best[t − p], with best[0] = 1.

Hint 3

For n itself, the part n alone is not allowed.

Where people lose marks

  • Allowing n as a single part, which returns n.
  • best[0] = 0, which zeroes every product.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.