From O(no) to Big O(n)

From O(no) to Big O(n)

Or How to Stop Worrying and Learn Data Structures and Algorithms the right way

·

11 min read

The Dilemma

Have you contemplated learning Data Structures and Algorithms (DSAs) in the past? Did you half-heartedly try to learn it but thought it wasn’t possible? Maybe you convinced yourself that you don’t need to learn it. “It’s too hard and my job doesn’t require me to know this”, you say. Maybe that’s true and you don’t need it for your current job, but in a time of recession where we see massive layoffs in the IT field, it wouldn’t hurt to have some extra tools in your tool belt to give you options. David Epstein stated it best in his book Range, when he said, “mental meandering and personal experimentation are sources of power…”

There's a little-known secret about the legendary tennis player, Andre Agassi: he hated tennis. Specifically, he hated to practice, but he did it anyway. He wanted to quit–which he almost did many times growing up–yet he always came back to the game. It's the same feeling I had with learning DSAs. You hate it for a while, and then you learn to embrace it. At some point, if you keep at it, you'll experience a breakthrough. This doesn't mean you have to grit and grind through frustration and put yourself through mental stress. No, that's not the way. Take your time with it by taking it one day at a time, as consistency is the most important factor in achieving things we set our minds to. The best way to alleviate some of the pressure and keep your motivation from waning is to see it as a process, stop focusing on the goal, and find the right type of learning material that suits you.

For a minute, imagine yourself in the not-too-distant future having learned DSAs and conquered your demons. How do you feel? Relieved? Accomplished? Proud, even?

The Mindset

As children, we are intrepid in our pursuit to learn new things; we’re always full of curiosity and wonder about our new world. Why is this? I believe children have a mindset to experiment because they don’t set limitations on their wonder. They don’t understand the concept of failure at that young age and only want to experiment and satisfy their curiosity. As adults, we allow feelings of doubt, fear, and judgment to permeate our minds and cripple our sense of wonder and what we think we can accomplish.

Thomas Edison was famous for his dogmatic pursuit of inventing the light bulb. I’m sure you’ve all heard his thoughts on his journey of discovery: “I have not failed, I’ve just found 10,000 ways that won’t work.” This is the growth mindset that Carol Dweck so lucidly explained in her book, Mindset. In the book, she proclaims, “In the fixed mindset, everything is about the outcome. If you fail—or if you’re not the best—it’s all been wasted. The growth mindset allows people to value what they’re doing regardless of the outcome. They’re tackling problems, charting new courses, working on important issues. Maybe they haven’t found the cure for cancer, but the search was deeply meaningful.”

Setbacks are an asset for a growth mindset because it sets the stage to try something in a new, innovative way. It nudges us to get back up and go for it again–but only if we have a growth mindset and understand that it’s okay to stumble, but it’s not okay to quit. As Ryan Holiday so eloquently put it in The Obstacle is the Way, “The obstacle in the path becomes the path. Never forget, within every obstacle is an opportunity to improve our condition.” Read that again–the obstacle in the path is the path. Obstacles are inevitable, conquering them is how you move forward. So, avoiding obstacles and stopping at the first sign of them, should never be an option. The difference between consistently showing up and giving up is the process and mindset you set for yourself.

We also need to stop looking ahead at our colleagues who are further along in the journey and feel dubious of our abilities. This is imposter syndrome. It’s an imposter because it’s an existential crisis that only exists in our minds. These kinds of thoughts are normal when we set out for the unknown to learn something new. Understand that everyone starts from zero. These self-sabotaging beliefs and the rat race of competition don’t serve us in any way to achieve our goal. The only person you have to compete with is the future version of yourself. So, be intentional in your process, focus on what you control, set smaller victories, enjoy the journey, and always be moving forward. Success is not about focusing on the goal; the goal is only a device we use to set our path. Real success comes while following the journey.

Success is not about focusing on the goal; the goal is only a device we use to set our path.

The Way Forward

With intent and consistency, the human mind can accomplish great things. Some people learn through reading, some through videos, and some through mentorship. If you’re having a hard time understanding AVL Trees or Tries, maybe all you need is another analogy or another perspective from a different source. The main thing is to be kind to yourself by being patient and finding what works and what doesn't.

It’s rarely the case where we set out to achieve a goal and follow a linear path. Achievements are fraught with detours, obstacles, adjustments, and challenges. But, this is the way. This is part of your hero’s journey. Let the obstacle be the way and slay your dragons. When we’re stuck trying to solve an algorithm problem, we have to find shortcuts to overcome these setbacks. It’s not always hard work and grind that wins. Many times it’s understanding that one way doesn’t work so we need to find an alternative way. That may be through watching a Youtube video, learning from a book, asking for help, and sometimes even taking a break. Richard Feynman said, “Study hard what interests you the most in the most undisciplined, irreverent, and original manner possible.” By finding shortcuts and detours in your search for knowledge, you will keep the well of interest and curiosity full.

Let the obstacle be the way and slay your dragons.

Letting Go

Is it necessary to always grind and move forward with a learning experience? No–there are times when our careers, interests, or motivations change. But, that should only be an option when you have something else to chase. Walking away should never be an option because of our insecurities. Once you allow your insecurities to take control, you’ll be surprised how many times they derail your future endeavors. Letting go should only be a viable option when there’s a legitimate reason. We are ever-evolving and our circumstances evolve as well, so I won’t say that you must complete everything you start.

Science has also shown that we engage in puzzles, video games, and hard tasks for that ‘A-ha!’ moment. It’s a special feeling of accomplishment. This feel-good sensation is the result of a hormone in our body called Dopamine. Dopamine is a messenger hormone responsible for reward processes in our minds. It’s the same hormone involved when we eat good food, earn money, and have sex. According to Christian Windischberger, one of the researchers investigating how the brain reacts to these ‘A-ha!’ sensations, “Our results indicate a close correlation between dopamine, exhilaration, and creativity.” This shows that solving problems is something we’re wired to enjoy and increases our ability to solve new problems in innovative ways. Without the struggle, there wouldn’t be an exhilarating reward.

Productive Mastery

Some tips that I have used in my journey of struggle may come to work for you as well. Here, I share some tips and resources that have served me well:

  • Learn for yourself and your curiosity, and not for school or a job.

  • The goal will take care of itself if you consistently show up. So, set an achievable goal with smaller milestones for early wins.

  • Be intentional in your journey by having a partner or a group to learn with.

  • Don’t just have a goal, visualize what achieving that goal would mean.

  • Learn every day, however small it may be. Enter that into a calendar and watch how hard it will be to break your string of commitments. This is habit tracking.

  • Don’t add pressure by only prepping for an interview. Start early and study because you’re curious and have a purpose for learning DSAs. The urgency of getting ready for a job interview adds too much stress by putting you on a tight schedule. Some people do work better under pressure, but it's a lot less stressful if you start before it becomes a crucial part of obtaining something you value.

  • Understand that it will be hard sometimes and there may be days when you feel exhausted. It’s ok, take a break if you need to or tackle a smaller problem. Never lose hope.

  • Reverse engineer your algorithms study when stuck by looking at the solution on Leetcode or a GitHub repository. It’s not cheating, it’s a shortcut as long as you understand the solution. Come back to it in a couple of days and try to solve the problem yourself. This hack could be used the entire time as a way to learn faster rather than toiling away trying to figure out the solution on your own. Remember, study in the most undisciplined, irreverent, and unique way possible. Don’t get stuck in a rut and get discouraged.

  • The majority of technical whiteboarding questions come from medium-level Leetcode problems. Spend time wisely on the easy to medium-level questions and only look at the really hard ones if it interests you.

  • Don't tackle Leetcode questions by filtering them by the data structure used. Mix it up and see if you know which data structure/algorithm to use.

  • Don’t memorize the problem, data structure, and algorithm used. Learn the patterns to solve similar problems. This way when the problem is slightly different, you won’t get stuck.

  • Teach what you’re learning to someone else in the simplest way possible or take notes in your own words to get comfortable with the subject matter. This is the famous Feynman Method.

  • Be comfortable in at least one or two programming languages. I would recommend Python and supplement that with any C-style language like Java or C++. At the very least, you should be comfortable reading code in Java or C++, if you don’t intend to be fluent.

  • Use the Pomodoro technique to study in 25-minute chunks. Don’t burn yourself out.

  • Understand that your energy levels are more important to your study than the amount of time you spend toiling away. Eat well, be active, take breaks, drink water, stay connected to your friends, and sleep well.

  • Keep distractions away by keeping devices away. Learn deeply or don’t learn at all.

Remember that our minds are capable of learning things when we persist. Don’t just be someone–become someone. Strive to be slightly better than the previous day.


Resources:

Books (full disclosure: these have affiliate links)

I would suggest any two of these books and I have purposely selected ones that don't delve into higher mathematics and theory.

  • Grokking Algorithms by Aditya Bhargava (full disclosure: I was one of the early reviewers of this book while it was being written). There is a Second Edition of this in the works. This is a good introductory book on the subject and very fun to read.

    Buy on Manning

  • A Common Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow*. This dives deeper into the subject and is a good book to follow Grokking Algorithms. The author makes learning the subject very approachable.*

    Buy on Amazon

  • Guide to Competitive Programming, Second Edition by Antti Laaksonen. This is a very unique book that delves into the competitive programming arena, but it’s a really good book to learn Data Structures and Algorithms in C++. Some people say this book is better than the Introduction to Algorithms book by CLRS because it’s not heavy into mathematical proofs and theory.

    Buy on Amazon

Honorable Mentions (recommended but ranked below my top 3)

  • Cracking the Coding Interview, Sixth Edition by Gayle Laakmann McDowell*. A book that most of us have heard of and seen recommended many times. But, if you follow any two of the above, I don’t think you need this one. But, it’s a personal call on what programming languages you are comfortable with and which teaching style you prefer. This book goes into more than just Data Structures and Algorithms.*

    Buy on Amazon

  • Elements of Programming Interviews in Python by Adnan Aziz, Tsung-Hsien Lee, et al. A great book for those who are comfortable with Python programming. But again, if you have any two of the top three, this probably isn’t needed. But, if you want a book on more than just Data Structures and Algorithms, then any of these two honorable mentioned books would be great.

    Buy on Amazon

Courses (full disclosure: some of these have affiliate links)

Paid

  • Back to Back SWE

    This course is taught by an engineering student in a friendly and enthusiastic way. It's like having a mentor by your side going through a rigorous college-level curriculum on DSAs. I wholeheartedly recommend this course. You can sign-up for a subscription for 6 months, 1 year, or lifetime. The course usually comes with a discount every so often, so I would look to see if there's a promo going on right now. Also, you can use synaptiq as a promo code to get an additional 10% off and stack it on top of any running promos.

    Link to Paid Course

  • Design Gurus Grokking the Coding Interview: Patterns for Coding Questions

    Having a trove of patterns that correlate to problem-solving techniques sets this course apart. Recognizing patterns for DSA problems gives you superpowers as it allows you to remove the window dressing in tough questions, understand the heart of the problem, and recognize the optimal solution.

    Link to Paid Course

Free

Forums

  • Reddit

    r/leetcode