## DEV Community π©βπ»π¨βπ» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Andrew

Posted on

# 5 Steps to Tackle The Coding Challenges

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.

## Agenda

### 1. Preparing our own environment

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 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

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

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

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: 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: 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

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
• `var`, `let` & `const`, if we only care about performance, which one is the fastest?
• tail call optimization
• ...

We can use the library (benchmark) or Javascript API (performance) 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.