PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skill in constructing and evaluating arithmetic expressions, handling numerical stability (including division-by-zero and floating-point tolerance), and reasoning about combinatorial permutations of numbers and operators.

  • Medium
  • Snapchat
  • Coding & Algorithms
  • Machine Learning Engineer

Determine and print expression to reach target

Company: Snapchat

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

You are given four numbers and a target T (e.g., 24). Using +, −, ×, ÷ and parentheses, and using each number exactly once, determine whether you can form an expression that evaluates to T. Return a boolean. Follow-up: if it is possible, output one valid expression string. Clarify how to handle division by zero and floating-point tolerance; discuss algorithmic approach (e.g., backtracking with pruning), complexity, and how you would extend the method to N numbers or different operator sets.

Quick Answer: This question evaluates skill in constructing and evaluating arithmetic expressions, handling numerical stability (including division-by-zero and floating-point tolerance), and reasoning about combinatorial permutations of numbers and operators.

You are given a list of numbers `nums` and a target value `target`. Using the four arithmetic operators `+`, `-`, `*`, `/` and parentheses, and using each number in `nums` exactly once, determine whether you can build an expression that evaluates to `target`. Return `true` if such an expression exists, otherwise `false`. The classic instance of this problem is the "24 game" (four numbers, target 24). Rules and clarifications: - Each number must be used exactly once; numbers may be combined in any order. - Any intermediate result may be a non-integer (division can produce fractions), and intermediate/final results are compared with a floating-point tolerance of `1e-6`. - Division by zero is not allowed: you may only divide by a value whose absolute value exceeds the tolerance. - An empty input cannot reach any target, so return `false`. - A single number `x` reaches `target` only if `x` equals `target` (within tolerance). This generalizes naturally to N numbers and to arbitrary operator sets; the reference approach is backtracking that repeatedly collapses two values into one until a single value remains.

Constraints

  • Each number in nums must be used exactly once.
  • Allowed operators: + - * / and parentheses (any grouping).
  • Intermediate and final values are compared with a floating-point tolerance of 1e-6.
  • Division is only permitted when the divisor's absolute value exceeds 1e-6 (no division by zero).
  • An empty nums list returns false.

Examples

Input: ([4, 1, 8, 7], 24)

Expected Output: True

Explanation: (8 - 4) * (7 - 1) = 4 * 6 = 24, so the target is reachable.

Input: ([1, 2, 1, 2], 24)

Expected Output: False

Explanation: No combination of 1, 2, 1, 2 with + - * / reaches 24.

Input: ([3, 3, 8, 8], 24)

Expected Output: True

Explanation: 8 / (3 - 8 / 3) = 8 / (1/3) = 24, the well-known hard 24-game case.

Input: ([1, 5, 5, 5], 24)

Expected Output: True

Explanation: 5 * (5 - 1/5) = 5 * 4.8 = 24, exercising fractional intermediate values.

Input: ([1, 1, 1, 1], 24)

Expected Output: False

Explanation: Only 1s cannot be combined to reach 24.

Input: ([2, 7, 11, 15], 9)

Expected Output: True

Explanation: A combination such as (15 - 11) + 7 - 2 = 9 reaches the target.

Input: ([5], 5)

Expected Output: True

Explanation: A single number reaches the target only when it equals the target.

Input: ([], 24)

Expected Output: False

Explanation: Empty input cannot form any expression, so the answer is false.

Input: ([6, 6, 6, 6], 24)

Expected Output: True

Explanation: 6 + 6 + 6 + 6 = 24.

Input: ([1, 2, 3], 7)

Expected Output: True

Explanation: Generalizes beyond four numbers: 1 + 2 * 3 = 7.

Hints

  1. Think of the problem as repeatedly replacing two values by the result of one operation, until a single value remains; if that value equals the target (within tolerance) you've found an expression.
  2. Try every ordered pair (a, b) so that both a-b and b-a, and a/b and b/a, are explored. Guard division by checking |b| > 1e-6.
  3. Use a small epsilon (1e-6) for all equality and division-by-zero checks, because intermediate division can produce non-integers.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.

Related Coding Questions

  • Determine Whether Courses Can Be Completed - Snapchat (medium)
  • Solve Decimal Coin Change - Snapchat (medium)
  • Find Maximum Island Perimeter - Snapchat (medium)
  • Solve Three Algorithmic Tasks - Snapchat (hard)
  • Implement a Timestamped Counter - Snapchat (medium)