Solution to coding problem