We behave more freely when we are alone, than when we are being watched.
And that's a good thing, because we are social beings, and we expect others to behave better when they are in our presence, and we are expected to behave better when we are in others' presence.
A building looks ugly during construction, but only the final product is judged. An author could make hundred little corrections and edits to his book before publishing it - again, they are not judged until their final draft. That gives them a lot of freedom to make mistakes during the journey.
But during an interview, I tend to freeze, because we are being judged both by our approach and our results. If I am not free to make the silliest of mistakes (1+1 = 2.5), then my mind is not free to arrive at the solution. In fact, most people arrive at the best result faster by making more mistakes, than by forcing them to make less mistakes by thinking through the approach, which basically throws a wrench in their brain.
How do you overcome the fact that a person, who already knows the answer to the question, is sitting across you and judging you?
Can you ask the interviewer to leave the room, or maybe you can go and work in solitude in another room (without computer/books/phone of course) ?
Rarely, if ever, is anyone asked to present a demo to large audience without already rehearsing it, in real life.
Rarely, if ever, is anyone asked to code a software while someone is watching over their shoulder and judging, unless they've already done it before, in real life.
Peple might say 'Building software is a collaborative effort. If you can't think when people are watching, maybe this isn't the best suited field.'
The keyword is collaborative. If both people don't know the answer, I have no problem thinking out loud. That's the real-life scenario. There is no real-life scenario where one person already knows the answer, and is 'testing' the other person.
So, what real-life skill exactly is an interview testing by asking candidates to code up a solution to a problem they've not seen before, WHILE BEING WATCHED AND JUDGED ? Give a candidate a problem, leave the room, and come back after 15 minutes to discuss approaches.