As a general idea with any of these games, you want to look at winning and losing positions. A winning position is one where player A has some move which leads to a losing position for player B. A losing position for player B is one where every move leads to a winning position for player A. As a cs student you will recognise an obvious recursion here. If you can find some property which is preserved for all winning positions then you have an easy way to identify a win. Otherwise you may need to brute force it.
For this game the simplest winning position is no apples in any compartment, which means that the previous player ate the last one and lost. The simplest losing position is one apple, since the only possible move leads to the previous winning position.
The next winning positions are just those where there is some move which puts the game into one of the losing positions. Either have one compartment with more than one apple and eat all but one; or two compartments, where one has exactly one apple and you take everything from the other.
What are the next losing positions? Two apples in each of two compartments, or one apple in each of three compartments.
Repeat this process until you get to the case that you are interested in.
On the way, you will find that some patterns emerge. For example, equal numbers (>1) in two compartments is a losing position because you have three choices.
- Eat all apples in one compartment. Opponent takes all but one from the other.
- Eat all but one. Opponent eats all apples from the other.
- Anything else. Opponent eats the same number as you, leaving two compartments with the same number in each, which is the same scenario you started from.
Once you have played with this strategy, then read up on nim, as others have suggested, since the universal strategy is unlikely to be apparent.