Optimal solution - Can I change my approach if I figure out a better way during the interview?

In my last interview, I started approaching a technical question with a brute force approach. However, 5 or 6 minutes in, I realized that there was a more optimal approach. I felt too nervous to change my approach so I continued with my brute force one. What should I have done?

You can start tackling a problem with a brute force approach. However, if you discover that there is a more optimal approach while coding your brute force one, especially early on in the question, you can signal to your interviewer that you have a more optimal approach in mind. Ask them if you should continue coding with your brute force approach or if you can pursue the optimal one.

With the brute force approach, there are no real shortcuts, as it relies on computing power to try all possibilities until the solution to the problem is found. Your interviewer might want to watch you walk through each possibility regardless, or they may allow you to pursue the more optimal approach. They might even ask you to explain the optimal approach verbally and continue writing out the brute force approach. No matter what you decide, explain your thought process and defer to the interviewer’s decision. Ask them yes/no questions to understand what they want to see and what they are asking for. That way, you can demonstrate your communication and problem solving skills.