How to use LeetCode without melting your brain

Published by Sohhom on

For those of you who are preparing for the so called “technical interviews” at any kind of coding position, LeetCode is probably your bread and butter. For the rest of the blissfully unaware folks, LeetCode (often abbreviated as LC) is a platform where people go to torment their brain … I mean to improve their programming skills.

LC is especially famous for improving the so called Data Structures and Algorithms (DSA) skills. What is DSA and why is it needed? Imagine this: you are a teacher who need to assign ranks to top 5 students in your class. This is a sorting problem, but also can be solved in a variety of other clever ways. Why would anyone want to be clever if a simple method would do the job? Well, now imagine instead of students in a class, the numbers represent the number of ‘likes’ of a set of posts on Facebook… and your app need to do this several times per minute. Suddenly the difference between a poorly performing algorithm and a fast one translates to a lot of savings in terms of API calls and cloud compute units.

For many aspiring software engineers, LeetCode is seen as a rite of passage – a challenging trial by fire that separates the wheat from the chaff. With its emphasis on solving complex coding challenges under time pressure, LeetCode can indeed feel like a pressure cooker for the uninitiated. However, with the right approach and mentality, LeetCode can be an invaluable learning tool rather than a hair-raising ordeal.

In this article, we look at a sample of opinions, stories and suggestions for acing LC problems. We focus on actionable tips as well as indulge in some vague generic ramblings, because we would like to give you the ability to to generalize beyond very specific advice. And at the end, we give several references where you can go to learn about DSA in a more enjoyable and in-depth fashion.

Let’s dive in!

DSA are fundamentals of computer science

The key to surviving LeetCode is having a solid grasp of computer science fundamentals, especially common data structures and algorithms. While LeetCode problems may seem esoteric at first glance, most can be solved or optimized using standard techniques like arrays, linked lists, trees, stacks, queues, hash tables, sorting, searching, recursion, and dynamic programming. Knowing when and how to apply these basic building blocks is critical for efficient LeetCode solutions.

For example, linked lists are ubiquitous in LeetCode challenges. Mastering fundamentals like traversing, reversing, and merging linked lists in different contexts is a must. The same goes for essential tree algorithms like traversal, insertion, deletion, and sorting. And having a good handle on efficient sorting algorithms like quicksort, merge sort, and heapsort will enable you to optimize solutions when needed.

Beyond data structures and algorithms, having a good process is key to LeetCode success.

What is a process? Before diving into code, always read the problem description carefully and try some examples by hand. Figure out the inputs and outputs and consider edge cases. Next, think through a conceptual brute force solution and look for inefficiencies to optimize. Only then should you start coding.

While actually coding, resist the urge to over-optimize early. Get a working solution first, then think about improvements. Use good coding practices like descriptive variable names, modularity, and proper indentation. Include comments explaining your thought process and pseudocode if helpful. Verbally explain your code to reinforce understanding. That’s the goal – understanding, so you really want to write the code in a way that it makes sense to you when you review it three months from the time of writing it.

Make sure to test against the provided input/output examples first. Then come up with your own test cases, including edge cases. Fix any bugs before submitting.

What Does LeetCode Provide?

LeetCode is basically an online platform that provides a huge database of coding challenges to help prepare for technical interviews. With its timed contests, leaderboards, and paywall-locked premium questions, LeetCode gamifies the interview prep process. But what exactly does a LeetCode subscription unlock? Here’s an overview of LeetCode’s offerings:

Problem Database: LeetCode’s claim to fame is its massive bank of interview-style coding challenges. Their catalog contains questions across an array of categories like algorithms, databases, shell, concurrency, and more. Questions are ranked by difficulty from Easy to Medium to Hard, allowing for progressive skill development.

Solution Explanations: Most questions on LeetCode include detailed written explanations of optimal solutions. These explain thought processes, key insights, common pitfalls, time/space complexities, and suggestions for further optimization. Solutions are community-sourced from engineers around the world.

Discussions Forum: LeetCode has an extensive discussion forum where users can ask questions, participate in contests, and collaborate on solving problems. You can find hints, compare approaches, and gain insights from other users tackling the same challenges.

Contests: LeetCode regularly hosts timed coding contests with leaderboards to add competitive pressure. Contests typically last 1-3 hours and involve solving 3-5 problems. This simulates the time-crunched environment of real interviews. Both individual and team contests are available.

Interview Preparation Courses: For premium users, LeetCode offers multi-week guided courses focused on mastering specific technical interview skills like data structures, algorithms, system design, and behavioral prep. Courses provide structured schedules, video lessons, supplemental readings, and practice problems.

Company-Specific Questions: LeetCode has partnered with prominent tech companies like Google, Facebook, Amazon, and Microsoft to provide real past interview questions asked at those companies. This lets users practice on the types of problems they’ll likely encounter during actual interviews.

Mock Interviews: LeetCode connects users with experienced mentors for time-boxed mock video interviews. Mentors provide feedback and coach users on improving coding skills, communication, interview etiquette, and handling pressure.

Global Rankings: Users gain visible ranks based on LeetCode performance, adding a gaming element. Ranks motivate improvement and let users benchmark against peers worldwide. Scoring well can help land jobs at top companies that value LeetCode rankings.

Premium-Only Features: Paying LeetCode subscribers gain access to advanced analytics like submission history trends, tag filters, and leaderboard tracking. Additional features like offline practice, dark theme mode, and ad removal make the platform experience smoother.

Integrated Development Environment: LeetCode provides an online code editor integrated seamlessly with each challenge. The IDE allows coding in various languages like Java, Python, C++, JavaScript, and more. Users can focus on problem-solving versus environment setup.

In summary, LeetCode aims to provide everything tech professionals need to systematically improve technical skills and ace the interview game at leading software companies. With its enormous question bank, competitive elements, instructional resources, and built-in development tools, LeetCode seeks to be a one-stop-shop for interview prep.

How Should You Use LeetCode?

There is no definitive answer to this. You need to look at your current skillset, the skillset your future job requires, things like the LC quickstart guide, and chart a course for yourself.

Here are some helpful articles from people who have done just that:

  1. A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions
  2. Roadmap for learning DSA
  3. What’s the best way to use leetcode?
  4. 5 years of leetcode with no progress. I’m giving up

Is LeetCode Essential For A Programming Job?

Sort of. LeetCode type problems are a quick and objectively verifiable method of gauging someone’s thinking capabilities. However, those thinking capabilities may not correspond to the actual capabilities that are needed for software development.

But hey, that’s true for almost all tests isn’t it? We have known since decades (citation) that the standardized exams like SATs or GRE or others are somewhat lukewarm indicators of college performance. Yet, universities continue to insist on these grades because they are easy to compare and easy to calculate cutoffs.

Grinding LC problems can cause the side effect of becoming less effective at solving problems, what with practice with a hammer making all problems look like nails etc. Apparently interviewers can tell, as attested by this anecdote from Reddit:

I had my final interview for the Microsoft SWE internship role today, and something really stuck out to me. We were wrapping my last interview up after all the questions were done, and he literally told me “you aren’t like other candidates”. I asked him what he meant, and he said every candidate he had interviewed so far just grinds LeetCode and he could tell. He said that apparently, everyone comes into the interviews and just codes out the solution regardless of how difficult the problem is that he gives them, but as an interviewer he doesn’t actually know if they can code well or not, they could literally be copy pasting a solution that they memorized. He said no one talks their solution out loud, and doesn’t communicate their thought process or ask questions, and he is left with no clue of their actual programming capabilities, so he isn’t impressed by their solution. He also said that, maybe for other companies LeetCode might be good, but at Microsoft they aren’t necessarily impressed by it anymore.

I have no clue if I’ll get the role or not, but just thought that was an interesting tidbit to share. Just something to keep in mind, to talk through your solution and communicate way more than code, as that seemed to be what he was actually looking for.

https://www.reddit.com/r/csMajors/comments/17vd61s/microsoft_interviewer_told_me_leetcode_is_bad/

When it comes to LC-style questions in interviews, Some people swear by it, while some people swear at it:

Some people are confident enough to consider letting their LC profile speak for itself:

Another perspective is that LC-style interviews are not difficult enough, and that the amount of poor quality algorithms running in production is too damn high. As Hacker News user fl0ki explains in the context of using profilers for optimizing slow parts of a program:

A profiler can’t tell you that an entire operation would become completely unnecessary if you maintain a certain invariant elsewhere, and this can go as far out as you let it, e.g. entirely changing your in-memory data model to prepare it for the operations you know you’ll be performing in the critical path.

I know because I’ve been fortunate to work in environments full of seasoned experts writing world-scale software in C++ and Rust, that I was still able to optimize 100x-1Mx by zooming out and solving the same problem from a new perspective, something a profiler cannot help with.

https://news.ycombinator.com/item?id=39019226

Coming more directly to the concept of using difficult “gatekeeping” questions asked in programming interviews, he elaborates:

Writing what is effectively a graph algorithm where every edge/node lookup is a string key in a hash map. Transforming the string keys into dense integer offsets once-off costs nearly nothing, and reduces every single lookup into a direct memory offset. That one optimization alone was an over 30x speedup, and you can imagine that if they wrote it like this then there was a lot of other low hanging fruit.

These people passed one of the most famous computer science hiring bars in the industry, and this code passed review.

When I interview people with a puzzle version of a problem like this, 95% of candidates still use string keys instead of reducing them to integers, so this is shockingly common and not at all an edge case. It’s just one of dozens of common performance antipatterns I see in code written by people with TC over $500k USD.

It’s also why I call bullshit when people online claim that computer science interviewing is gatekeeping and they don’t need algorithms in their day job. This is false both ways; it’s not gatekeeping successfully enough because nonsense like this makes it to global production and costs tens of millions, and anyone who wants to succeed in this job is expected to know better.

https://news.ycombinator.com/item?id=39020795

Which is something that I have come to realize from other contexts such as manipulating audio data as well, but that is a topic for another day 🙂

Suggested Resources

Here are some articles and repositories that you would give you a leg up on your LC journey:


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *