Decide Whether Four Cards Can Form Twenty-Four

Quick Overview

Decide Whether Four Cards Can Form Twenty-Four. Use all four cards exactly once in a parenthesized expression with binary +, -, *, /.

Decide Whether Four Cards Can Form Twenty-Four

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Use all four cards exactly once in a parenthesized expression with binary +, -, *, /. Division is real division. Unary minus and concatenating card digits are forbidden. Return whether any permitted expression has exact value 24. ```cpp class Solution { public: bool judgePoint24(vector<int>& cards); }; ``` ### Examples **Example 1:** ```text cards = [3,3,8,8] Output: true Explanation: 8/(3-8/3) = 24 uses real division. ``` **Example 2:** ```text cards = [1,1,1,1] Output: false Explanation: A pair of ones can only produce 0,1,2 (or undefined division); combining the resulting two pairs reaches at most 4, and the three-plus-one parenthesizations likewise cannot reach 24. ``` ### Constraints - Exactly four card entries are supplied; each integer lies in 1..9. ### Clarifying Questions Use every card once, support noninteger intermediates and reject division by zero. ```hint Read the boundary condition Can combining two available values recursively cover every parenthesized binary expression? ``` ### Contract considerations Use every card once, support noninteger intermediates and reject division by zero.

Overview: Decide Whether Four Cards Can Form Twenty-Four. Use all four cards exactly once in a parenthesized expression with binary +, -, *, /.

Google logo
Google
Jul 3, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Use all four cards exactly once in a parenthesized expression with binary +, -, *, /. Division is real division. Unary minus and concatenating card digits are forbidden. Return whether any permitted expression has exact value 24.

class Solution {
public:
    bool judgePoint24(vector<int>& cards);
};

Examples

Example 1:

cards = [3,3,8,8]
Output: true
Explanation: 8/(3-8/3) = 24 uses real division.

Example 2:

cards = [1,1,1,1]
Output: false
Explanation: A pair of ones can only produce 0,1,2 (or undefined division); combining the resulting two pairs reaches at most 4, and the three-plus-one parenthesizations likewise cannot reach 24.

Constraints

  • Exactly four card entries are supplied; each integer lies in 1..9.

Clarifying Questions Guidance

Use every card once, support noninteger intermediates and reject division by zero.

Contract considerations

Use every card once, support noninteger intermediates and reject division by zero.