User Story EP3: How to Practice LeetCode Problems
Published: 2024-06-14 05:12:21

[LeetCode](https://leetcode.cn/?utm_source=LCUS&utm_medium=ip_redirect&utm_campaign=transfer2china) is an online platform for coding interview preparation. The service provides coding and algorithmic problems intended for users to practice coding. LeetCode has gained popularity among job seekers and coding enthusiasts as a resource for technical interviews and coding competitions.

In the past, when using LeetCode, I preferred to write down my problem-solving thoughts using a txt editor or plain paper, and then code using VS Code. When I encountered problems that I couldn't understand, I needed to consult Google or ChatGPT. Although this method helped to clarify my thoughts, solving a single problem required frequent switching between multiple tools. This not only consumed a lot of time but also interrupted the continuity of my thinking, resulting in low overall efficiency. Therefore, I have been looking for a solution that could simplify this process and improve my practice efficiency.

Recently, a friend recommended an AI-powered Cloud IDE product called **MarsCode** to me. The overall experience is quite similar to VS Code, while also providing a ready-to-use development environment that supports debugging and running. The best part is that the built-in AI assistant can provide more accurate answers by fully understanding the context. Next, I will share my experience of using MarsCode.

## Preparation
You need an account for MarsCode, which is very easy to get. Visit the MarsCode official website at [MarsCode](https://www.marscode.com/?utm_source=devto&utm_medium=post&utm_campaign=ep3), and then register (or log in) to your account. 

MarsCode offers various development templates, allowing users to start coding without worrying about setting up the programming environment. For instance, I chose Node.js here.
<img width="100%" style="width:100%" src="https://i.imgur.com/uirQKCz.png" >
_After creating, you can code just like you do with a local IDE.⬆️_

<img width="100%" style="width:100%" src="https://i.imgur.com/VmJTnz6.png" >
_You can run the index.js by clicking the 'Run' button or executing node index.js in the terminal.⬆️_

## Experience with MarsCode
Usually, when I encounter a problem, I first jot down my thoughts on scratch paper. This time, to test MarsCode's capabilities, I jumped directly into the coding phase. Let's take a look at a classic LeetCode problem: the "[Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/description/)"!
<img width="100%" style="width:100%" src="https://i.imgur.com/NjRj0Fs.png" >
For LeetCode problems, we can write code with the assistance of MarsCode, or we can directly copy the LeetCode answer into the IDE and use AI capabilities to help us understand the problem and solution. MarsCode's AI assistant is very powerful, supporting features like code generation and code explanation. MarsCode can help me learn LeetCode in several key ways.

## Coding
When writing code, MarsCode provides three ways to assist with coding:
- While coding in the editor, the AI assistant infers your intentions based on the project context and comments, auto completing your code.
- Generate code directly in the editor through the Inline AI Chat.
- Chat with the AI assistant in the Side chat bar to get the AI to directly output the code.

For example, in the Node.js project created in the previous section, we can invoke **Inline AI Chat** in index.js.
<img width="100%" style="width:100%" src="https://i.imgur.com/7JYjHAw.png" >
<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExY2c2cWxybWJ0dWNoN29nMHM5bXZpeXRrdGYydW5pNXExY3c5bW1rZCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/clq0zrSPbg9kpIBP5j/giphy.gif" >
_When we input the problem, the AI assistant will infer the implementation of the function, even including the function call code!⬆️_

<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNjhhcnQ3eDlzYWM4dHN0cGgwZnhhNm9zNGY3aHQ3ZjYzd2E1c2RoaCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/fnAXs655UwZ9DSLtuu/giphy.gif" >
_After accepting the code, click the Run button to test the code and see the results.⬆️_
<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZDVjNGx3amlub2Vuc3JsNzd2bGV2cXJ6ZHRxZWJuZ3ZraWlsdnBlMiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/TejrN6LPZA6ypsiRdC/giphy.gif" >
_If there are parts we don't understand, we can ask the AI assistant to explain them.⬆️_

## Writing test cases
Even though MarsCode has excellent coding capabilities, whether the code is completed by AI or by ourselves, we can use test cases to verify the correctness of the code.
 
In fact, with the help of AI, our test cases can be more comprehensive. Taking the "Longest Palindromic Substring" as an example, we can use the AI assistant to generate test cases using Mocha. 
<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExYXBybzM3N3o1bmY3c3Y5d2QyeWNodjVwcmk1MTU3YXFkd25zMWVtOCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/2z6q7A2KKlN8G0GV8c/giphy.gif" >
_This way, we can easily and quickly verify accuracy through the test cases.⬆️_

## Debug
MarsCode provides templates that already support debugging capabilities. If there are any doubts about the code execution process, we can also use the Debug feature to understand the code execution step by step.
<img width="100%" style="width:100%" src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExZXNwaGFrZzFzMHMzdjRncTV0Z2Z5MzQzZHpnOWc0NWU2b2xubmF4aSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/UBhbHT9nzXJCLq6gUu/giphy.gif" >
_Open the Debug panel on the right side and click the "Start Debugging" button.⬆️_

## Exploring Better Solutions with AI
We can explore more solutions by conversing with MarsCode's AI assistant. For example, in the previous example, we used the **center expansion algorithm** for the longest palindromic substring, but another common solution is **dynamic programming**.
<img width="100%" style="width:100%" src="https://i.imgur.com/RM8ypmm.png" >
<img width="100%" style="width:100%" src="https://i.imgur.com/1CXcmRz.png" >
_If we are not familiar with dynamic programming, we can ask further: What is dynamic programming?⬆️_
<img width="100%" style="width:100%" src="https://i.imgur.com/ucrvwQR.png" >
_We can also ask the AI assistant to provide some examples of dynamic programming. ⬆️_

As you can see, on MarsCode, the AI assistant acts like a teammate, helping us gradually increase our understanding of the problem through conversation.

## Conclusion
Overall, my experience with MarsCode has been excellent. Features like hassle-free setup, free AI, and resource environments are very helpful for validating ideas and boosting efficiency. However, when practicing LeetCode, your own thought process is paramount. I recommend first organizing your ideas and then using MarsCode to solve the problems. This approach maintains the flow of thought while leveraging the powerful tools MarsCode offers, ensuring you effectively and solidly grasp algorithms and coding techniques.

Wishing you great success in your LeetCode practice!🎉