- It's not the best solution that counts, it's how you problem solve
Steps to help you problem solve:
- Verify the problem and the probem inputs eg: whether an array of data is of a fixed length, if the order matters etc
- Think about the problem and derive a verbal solution (this doesn't need to be the best or final solution). Experience and practice helps with this
- Write down a first version in code (this could be pseudo-code)
- The first version could be a simple, greedy approach (just ensure you verify the results / try different inputs)
- Does this solution catch all the test cases?
- Derive the time complexity for the solution and see if you can improve it eg: can you store intermediate values?
- Split the problem into smaller sub-problems eg: split an array into smaller chunks possibly combined with recursion
- Use
console.log()
or breakpoints to verify what's in your (temporary) variables eg: if you explain why you're checking variables then it is fine. It's better to check early than to go down the wrong path, especially with recursion. - Use helper variables, eg: helper arrays to store intermediate variables. Don't worry about space complexity at this point, the bottom line is trying to make your code as easy to understand as possible.
- Practice by diving into common algorithms and interview questions