Facebook Hacker Cup 2015 Qualification Round Solutions
Facebook recently organized the qualification round of Hacker Cup 2015. They posed some interesting problems and anyone who could get at least one problem right can move to the next round.
I managed to get a rank of 217, with a perfect score of 100. I have posted my solutions below with a little bit of commentary. You can access the problems here.
Cooking the Books (15 points)
This was the easiest question. Since the constraints were so small, it suffices to use brute force and try all possible swaps. Care has to be taken however to make sure that a number never starts with 0.
Max number of digits in the input number is 9. Therefore we have 36 possible swaps. For 100 test cases, we have a limit of 3600. Therefore we can easily apply brute force.
|
|
New Year’s Resolution (30 points)
On the surface, it looks like a ghastly version of subset sum problem which happens to be NP-complete.
However, if we look at the constraints of the problem we will notice that the max number of food items is 20. Therefore, total possible number of food choices is 2 ^ 20 = 1048576. Therefore, we will simply iterate over the power set of all food items and try to find a combination that sums exactly to the macro nutrients we need. I have covered power set generation in a separate blog post.
|
|
Laser Maze (55 points)
Let us forget about the lasers for now. This restricted version of the problem can be solved using BFS. Adding lasers brings two complications:
- We need to check for safe spaces. At any point in time, we cannot occupy a space which is being targeted by a laser.
- The board’s state is not a function of just player position anymore. It is now dependent on laser turrets configuration as well. Fortunately for us, all lasers turn simultaneously and can have only 4 possible states.
Taking these 2 modifications into consideration, we can now write a modified BFS.
|
|
As always, comments are welcome.