Do LeetCode

Austen’s a CEO, so I can’t answer him either. But for you Mr. programmer, I can:

Do LeetCode

Why LeetCode

A musician plays notes. Practicing scales is note playing without the fluff. Ergo, he practices scales to get better at playing notes.

You program. Leetcode is programming without the fluff. Ergo, you do leetcode to program better.

To get the most out of your coding practice, follow these simple rules.

Use your work language

Violinists play scales on violins, not pianos.

Python programmers do LeetCode in Python.

Don’t use C++ for LeetCode because that’s the “best language” for competitive programming.

Optimize implementation time, not runtime

LeetCode gives runtime percentages (ex. your solution ran faster than 50% of all python solutions and used less memory than 50%). Ignore these.

You’re a fast coder, so optimize implementation time. The interview mocks measure this and will rank you accordingly.

…but go for better asymptoptic runtime

At work, keep your code simple. If you’re data is small, O(n^2) is good enough, don’t sacrifice simplicity for an O(nlogn) solutions.

In LeetCode, you’re practicing your skills. One skill is writing gnarly code with lots of bookkeeping. So if you know how to do O(nlogn) but you need to manually implement a heap, do it.

Don’t break your head

The primary point of practice is to write lots of code. Don’t waste time breaking your head on “ah ha” problems. If it takes more than 30 minutes to figure out the solution, find a new problem.

Be careful of TLEs

TLE, TimeLimitExceeded, can sometimes be overly tight.

I once had a solution TLE because I used s = s.union(a) instead of s |= a. What a waste of 20 minutes figuring that one out.

If you get a TLE with the best asymptotic runtime, check the solution to make sure you’re doing it right, declare victory and move on.

Read solutions

Solutions will teach you:

  • Programming tricks. Did you know in python you can memoize almost any method just by adding the “@cached” decorator? I didn’t.

  • Cleaner ways to think. Sometimes you’ll solve it, but in an ugly way. Seeing cleaner solutions will give you more.

If you want more content like this, just click the heart below.

If you don’t, say what you want in the comments and I’ll do my best for you.