Algorithms questions and solutions in Java