DIY: 24 Game

Solve the interview question "24 Game" in this lesson.

Problem statement

Suppose you are given an integer list, cards, of length 4. Each one of the four cards contains a number in the range [1, 9]. You have to check if an arithmetic expression formed by an interleaving of these cards with the operations '+', '-', '*', and '/' evaluates to 24.

You are restricted by the following rules:

  • The division operator '/' represents real division, not integer division.
  • Every operation done is between two numbers. In particular, we cannot use '-' as a unary operator.
  • You cannot concatenate numbers together For example, if cards = [1, 2, 1, 2], the expression 12 + 12 is not valid.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.