5 Steps to Tackle The Coding Challenges
Tags: leetcode, interview
Published: 2020-09-10 08:30:07

<blockquote>
In the past six months, I have spent lots of time on solving coding questions on LeetCode and successfully passed a few companies' coding interviews. I have learned a bunch of experiences along the journey. So in this article, I want to share some resources I used to practice my skills. And the steps that I implement when solving a coding challenge.
</blockquote>


## Agenda
  * [1. Preparing our own environment](#agenda-1)
  * [2. ❌ Brute-force solution → ⭕️ Have our first solution](#agenda-2)
  * [3. Adding extra test cases for the edge cases](#agenda-3)
  * [4. Optimize our solution](#agenda-4)
  * [5. Optimize the syntax](#agenda-5)

### 1. Preparing our own environment <a name="agenda-1"></a>
Solving the coding questions on our own IDE will be way more efficient than coding on the website like LeetCode. I use Node.js and [Jest](https://jestjs.io/docs/en/getting-started) to practice all the LeetCode questions on my laptop.  
- Some plugins like prettier, linter, and code snippet can help us get rid of many tedious errands.
- Jest watch mode can automatically retry our solution. 
- Moreover, it's also easier to analyze our algorithm in our local environment, like setting the breakpoint to check the variable value step by step.   

❗️Some coding test platforms might not allow you to just copy/paste your final answer, because they need to see your solving process. Remember to check beforehand when working on a coding interview.❗️


### 2. ❌ Brute-force solution → ⭕️ Have our first solution <a name="agenda-2"></a>
I have seen some articles suggest starting with a brute-force solution. This is a good tip when we can't figure out any solution. But in reality, I think most of the time we can have our first answer which might not be the best solution but it's better than brute-force.  
When working on our first solution, we usually will find many possible ways to optimize it. But it's totally fine to add some `TODO` comments and optimize them later. To keep the first approach simple will bring lots of benefits that I will mention in the next paragraph.  
This idea also works on our day-to-day coding, `Premature optimization is the root of all evil`.


### 3. Adding extra test cases for the edge cases <a name="agenda-3"></a>
LeetCode will check all the different scenarios when we submit the answer. But in a real coding test, we have to add some edge cases on our own.
For me, most of the time, my first approach won't be able to pass all the edge cases. This is the benefit to keep our first solution simple. It will be way easier to figure out how to modify our solution if we choose to optimize later.  


### 4. Optimize our solution <a name="agenda-4"></a>
Once we pass all the test cases, it's time to optimize our solution.    

#### (1) Algorithm & Data structure 
The most common approach to optimize the solution is to apply some algorithms or data structures. And which algorithm & data structure should be applied to a specific question will depend on our experience. Therefore, it's very important to understand the pros and cons of each algorithm & data structure, rather than just memorize the best answer. 
If you're not familiar with algorithm & data structure, there are two great resources, each of them contains about 50 questions. If you can finish either one of them, I think you will be totally fine to start your first coding interview.  
- [Tech Interview Handbook - Best Practice Questions](https://yangshun.github.io/tech-interview-handbook/best-practice-questions/): all the questions if come from LeetCode, so it's easy to practice and reference some different answers on the discussion board.   
- [bytebybyte - 50-questions](https://www.byte-by-byte.com/50-questions/): the author provides the video explanation for all the questions, it might be easier to understand by some people. And the solutions are written in Java. You will have to pick up some basic concepts about this language if you don't have any experience before.  

#### (2) The trade-off between time and space complexity 
Sometimes, choosing the best algorithm & data structure will be the trade-off between time and space complexity. Therefore, we have to understand the limitations of the question. If it's a whiteboard test, it's good to ask some questions to the interviewers to clarify what is more important (time (performance) or space (memory usage)) in this specific problem before we make the final decision.   


### 5. Optimize the syntax <a name="agenda-5"></a>
After we came out with a decent solution, another way to optimize our answer is through the syntax. Most of the time, this will depend on the language you're using. Taking Javascript as an example, Javascript is a very flexible language, it's common to have multiple approaches for the same purpose. When working on a real application, we tend to choose the one that is easier to maintain or use linter or other tools to make sure we follow the same convention. But in the coding challenge, I usually will try to optimize the performance as best as I can.  

- What's the fastest way to iterate and duplicate the object/array? [reference](https://medium.com/hackernoon/3-javascript-performance-mistakes-you-should-stop-doing-ebf84b9de951)
- `var`, `let` & `const`, if we only care about performance, which one is the fastest?
- [tail call optimization](https://stackoverflow.com/questions/310974/what-is-tail-call-optimization)
- ...

We can use the library ([benchmark](https://benchmarkjs.com/)) or Javascript API ([performance](https://nodejs.org/api/perf_hooks.html)) to test all the different approaches. Then we will have a better understanding of the performance of different syntax.


## Conclusion
That's all, thanks for reading, I hope these tips will help you tackle the coding challenge and pass the interview. Please feel free to leave the comment if you have any ideas about how to solve coding challenge questions.

---

## Reference
  * Cover Image: Image by <a href="https://pixabay.com/users/qimono-1962238/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1872665">Arek Socha</a> from <a href="https://pixabay.com/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1872665">Pixabay</a>
  * [Jest](https://jestjs.io/docs/en/getting-started)
  * [LeetCode](https://leetcode.com/)
  * [Tech Interview Handbook](https://yangshun.github.io/tech-interview-handbook)
  * [bytebybyte](https://www.byte-by-byte.com)
  * [3 JavaScript Performance Mistakes You Should Stop Doing](https://medium.com/hackernoon/3-javascript-performance-mistakes-you-should-stop-doing-ebf84b9de951)
  * [What is tail call optimization?](https://stackoverflow.com/questions/310974/what-is-tail-call-optimization)
  * [benchmark.js](https://benchmarkjs.com/)
  * [performance api](https://nodejs.org/api/perf_hooks.html)