There were three men in a line (facing forward) so they can only see the man (or men) in front of them. There are also five hats, two black and three white. Each man is wearing one of the hats.
When the man at the back of the line was asked "Do you know your hat color?", he replied "No". When the man that was second in line was then asked "Do you know your hat color?" he replied "No". When the man at the front of the line was asked "Do you know your hat color?" he replied "Of course".
What color of hat was the man at the front of the line wearing?
The first person is having a white cap.
The third person in the line will know which hat he himself is wearing if the both the persons standing in front of him have black caps. Since he replies in negative, this implies that one of the two persons standing in front of him is wearing a white cap and other the black cap.
The second person can see the hat which the first person is wearing. If the first person is wearing black cap his answer would be yes, because he knows that one of them is wearing white hat as implied by the third persons reply to the question.
hence the man in front of the line was wearing white cap.