As someone ashamed of themselves with the mistakes they have made in their mock interviews, I decided to create a formula so that I dont miss out on any steps. Here are 10 steps to solve a problem.
Please suggest edits and additional information.
Understand the question - Rephrase the problem in your own words and use an example. State any assumptions you are making. (I assume that there are not spaces at the beginning of the string, I assume there are no special characters etc)
Clarifications - Can I use the language’s built-in library to reverse a string in this problem? Is the input sorted? Do I have to do this in-place? Does negative sign affect the answer? If 121 is a palindrome, is -121 too? How do I know if an input is invalid? What are the constraints on the inputs?
Get an Example - Write down a sample input and it's out as a comment. This can be used while explaining your approach. And also for writing the function signature. (You won't return a string when asked for a char array)
Sample test cases - what if the input is “-+-543” ? What if the input is empty? What if it's too long?
DataStructure - Will you use an additional data structure? Is your chosen data structure okay with the time complexity asked for? Is your data structure suitable for your algorithm?
Algorithm and approach - Use pseudo code if needed and write down your approach in comments while using an example to explain it. Explain it thoroughly and engage the interviewer. Discuss the time and space complexities. Ask if you should optimize for space or time.
Code - By this time, your interviewer should be satisfied with your approach. If they aren't, see what's missing. Start writing the code while talking out loud about what you are doing. Code should be readable and consistent. Variable names should make sense. Don't be in a hurry.
Dry Run - Use the test cases you formulated with the help of your interviewer in step-4. Comment on each line what the variable’s value would be. Make sure that there are no bugs. If you encounter a bug, correct the code. Once satisfied, ask the interviewer if it looks fine to them.
Run the code - If the platform gives you the chance to run your code, and if the interviewer asks, run the code and the test cases. If you see any bugs at this point, go back to your approach and correct it after discussing it with your interviewer.
10.Discuss - Discuss what you might want to change/improve if you had more time. If there are any follow-up questions, move on to them.