1) In surah (KAHF) there is a story of prophet musa and prophet khidr (peace be upon them). In the Story prophet khidr (pbuh) killed a boy. Why he did such?

2) If allah knew that the person would do bad things then why did he let him exist at all?

1) Because Allah knew that the boy would grow up to be evil and cause mischief, and so Allah had him die young so he wouldn’t do that evil and also so he wouldn’t cause suffering to his righteous parents. There are many ways by which a person dies. Sometimes it’s a disease, sometimes an accident, and in this story, Allah commanded one of his servants to kill this boy to avoid that black future.

2) To try his parents. It was a divine trial for them