FastPrepLargest Minus Smallest Value of a Parenthesized Expression
Problems/Largest Minus Smallest Value of a Parenthesized Expression

Largest Minus Smallest Value of a Parenthesized Expression

Nutanix logoNutanix● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second round of an on-campus internship drive, the interviewer asked for the gap between the largest and the smallest value you can get by placing parentheses in a string of digits joined by plus and times signs. The candidate wrote a dynamic programming answer on paper, and the interviewer tried it on small cases.

The report does not say how big the numbers can be or how long the string is. Here every number is a single digit from 0 to 9. The string holds 1 to 19 digits with no spaces. The digits and signs stay in their order. Only the parentheses decide which sign goes first, and any grouping is allowed. A single digit gives 0. You return the largest value minus the smallest as a 64-bit integer.

Our version is about 90% the same as the reported question.

You are given a string expression made of single digits joined by + and * signs, such as "2*2+2". There are no spaces.

Add parentheses to decide the order in which the signs are applied. The digits and signs stay in their order, and each sign combines the values on its two sides. The usual rule that * goes before + does not apply here: only the parentheses decide the order, and every way of grouping is allowed.

Different groupings can give different results. Let maxValue be the largest result over all groupings and minValue the smallest. Return maxValue - minValue.

Function

maxMinusMinValue(expression: String) → long

Examples

Example 1

expression = "2*2+2"return = 2

There are two groupings:

  • 2*(2+2) = 8
  • (2*2)+2 = 6

The largest value is 8 and the smallest is 6, so the answer is 8 - 6 = 2.

Example 2

expression = "1+2*3+4"return = 10

There are five groupings:

  • ((1+2)*3)+4 = 13
  • (1+2)*(3+4) = 21
  • (1+(2*3))+4 = 11
  • 1+((2*3)+4) = 11
  • 1+(2*(3+4)) = 15

The largest value is 21 and the smallest is 11, so the answer is 21 - 11 = 10.

Example 3

expression = "1+1*0"return = 1

The groupings are (1+1)*0 = 0 and 1+(1*0) = 1.

Here doing the + first gives the smaller value, because the result is then multiplied by 0. The answer is 1 - 0 = 1.

Constraints

  • 1 <= expression.length <= 37
  • expression.length is odd. The characters at even indexes (0, 2, 4, ...) are digits from 0 to 9, and the characters at odd indexes are + or *.

More Nutanix Onsite Interview questions

  • Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
  • Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
  • Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
  • Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
  • LRU CacheONSITE INTERVIEW · Seen Aug 2025
  • Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
  • Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
  • Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
  • Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
  • Sudoku SolverONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Every Result of Grouping an ExpressionC3.ai · ONSITE INTERVIEWC3.ai interview questions
  • Prime StringSnowflake · OASnowflake interview questions
  • Sum of All ValuesFlexport · OAFlexport interview questions
  • Without WhitespacesPostman · OAPostman interview questions
  • Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Integer BreakAmazon · ONSITE INTERVIEW
  • Knight Probability in ChessboardGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Maximum Bouquet ValueOracle · ONSITE INTERVIEWOracle interview questions
  • Count Staircases with Strictly Increasing StepseBay · ONSITE INTERVIEWeBay interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW

Interview reports that mention this question

  • Nutanix Intern Debugging Round and Interviews: 25 Experiences

Nutanix interview experiences

  • Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
  • Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
  • Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
  • Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
All Nutanix interview questionsSee Nutanix hiring insights
Saved
class Solution {
    public long maxMinusMinValue(String expression) {
        // Write your code here.
    }
}
Checking account…