Algorithms exercise