Decide Whether Four Cards Can Make 24 With Arithmetic
Last updated: November 29, 2026
Quick Overview
Given four cards with values 1 to 9, decide whether you can combine them using +, -, *, / and any parenthesization to get exactly 24. Division is real (not integer) division.
0
0
0 solved
Given four cards with values 1 to 9, decide whether you can combine them using +, -, *, / and any parenthesization to get exactly 24. Division is real (not integer) division.
A Google onsite favorite that tests search over expression trees rather than over strings. The interviewer checks the pick-two-and-combine recursion, floating-point tolerance or exact fractions, and avoiding division by zero.
What the Interviewer Expects
- Recursively picks any two numbers, replaces them with each possible result, and recurses on the smaller list
- Handles both a - b and b - a, and a / b and b / a
- Uses exact Fractions or a tolerance for the final comparison
- Guards against division by zero
- Explains why this covers every parenthesization
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return one expression string that makes 24.
- Generalize to n cards and a target T. How does the cost grow?
- How would you dedupe symmetric work for commutative operators?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of 4 ints in 1..9.
- Output: True if some expression using each card exactly once equals 24.
- Example:
[4, 1, 8, 7]is True: (8 - 4) * (7 - 1).[1, 2, 1, 2]is False. - Unary minus ...
High-Level Design
Any expression over the cards is a binary tree. Its last-evaluated operation is at the root, but working bottom-up is easier: some pair of numbers is combined first. So:
- If one number remains, che...