← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Pinterest ML Engineer interview with a tricky string manipulation problem that took me a while to fully wrap my head around. The math behind it is deceptively subtle and I'm not sure I nailed the explanation.

Questions Asked (1)

Q1

You're given a string of the form 'A+B' where A and B are digit strings with no zeros. Insert exactly one pair of parentheses that must enclose the plus sign, such that digits outside the parentheses are treated as multiplicative factors. Find the placement that minimizes the resulting value and return the full expression string.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The problem looks like a parsing puzzle but it's really an optimization problem once you see the structure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem by restating it and confirming edge cases. Then, derive the mathematical condition for the optimal split point by comparing the effect of parentheses at different positions. Finally, implement a linear scan to find the split that minimizes the value, and construct the resulting expression string.

Pro tip: Mention that the optimal split occurs where the left part's value is just below the right part's value, and that you can avoid big integer arithmetic by comparing strings or using logarithms. This shows you understand both the math and practical implementation constraints.

1. Clarify the problem and constraints

Restate the problem to ensure understanding: we have a string 'A+B' with no zeros, and we must insert one pair of parentheses around the plus sign. The digits outside become multiplicative factors. Confirm that the parentheses must enclose the plus sign and that the expression is evaluated with standard precedence (multiplication before addition).

2. Derive the mathematical condition

Let the split be at position i: left part L = A[0..i], right part R = A[i+1..] + B. The value is L * (R + B) if parentheses are around the plus? Actually, the expression becomes: digits before '(' multiply the sum inside, and digits after ')' multiply as well? Wait, the problem says: 'digits outside the parentheses are treated as multiplicative factors.' So if we insert parentheses around the plus sign, the expression becomes: (prefix) * (A_suffix + B_prefix) * (suffix)? Need to parse carefully. Typically, the string is like '123+456', and we insert parentheses such that the plus is inside, e.g., '12(3+4)56' meaning 12 * (3+4) * 56. So the value is (left_outside) * (sum_inside) * (right_outside). So we need to choose a split of A into A1 and A2, and B into B1 and B2, such that the expression is A1 * (A2 + B1) * B2. But the parentheses must enclose the plus sign, so the plus is between A2 and B1. So we choose a split point in A and a split point in B? Actually, the parentheses enclose the plus sign, so the plus sign is inside. The digits outside are multiplicative factors. So if we put parentheses around the plus sign, the expression becomes: (digits before the opening parenthesis) * (digits inside the parentheses) * (digits after the closing parenthesis). The digits inside the parentheses are of the form A_suffix + B_prefix, where A_suffix is a suffix of A and B_prefix is a prefix of B. The digits before are the prefix of A, and digits after are the suffix of B. So the value is: A_prefix * (A_suffix + B_prefix) * B_suffix. We need to choose where to split A and B such that the plus sign is inside. Actually, the plus sign is fixed between A and B. So the parentheses must enclose the plus sign, meaning the opening parenthesis is somewhere in A (or before A) and the closing parenthesis is somewhere in B (or after B). But the problem says 'insert exactly one pair of parentheses that must enclose the plus sign'. So the parentheses can be placed such that the plus sign is inside. The digits outside are multiplicative factors. So if we put '(' after some prefix of A and ')' after some prefix of B, then the expression is: A_prefix * (A_suffix + B_prefix) * B_suffix. We need to minimize this value. So we need to choose split points i in A and j in B. But note that the parentheses must enclose the plus sign, so the opening parenthesis must be before the plus sign and the closing parenthesis after the plus sign. So i can be from 0 to len(A) (where i=0 means '(' before A, i=len(A) means '(' after all of A, i.e., just before the plus sign). Similarly, j can be from 0 to len(B) (j=0 means ')' just after the plus sign, j=len(B) means ')' after all of B). So we have (len(A)+1)*(len(B)+1) possibilities. But we can simplify: the value is A_prefix * (A_suffix + B_prefix) * B_suffix. We want to minimize this. Since all digits are non-zero, all parts are positive. We can consider the effect of moving the parentheses. Typically, the optimal is to put the parentheses such that the sum inside is as small as possible relative to the outside factors. But we need to find the exact split. We can derive that the optimal split is when the left factor is just less than the right factor? Actually, for a fixed total product, we want to balance. But here the sum inside depends on the split. We can think of it as: we are splitting the string into three parts: X, Y, Z, where Y is the sum inside, and the value is X * Y * Z. But X and Z are numbers formed by digits, and Y is the sum of two numbers formed by digits. This is more complex. However, note that the plus sign is fixed, so the split of A and B are independent? Actually, the sum inside is A_suffix + B_prefix. So we can think of choosing a split point in the concatenated string? Not exactly. Let's denote the string as S = A + '+' + B. We insert '(' after some position in A (or before A) and ')' after some position in B (or after B). So the expression becomes: A[0..i] * (A[i..] + B[..j]) * B[j..]. We want to minimize this. Since all numbers are positive, we can take logs: log(A_prefix) + log(A_suffix + B_prefix) + log(B_suffix). We want to minimize this sum. This is a discrete optimization problem. We can iterate over all possible i and j, but that could be O(n^2) which might be too slow if the string is long. But we can optimize: for a fixed i, the function f(j) = log(A_suffix + B_prefix) + log(B_suffix) is unimodal? Not necessarily. But we can note that the optimal j is likely where B_prefix is just less than A_suffix? Actually, we can use the fact that for positive numbers, the product is minimized when the factors are as unbalanced as possible? No, we want to minimize the product, so we want the factors to be as small as possible. But the sum inside is at least the sum of the first digit of A_suffix and the first digit of B_prefix? Actually, we can think of it as: we want to make the sum inside as small as possible, but also the outside factors as small as possible. There is a trade-off: moving the parentheses to the right (increasing i) makes A_prefix larger and A_suffix smaller, so the sum inside might decrease, but A_prefix increases. Similarly for j. So the optimal is somewhere in the middle. We can derive that the optimal split is when A_prefix is just less than A_suffix + B_prefix? Not exactly. Let's consider a simpler version: if we only had to insert parentheses around the plus sign without splitting B? Actually, the problem is known: given a string of digits and a plus sign, insert parentheses to minimize the value. This is a known problem. The solution is to find the split point where the left part is as close as possible to the right part? Actually, for the expression A * (B + C) * D, we want to minimize. Since all are positive, we can take logs. The derivative with respect to the split points is not continuous. But we can use the fact that the optimal split occurs when the left factor is just less than the right factor? Let's test with an example: '123+456'. Possible splits: i=0: '(' before A, so A_prefix=1? Actually, if i=0, A_prefix is empty? But the problem says 'digits outside the parentheses are treated as multiplicative factors.' If there are no digits outside, then it's just 1? Typically, an empty string is not a number. So we must have at least one digit outside? The problem says 'insert exactly one pair of parentheses that must enclose the plus sign'. It doesn't say that there must be digits outside. But if we put parentheses at the very beginning and end, then the expression is just (A+B), which is a valid expression. But then there are no multiplicative factors. So the value is A+B. That is a candidate. So i can be 0 (meaning '(' before A) and j=len(B) (meaning ')' after B). Then the expression is (A+B). That is valid. So we need to consider all i from 0 to len(A) and j from 0 to len(B). But note that if i=0, A_prefix is empty, so it's like 1? Actually, in the expression, if there is no digit before '(', then it's just (A_suffix + B_prefix) * B_suffix. But the problem says 'digits outside the parentheses are treated as multiplicative factors.' If there are no digits outside on the left, then there is no factor. So the value is just (A_suffix + B_prefix) * B

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.