I've been practicing my Data Structures and Algorithms on LeetCode for a few months now, and it's an awesome platform. The quality of the questions is generally great, there's very nice explanations for most of the solutions, and there's a very motivated and active community around all of it. Overall, LeetCode is a great platform.
BUT, I had two slight problems with it:
I personally never understood the idea of writing your code and testing it on a web editor. I mean, I have my own programming environment tailored to exactly how I like to write and test my code. And yes, granted that I'll have to use a google doc or something for my interviews, but I still prefer to have control over how I practice.
I'd like to keep and test my solutions locally, in a version-controlled manner, so that I can come back to them later, search through them easily, make changes over time, and to just make sure I never lose my code.
So for this, I created my own LeetCode workflow and local testing library:
python-leetcode-runner
And it has made me many times more productive in solving leetcode problems.
An example workflow
I start with opening my github repo where I store all of my leetcode problems:
-
Then I copy over the given example test cases into a variable called
tests
:
Then it's time to write a solution and test it using the
pyleet
command given by my testing library:Uh oh. Looks like I missed an edge case. Let's fix the code:
And now all tests pass. Now you can copy your code file (yes, with the tests
and all) straight into leetcode's editor and submit it. No more silly "Wrong Answers".
Advanced use cases
Another example: Linked List cycle
Now this has an issue: We need to transform the given input into the linked list data structure, before running our solution.
In other questions for example, you don't just have to match expected output with function output. Like sometimes it might ask you to modify a list in-place, or some might have answers where the order of the output doesn't matter, etc.
For that case, you can provide your own custom validator function.
A validator
is a function that receives 3 arguments:
-
method
: your leetcode solution function -
inputs
: your test inputs tuple -
expected
: your expected test output value
Validator solution example
- Head to the question.
- Copy the problem title, format it, and paste the sample code and examples.
- Now to be able to run the tests locally, we need to write our own code to convert the array into a linked list:
- Now we can solve the question:
- Running the tests:
And sure enough, we passed all the test cases. Now simply copy the entire code over to leetcode, and:
You can find the complete solution here.
Extra tips
- I use mypy to run static type checking on my code, which ensures stuff like no runtime Null Pointer Exceptions.
- I've created a bunch of code snippets that auto-fill the test cases, the validator function, and the assert statements. These snippets can be used in VSCode as of now. More info on the github page.
- My solutions to all of the problems are stored in this github repository.
Footnotes
Currently this package only works with Python solutions. But if required, it can be extended to use any language at all. If you're interested in working on other language support, do let me know.
Thanks for reading, I hope this helps you be more productive. β¨
Top comments (2)
Thanks for writing this article.
I picked up a few good tips from you. Namely
-storing my Leetcode problems and solutions on Github; and
-using python_leetcode_runner
I've been Leetcoding for about a month now and it's been great. Very challenging but I can
tells that my skills are becoming sharper.
Leetcoder,
youtube.com/playlist?list=PLctj_N-...
Glad to see someone actually using the project, I think it's really useful :)