TCS Coding Problem 8