Signed Digit Partitions Reaching a Target
Updated
Problem statement
Given a string num of decimal digits and an integer target, return every expression that evaluates to target by partitioning num into one or more nonempty contiguous blocks and placing either + or - between consecutive blocks.
Use every digit exactly once in its original order. A block may contain several digits. The first block may have a leading - or no sign; never write a leading +. Multiplication, parentheses and rearranging digits are not allowed.
A multi-digit block cannot start with 0; the single-digit block 0 is allowed. Expressions are distinct strings, so 0 and -0 are both valid expressions for zero.
Return the distinct expressions in ascending lexicographic order, using no spaces. Return an empty array when no expression reaches the target.
Function
signedExpressions(num: String, target: int) → String[]Examples
Example 1
num = "123456789"target = 100return = ["-1+2-3+4+5+6+78+9","1+2+3-4+5+6+78+9","1+2+34-5+67-8+9","1+23-4+5+6+78-9","1+23-4+56+7+8+9","12+3+4+5-6-7+89","12+3-4+5+67+8+9","12-3-4+5-6+7+89","123+4-5+67-89","123+45-67+8-9","123-4-5-6-7+8-9","123-45-67+89"]Multi-digit blocks such as 78 and a leading minus are permitted.
Example 2
num = "105"target = 6return = ["1+0+5","1-0+5"]Both 1+0+5 and 1-0+5 evaluate to 6. The block 05 is forbidden by the rules.
Example 3
num = "0"target = 0return = ["-0","0"]Both spellings are distinct expressions, ordered lexicographically.
Example 4
num = "12"target = 5return = []No permitted partition and sign choice reaches 5.
Constraints
1 <= num.length <= 10.numcontains decimal digits only.-10^9 <= target <= 10^9.- Use arithmetic wide enough for a ten-digit block, even when the target fits a 32-bit integer.
More Meta Phone Screen questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockPHONE SCREEN · Seen Nov 2026
- First Element Greater Than KPHONE SCREEN · Seen Nov 2026
- Count Events in Rolling Minute WindowsPHONE SCREEN · Seen Oct 2026
- Valid Word AbbreviationPHONE SCREEN · Seen Oct 2026
- Shortest Path in a Binary MatrixPHONE SCREEN · Seen Sep 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Values Exclusive to the Current UserPHONE SCREEN · Seen Sep 2026
- First Bad Version in a Monotone ArrayPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Next Number with Disjoint DigitsElevenLabs · OAElevenLabs interview questions
- Unique Digit Permutations Without Leading ZeroMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Choose the Next Minesweeper ActionRunloop · PHONE SCREEN
- Balanced Parentheses with Sequential Left DeletionsGoogle · PHONE SCREENGoogle interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIWex · PHONE SCREEN
- Expression Add OperatorsPinterest · PHONE SCREENPinterest interview questions
- Parse, Normalize, and Combine PolynomialsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Word Break IIInMobi · PHONE SCREENInMobi interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences