Listen carefully your interviewer. Don't interrupt. Even if you know the problem interviewer is talking about. Don't think about solution here!
Clarifying questions. Constraints.
Some common questions you can ask:
Draw examples. Even if you understand everything. It will help you in the end during debugging.
Generate ideas. Discuss it with with your interviewer, explain your high level approach to the interviewer even if it is a naive solution. If you are stuck, consider various approaches and explain out loud why it will/will not work. Sometimes your interviewer might drop hints and lead you towards the right path.
Start with a brute force approach, communicate it to the interviewer, explain the time and space complexity and why it is bad. It is unlikely that the brute force approach will be one that you will be coding. At this point, the interviewer will usually pop the dreaded "Can we do better?" question, meaning that they are looking for a more optimal approach. In my opinion, this is usually the hardest part of the interview. In general, look for repeated work and try to optimize them by potentially caching the calculated result somewhere and reference it later, rather than having to compute it all over again. There are some tips on tackling topic-specific questions that I dive into details below.
Estimate complexities of your solution and discuss it. Only start coding after you and your interviewer have agreed on an approach and complexity analysis and they have given you the green light.
Code.
Write your code with good coding style. Reading code written by others is usually not an enjoyable task. Reading horribly-formatted code by others makes it worse. Your goal is to make your interviewer understand the code you have written so that they can quickly evaluate if your code does what you say it does and whether it solves the given problem. Use clear variable names, avoid single letter names unless they are for iteration. However, if you are coding on a whiteboard, you might not want to use extremely verbose variable names for the sake of reducing the amount you have to write. Abbreviations are usually fine if you explain what it means beforehand.
Always be explaining what you are currently writing/typing to the interviewer. This is not about literally reading out what you are typing to the interviewer. Talk about the section of the code you are currently implementing at a higher level, explain why it is written as such and what it is trying to achieve.
While coding, if you find yourself copying and pasting code, consider whether it is necessary. If you find yourself copying and pasting one large chunk of code spanning multiple lines, it is usually an indicator that you can refactor by extracting those lines into a function and defining parameters for the differences in them. If it is just a single line you copied, usually it is fine. Do remember to change the respective variables in your copied line of code where relevant. Copy-paste errors are a common source of bugs even in day-to-day coding!
Test. Consider all corner cases, debug few inputs. Go through your examples you draw in point Look through your code from start to finish as if it is the first time you are seeing it, as if it was written by someone else and you are trying to spot bugs in it. That's exactly what your interviewer will be doing. Look through and fix any minor issues you may find.
Estimate time and space complexity of your solution.
Give the time/space complexity of your code and explain why it is such. You can even annotate certain chunks of your code with the various time/space complexities to demonstrate your understanding of your code and the APIs of your chosen programming language. Explain any trade-offs in your current approach vs alternative approaches, possibly in terms of time/space.
If interviewer is happy, you're done! :)
BUT...
What to do when stuck?
Getting stuck during coding interviews is extremely common. But do not worry, that is part of the process and is a test of your problem solving abilities. Here are some tips to try out when you are stuck:
Talk through what you initially thought might work and explain why it doesn't
This can help guide you on the right track by avoiding the pitfalls
Come up with more test cases and write them down
A pattern may emerge
Think about how you would solve it without a program
You may spot a pattern and come up with a general algorithm for it
Recall past questions related to the topic, what similar questions in the past have you encountered and what techniques did you use?
Enumerate through the common data structures and whether they can be applied to the question
Dictionaries/maps are extremely common in making algorithms more efficient
Look out for repeated work and determine if you can cache those computations
Trade off memory for speed