Compass interview question

Round 2: Recursion - Back Tracking related question