Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Target From Number String

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Target From Number String. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Veeva Vault configuration data can encode numeric expressions as a single digit string. Given a string of digits and a target integer, split the string into exactly three nonempty numbers without changing digit order, insert one operator between adjacent numbers, and return an equation that evaluates to the target.

Allowed operators are +, -, and *. Multiplication follows normal arithmetic precedence. A number may not contain leading zeroes unless it is exactly 0. Return any valid equation as a string in the form a<op1>b<op2>c=target. Return None if no valid equation exists. The function should not print directly, which makes the result easier to test.

Formal Specification

  • Input: digits, a string containing only characters from 0 through 9, and target, an integer.
  • Output: A valid equation string, or None when no solution exists.
  • The three numbers must be contiguous, nonempty substrings whose concatenation equals digits.

Constraints

  • 3 <= len(digits) <= 300
  • digits contains only characters from 0 through 9
  • -10^18 <= target <= 10^18
  • Numbers may not have leading zeroes unless the number is exactly 0
  • The three numbers must be nonempty contiguous substrings
  • Return any valid equation, or None if no equation exists

Function Signature

def build_expression(digits, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output