Updated function to solve exam question