PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Pinterest

Insert parentheses to minimize expression value

Last updated: Mar 29, 2026

Quick Overview

This question evaluates string parsing, arithmetic expression modeling, and combinatorial optimization skills by requiring reasoning about how digit concatenation and parentheses placement alter multiplicative factors and the final numeric value.

  • hard
  • Pinterest
  • Coding & Algorithms
  • Machine Learning Engineer

Insert parentheses to minimize expression value

Company: Pinterest

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

You are given a string expression of the form `A+B`, where `A` and `B` are non-empty strings of digits (no digit is `'0'`). You must insert exactly one pair of parentheses `(` and `)` into the expression such that: - The parentheses form a valid expression and **must include the plus sign**. - Any digits **outside** the parentheses remain adjacent to the parentheses and are interpreted as **multiplication** with the parenthesized sum. More precisely, choose a split of `A` into `A = L1 + L2` (concatenation) and a split of `B` into `B = R1 + R2` such that the resulting value is: - If `L1` is empty, treat it as multiplicative factor 1; otherwise factor is integer value of `L1`. - If `R2` is empty, treat it as multiplicative factor 1; otherwise factor is integer value of `R2`. Resulting value = `factor(L1) * (int(L2) + int(R1)) * factor(R2)`. Return the expression string with parentheses inserted that yields the **minimum** possible value. If multiple answers tie, return any one. Example: input `"435+122"` could become something like `"4(35+12)2"` (not necessarily optimal).

Quick Answer: This question evaluates string parsing, arithmetic expression modeling, and combinatorial optimization skills by requiring reasoning about how digit concatenation and parentheses placement alter multiplicative factors and the final numeric value.

Related Interview Questions

  • Design Hierarchical Permission Checks - Pinterest (medium)
  • Implement weighted random choice - Pinterest (medium)
  • Solve five hard algorithm problems - Pinterest
  • Sample a string by real-valued scores - Pinterest (hard)
  • Find First Prefix-Matching Word - Pinterest (medium)
Pinterest logo
Pinterest
Feb 9, 2026, 12:00 AM
Machine Learning Engineer
Take-home Project
Coding & Algorithms
4
0

You are given a string expression of the form A+B, where A and B are non-empty strings of digits (no digit is '0'). You must insert exactly one pair of parentheses ( and ) into the expression such that:

  • The parentheses form a valid expression and must include the plus sign .
  • Any digits outside the parentheses remain adjacent to the parentheses and are interpreted as multiplication with the parenthesized sum.

More precisely, choose a split of A into A = L1 + L2 (concatenation) and a split of B into B = R1 + R2 such that the resulting value is:

  • If L1 is empty, treat it as multiplicative factor 1; otherwise factor is integer value of L1 .
  • If R2 is empty, treat it as multiplicative factor 1; otherwise factor is integer value of R2 .

Resulting value = factor(L1) * (int(L2) + int(R1)) * factor(R2).

Return the expression string with parentheses inserted that yields the minimum possible value. If multiple answers tie, return any one.

Example: input "435+122" could become something like "4(35+12)2" (not necessarily optimal).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Pinterest•More Machine Learning Engineer•Pinterest Machine Learning Engineer•Pinterest Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.