r/learnpython • • Feb 05 '20

I keep failing coding interviews - best way to learn data structures and algorithms?

For context, I'm a Stats major with some programming experience. I know how to code in Python from the standpoint of knowing syntax and OOP concepts. Outside of this, I know Matplotlib, NumPy, and Pandas. I attended a few SWE intern interviews and totally bombed the coding challenges. I was asked about what type of algorithm would be most efficient for the challenge and I had to tell them I had no idea. For HackerRank challenges, I am able to code the solution, but it is nowhere near efficient. If anyone has any Python related DS&A resources which I could use to learn this topic well and that could subsequently help me in these interviews I would really appreciate it!

456 Upvotes

49 comments sorted by

347

u/dbramucci Feb 06 '20

You may want to read some books that cover some fundamentals in c.s. like

  • Data-Structures

  • complexity analysis (big-O notation)

  • program architecture (like mvc: model-view-controller)

  • classic algorithms (like Binary Search, Dijkstras and A* search algorithms, sorting algorithms, ...), concurrency

  • parts of the standard library like

    • collections

    • multiprocessing

    • json

  • databases (namely SQL such as sqlite)

  • Recursion

  • Testing

    • Unit Testing

    • Integration Testing

    • Functional Testing

    • Niche tools like

      • Property-based testing

      • Fuzzing

      • Mutation Testing

It sounds like you currently need data-structures, complexity analysis and classic algorithms some resources include

  • Think Python: How to Think Like a Computer Scientist by Allen B. Downey

    An introduction to Python that covers the basics and introduces some useful fundamentals like recursion, algorithm analysis and interface design.

  • Introduction to Algorithms, 3e by Cormen, Leiserson, Rivest, Stein

    A book you will need to buy (physical editions exist)/checkout from your school/city library (interlibrary loans + book requests can help here). This book is a real big introduction to algorithms and will cover Data-Structures, Complexity Analysis and many algorithms to a greater extent than most c.s. undergraduate students will study. It's also a massive 1300 page book and costs on the order of $50 dollars to buy, but it's a nice reference book to own. This book is largely written in psuedocode

  • Structure and Interpretation of Computer Programs, by Abelson, Sussman, and Sussman

    SICP is a classic in computer science education that introduces readers to fundamental computer science ideas like recursion, functions and how programming works. This source uses a version of Scheme, not Python but the book's lessons aren't about its particular language choice. If you use this source, I'd advice checking out the physical copy or the unofficial digital edition which has a much more readable website (HTML) and pdf than the official ebook does.

  • MIT Open Courseware

(also, I don't think you need to learn all/most of the stuff above, I just list it because it will help you identify what you don't know and where you can learn it)

18

u/DennisLarryMead Feb 06 '20

Awesome write up, thanks for this.

9

u/vEnoM_420 Feb 06 '20

Thank you, mate. I'm gonna save it for future references.

6

u/chefsslaad Feb 06 '20

This should be a pinned answer

7

u/[deleted] Feb 06 '20

I love that there are communities that are this supportive of each other. Thank you from all of us for such a thoughtful and well written response.

13

u/relaxationfixation Feb 06 '20

There are some really great resources, before you dig in though it might be worth considering what you want from a new job. There are different technical job titles whose expected skills often include programming:

  • software engineer

  • programmer

  • SRE

  • data scientist

  • data analyst

  • ... And sent more

Each of these involve other related skills in addition to programming. Each also has a different list of typical responsibilities.

For example, a software engineer is expected to know about algorithms, runtime complexity, and systems design. They are responsible for creating and maintaining systems. In contrast, a data scientist is expected to understand statistics. They are generally expected to analyze data in order to better understand the business of their company. Many data scientists use code to do this, including many of the tools you mentioned having experience with.

9

u/dbramucci Feb 06 '20

I'll just say that any "sufficiently-matured" person who writes significant amounts of code should develop at least a vague intuition for runtime complexity.

It might not matter if they can prove that an algorithm runs in O(n2) time but they should get an idea of when they need to choose between a dictionary or a list due to how they are using it. I have had several peers who only got an introduction to programming through solving "real-world problems" and, despite being able to solve problems correctly, would have O(n4) algorithms instead of O(n) algorithms. This would drag their software's performance down to something unusable when used on any data larger than a small debugging test set. They were aware of their performance problems, but had no way to reason about why they were getting performance problems. Many of the changes are easy

  • don't add elements to the beginning of a list

  • don't modify individual characters in a string repeatedly

  • Use a set, not a list to check for duplicates

But they had no words (big O notation) to read, document and reason about these issues. That's why I think, based on what I've seen happen to some of my peers, that if you do enough programming you should at least vaguely know these things. It may be fine for beginner or even young working data-scientists to be unaware of these issues but, eventually, they will experience recurring dividends from learning complexity analysis and basic data structures + algorithms.

Granted, for an introductory role, I don't know if that would be in an interview for a data-scientist or not.

(p.s. and as a young programmer, I too, went through a phase of writing code that does what it needs to but has really bad asymptotic complexity in time and space)

5

u/Solonotix Feb 06 '20

I'm glad you mentioned the intuition about runtime complexity. That's one of those skills that I generally have a good grasp on, but it's hard to explain the possession of said skill, as well as to teach that skill to others. It really is just an experience thing from what I've seen.

The simplest explanation I can give is working in SQL, since that's my daily. It seems like non-database programmers take for granted the cost of grouping sets, distinct operators, the utility of indexes and how they're organized, etc., not to mention the intricacies of how user-defined functions (UDF) operate. A lot of people assume that a UDF operates in-line, as something like C# code might, but typically it acts as a nested For-Loop inside the For-Loop of your statement (since the FROM keyword is an implicit For-Loop, based on the records in the join product and filtered by the WHERE predicates provided). That's all before getting to more nuanced levels of performance degradation when using filtered views and CTE's, which frequently have to internally filter by the provided clause before exposing the values that a JOIN might filter out.

While I'm by no means a performance expert, it has become a sort of obsession to find the fastest and cheapest ways to perform operations within a given coding context.

2

u/acquireCats Feb 06 '20

So helpful! Thank you!

2

u/jjj7676 Feb 06 '20

Great answer also what are you thoughts on the Harvard course in edx I have been thinking on taking these courses but I am torn between these two

3

u/dbramucci Feb 07 '20

Harvard's CS50 seems to cover a very wide spectrum of fundamental concepts but it also looks like it goes very quickly. It might be better if you are trying to have a shallow understanding of all these concepts or, if you are trying to identify any weaknesses in your fundamentals.

The MIT 6.0001 looks like it covers far less material but it looks much more reasonable for the time-frame it takes. Then you could do other courses at your leisure.

If you already have some experience and only want to take 1 course, I would probably go with Harvard's CS50 but if you are new to programming, I would go with 6.0001 unless the MOOC interface of CS50 helps you stay on track. It's really that things like

Familiarity in a number of languages, including C, Python, SQL, and JavaScript plus CSS and HTML

Is a tall order for a 10 week course and I doubt Harvard has time to dig into the details of issues like the stack and the heap even if it briefly explains them. Your lexicon will be better, but you understanding probably won't be too deep just from that course.

2

u/findingNemoto Mar 01 '20

Damn man. Love your answer but those books will cost me more than a months salary.

1

u/dbramucci Mar 01 '20

I'll admit that "Introduction to Algorithms" is pricey but the other two books have free online books you can read. Likewise MIT Open Courseware is available online for free.

You can, assuming your libraries are like mine, go to your public library and request they borrow "Introduction to Algorithms" from another library (an inter-library loan) or purchase a copy (unlike a lot of tech books "Windows XP for Seniors" it won't go stale anytime soon).

2

u/deninho87 Feb 06 '20

I'll second the introduction to algorithms by CLRS. Excellent book and kind of must have :)

1

u/NotSoYoungAndBroke Feb 06 '20

RemindMe! One Week

1

u/RemindMeBot Feb 06 '20

There is a 39.0 minute delay fetching comments.

I will be messaging you in 7 days on 2020-02-13 20:16:24 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


InfoCustomYour RemindersFeedback

20

u/steelersexcel93 Feb 05 '20

check out csmajors subreddit. General recommendation for algo questions is Leetcode

10

u/[deleted] Feb 06 '20

For python, get a good book. I recommend Data Structures and Algorithms in Python by Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser. Only downside is there isn't an answer sheet for the end of chapter exercises, but hints are provided that will assist you.

9

u/RangerPretzel Python 3.7+ Feb 06 '20

Review this Programmer Competency Matrix

This will help you identify your strengths and weaknesses. Figure out where you're weakest and start reading up.

15

u/FGVel0ciTy Feb 05 '20

LeetCode is pretty good way to practice coding algorithms but to learn them I would recommend youtubing certain problems.

https://www.youtube.com/channel/UCmJz2DV1a3yfgrR7GqRtUUA

Back to back SWE is a pretty good channel to learn the concepts of different algorithms but they do their problems in Java. Still helpful to glance abstract concepts from the videos though.

13

u/duhhobo Feb 05 '20

you need to learn about big O notation and algorithm complexity as well. There will be multiple solutions to a question, and you need to understand which is the most performant and why.

4

u/[deleted] Feb 06 '20

I'm about 2/3 of the way through this, and I strongly recommend it as an incredibly informative and interesting resource for understanding algorithms, but it's hard to recommend unequivocally, given its length. Most people probably don't have the time to read well over 1000 pages on this topic.

I was looking for this book, which I have heard is good, and I stumbled on this, which may or may not be good, but seems to be free, which is nice.

13

u/Magnivilator Feb 05 '20

Try Project Euler.

17

u/Decency Feb 06 '20 edited Jul 23 '25

I enjoyed Project Euler, too. Many of the problems are complex enough that if you do them in an inefficient way, you simply won't be able to solve them. So it requires you to use a correct approach that helps ensure that you can handle that kind of complexity.

3

u/ckini123 Feb 06 '20

I haven't gone through all of it but I've enjoyed what I've read of this: https://runestone.academy/runestone/books/published/pythonds/index.html

Entirely free book that covers basic data structures and algorithms; there are interactive exercises to make sure you're keeping up with material. I'd really recommend you pick any one of the resources here and just following it. Websites like HackerRank and Leetcode are good for practicing but take some time to actually learn the basics.

3

u/Darth_Ender_Ro Feb 06 '20

Learning data structures and algorithms is a long and difficult process. Do not expect to do it in 1 month. The best way to do it is to start. Understand the concept of a, let’s say linked list, and then implement it yourself. Fail 100 times until you get it. Don’t copy it after your first fail. Then move on. Later on come back to the linked list, try to do it from memory. Have fun.

2

u/zoldier Feb 06 '20

I prefer codewars over hackerrank due to codewars showing the most clever user solutions after you submitted your answer

https://www.codewars.com/

2

u/say_fuck_no_to_rules Feb 06 '20

Deliberately and patiently work through (don’t just read!) Gayle Laakmann McDowell’s Cracking the Coding Interview. It will help put the concepts of Big-O etc. into context with the type of problems you’ll encounter in a whiteboard-style interview. I have a BS in CS, and I didn’t read it until after I’d been in the industry for about 5 years, but it was still super helpful in giving me greater confidence and more accurately display my competence in interviews.

1

u/notParticularlyAnony Feb 06 '20

Great book, but not great for learning the basics of algorithms/data structures. For that, see the other answers here.

2

u/safwankdb Feb 06 '20

There is a book written for people like you. It'll surely suit your needs. It's called 'Cracking the Coding Interview'. Have a look at it.

1

u/notParticularlyAnony Feb 06 '20

Great review, not great for initial learning of the concepts.

1

u/not_a_theorist Feb 06 '20

I'm coming from a similar place and I recommend https://www.coursera.org/specializations/algorithms

1

u/Deezl-Vegas Feb 06 '20

Practice with problems that feel a bit easy for you. You should be able to grasp the solution in 2-4 minutes and code it out in under 10.

When you meet a problem that you don't understand and/or melts your brain, give up after 2 minutes and check the answer.

Side study topics include: arrays, loops, stacks and queues, linked lists and trees, sorting algos, big O notation, regex and string manipulation, map/filter/reduce.

The main skills for whiteboard algos are knowing the solution in advance and breaking the problem into smaller parts. This strategy exposes you to lots of problems, and it gets you good at fixing little bugs and handling edge cases. Exposure to tons of problems and writing a lot of small chunks of code are key. Every complex algorithm is composed of simple algorithms.

1

u/PaulRudin Feb 06 '20

It can be a bit hit and miss. Over the years I've aced some technical interviews and made a mess in others. The problem can be that in a relatively short time you need to spot exactly what the underlying issue is and how to address it. If you start out down the wrong path it's hard to reset in this kind of setting.

For a good grasp of the fundamentals of data structures and algorithms I recommend Tim Roughgarden's lecture course, available for free on YouTube. See http://timroughgarden.org/videos.html

1

u/zeus5552 Feb 06 '20

Practice on codewars.com

1

u/sephiap Feb 06 '20

Honestly, I've never come across a resource better than Elements of Programming Interviews - it has study guides with timescales before interviews, thorough topic explanations, and EPI Judge that gives barebones skeletons of solutions and an automated checker for offline practice.

Edit: added a link to their free sample that shows the structure of the book.

1

u/varun_bat Feb 10 '20

I was in similar situation as you are and came across book "Algorithms" on the subject by Prof. Robert SedgeWick. Also audited his coursera course and really loved his teaching style. Highly recommended for people who love coding but are from non computer science background as datstructures and algorithms is very fundamental to the field and is a topic of most of the tech interviews.

1

u/twisted_mentality Feb 05 '20

Love the username btw.

I have heard good things about LeetCode, although I haven’t tried it out yet. My gut reaction is to just Google and YouTube to find out what the most common algorithm structures are, and then begin studying those structures until you feel comfortable enough. Also, so lock interviews.

I need to study to stay fresh and prepare myself. Message me if you’d like to focus and exam he study plans/ideas.

0

u/[deleted] Feb 06 '20

Algoexpert.io is pretty cheap and has everything you need

4

u/[deleted] Feb 06 '20

[removed] — view removed comment

1

u/[deleted] Feb 06 '20

I was thinking about buying it. Would you care to elaborate why it's shallow? Thanks.

3

u/balthazar_brat Feb 07 '20

As a former subscriber I'll say it's not worth, Leetcode and CTCI is more than enough also discussion section in leetcode is very good you can get many solutions for same problem some cool tricks that you never know, don't worry much about most optimal solution worry about understanding and be able to solve problems.

1

u/[deleted] Feb 07 '20

Thank you!!

0

u/kabooozie Feb 06 '20 edited Feb 06 '20

People have given good resources. Practice is important, so I’d suggest the CodeSignal website for practice problems and challenges.

Also Cormen worked with Khan Academy to do a pretty cool data structures and algorithms course for free on KA.

-2

u/zpokmn18 Feb 06 '20

There is no short way to DSA my friend.

4

u/notParticularlyAnony Feb 06 '20

OP didn't ask for a short way, just for a way.

1

u/ApoorvWatsky apoorvpatne10 Feb 06 '20

Agreed, it's all about longevity and consistency.