I'm new to interviews and am unsure how to respond if I'm asked about optimizing an algorithm but can't figure one out. For example, if I propose a brute force solution and the interviewer prompts me for an optimization, what should I do next? Should I just go ahead and code the brute force solution, or is it better to ask the interviewer for hints on how to optimize the solution?
3 Answers
If you're stumped on optimization, it’s fine to say so! Just explain that you’d research it if you had more time. The goal is to demonstrate you can communicate well and think critically. Since you're applying for entry-level positions, you don’t have to nail every question perfectly right away.
Writing a correct but not optimal solution is definitely better than not coding at all! Just call it "straightforward" and focus on solving the problem before worrying about optimizations. If you have time, review the runtime of your solution, and only then think about potential optimizations. It’s about showing you can think critically and resolve issues, not just present fancy solutions.
Remember, the interview is more like a discussion than a test. Start by outlining your brute force approach and its time complexity. If they ask for optimizations, share your thoughts on how you might tackle it, even if you're not sure. For example, suggest sorting inputs before processing. If you hit a wall, it’s cool to admit it and ask for tips on optimizing. It shows you’re open to guidance.

Exactly! Even if your first attempt isn't perfect, being able to analyze it shows you're on the right track. It's better to show your thought process than to freeze up because you're worried about perfection.