Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
I am playing a card game called 24. Cards ace to king are numbered 1 to 13. During a given round, I am provided four cards to play with from the shuffled pack. If the numbers from the four cards result in 24 then I win the round if I shout '24' first. How would you code a function for this?
| Tags: | code See more , See less 8 |
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
In the different steps you have (target = 24):
(target) : using cards A, B, C, D
(target - A): using cards B, C, D
(target - A - B): using cards C, D
(target - A - B - C): using card D
Repeat for each of the other cards in the step's grouping if the target isn't met, ie.
(target) : using cards B, C, D
(target - B): using cards C, D
(target - B - C): using card D
And so on.
Pass true up the stack when any of the target values reach zero.
Once you have that down, it's trivial to write the function.
Helpful Answer?
Yes |
No
Inappropriate?
2 of 3 people found this helpful
Sorting or structuring will take too much time even though it could be a big win in the general case - someone else will shout while we're moving our cards around.
Helpful Answer?
Yes |
No
Inappropriate?
In the different steps you have (target = 24):
(target) : using cards A, B, C, D
(target - A): using cards B, C, D
(target - A - B): using cards C, D
(target - A - B - C): using card D
Repeat for each of the other cards in the step's grouping if the target isn't met, ie.
(target) : using cards B, C, D
(target - B): using cards C, D
(target - B - C): using card D
And so on.
Pass true up the stack when any of the target values reach zero.
Once you have that down, it's trivial to write the function.
You only consider the + operator, which is not true in 24 points game. There are +,-,* and / 4 operators.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 0 people found this helpful
by Interview Candidate:
Consider permutations of such expressions in postfix notation.
Evaluate these permutations till you get 24.