Check whether four cards can be combined into 24 with +, -, * and /

Quick Overview

Given four cards numbered 1 to 9, decide whether some expression using each card once with +, -, *, / and parentheses equals 24 under real division. Tests exhaustive search with backtracking, covering every expression shape, and handling floating-point error.

Check whether four cards can be combined into 24 with +, -, * and /

Company: Point72

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are given an integer array `cards` of length 4. Each card holds a number in the range `[1, 9]`. Arrange the numbers on these cards in a mathematical expression using the operators `+`, `-`, `*`, `/` and the parentheses `(` and `)` to get the value 24. Return `True` if some valid expression evaluates to 24, and `False` otherwise. ### Function Signature ```python def judge_point_24(cards: list[int]) -> bool: ``` ### Rules - Use every card exactly once. - The division operator `/` is real division, not integer division. For example, `4 / (1 - 2 / 3) = 12`. - Every operation is between two numbers. You cannot use `-` as a unary operator. For example, with `cards = [1, 1, 1, 1]`, the expression `-1 - 1 - 1 - 1` is not allowed. - You cannot concatenate numbers. For example, with `cards = [1, 2, 1, 2]`, the expression `12 + 12` is not valid. ### Constraints - `len(cards) == 4` - `1 <= cards[i] <= 9` ### Examples ```text Input: cards = [4, 1, 8, 7] Output: True ``` `(8 - 4) * (7 - 1) = 4 * 6 = 24`. ```text Input: cards = [1, 2, 1, 2] Output: False ``` No arrangement of 1, 2, 1, 2 reaches 24. The largest value these cards can produce is `(1 + 2) * (1 + 2) = 9`. ```text Input: cards = [1, 5, 5, 5] Output: True ``` `5 * (5 - 1 / 5) = 5 * 4.8 = 24`. The intermediate value 4.8 only exists because division is real division.

Overview: Given four cards numbered 1 to 9, decide whether some expression using each card once with +, -, *, / and parentheses equals 24 under real division. Tests exhaustive search with backtracking, covering every expression shape, and handling floating-point error.

Point72 logo
Point72
Nov 24, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array cards of length 4. Each card holds a number in the range [1, 9]. Arrange the numbers on these cards in a mathematical expression using the operators +, -, *, / and the parentheses ( and ) to get the value 24.

Return True if some valid expression evaluates to 24, and False otherwise.

Function Signature

def judge_point_24(cards: list[int]) -> bool:

Rules

  • Use every card exactly once.
  • The division operator / is real division, not integer division. For example, 4 / (1 - 2 / 3) = 12 .
  • Every operation is between two numbers. You cannot use - as a unary operator. For example, with cards = [1, 1, 1, 1] , the expression -1 - 1 - 1 - 1 is not allowed.
  • You cannot concatenate numbers. For example, with cards = [1, 2, 1, 2] , the expression 12 + 12 is not valid.

Constraints

  • len(cards) == 4
  • 1 <= cards[i] <= 9

Examples

Input: cards = [4, 1, 8, 7]
Output: True

(8 - 4) * (7 - 1) = 4 * 6 = 24.

Input: cards = [1, 2, 1, 2]
Output: False

No arrangement of 1, 2, 1, 2 reaches 24. The largest value these cards can produce is (1 + 2) * (1 + 2) = 9.

Input: cards = [1, 5, 5, 5]
Output: True

5 * (5 - 1 / 5) = 5 * 4.8 = 24. The intermediate value 4.8 only exists because division is real division.