Optimal Division

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an array nums of integers, write them in order with / between neighbours, e.g. [2,3,4] gives 2/3/4, which is evaluated left to right. You may add parentheses anywhere to change the evaluation order. Return the expression, as a string, that achieves the **maximum** value. The string must contain no redundant parentheses: only add parentheses that change the evaluation, and never wrap a single number or the entire expression. With all values at least 2, this expression is unique.

Examples

Input: nums = [1000,100,10,2]
Output: "1000/(100/10/2)"
1000/(100/10/2) = 1000/0.5 = 2000, the largest achievable value.
Input: nums = [2,3,4]
Output: "2/(3/4)"
2/(3/4) = 8/3, larger than 2/3/4 = 1/6.
Input: nums = [2]
Output: "2"
A single number needs no operators.

Constraints

  • 1 <= nums.length <= 10
  • 2 <= nums[i] <= 1000
  • There is exactly one optimal expression for every valid input

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems