Ask HN: How to get better at Data structures and Algorithm?
I am a fairly experienced programmer (10 years) and have always worked with startups building prototypes and getting products to product-market fit. I have worked in all kinds of languages Java, C#, Ruby, Elixir. However, I am not good with data structures and algorithms. What do you reckon I should do in order to get good? Any books, tutorials, course, etc that have helped you?
17 comments
[ 2.0 ms ] story [ 52.0 ms ] threadThe past 12 years I've been doing automation of various types, so I've forgotten much of the Data Structures and Algorithms I learn in college.
https://www.codeforces.com, https://www.codechef.com/, https://www.topcoder.com/ (even if in my opinion, this last one is becoming worse and worse) provides good problems and contests to apply and discover new ideas.
And if you're even more motivated, upsolving previous contests problems. Codechef long contests are really interesting for this: after thinking a lot about these problems during more than 1 week, reading the editorial and trying to apply by yourself the idea seems to me an excellent way to learn.
https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press...
It's all in there. Put away a week of time to really start digging into it, get into the habit of learning from a book. I'd say it's worth it.
For the first you learn about a couple common paradigms like dynamic programming, some graph algo & ds like BFS/DFS, shortest path algorithms, backtracking etc... and just do a lot of leet code problems. The more of these you do, the better you get at them, because most of them can be solved faster by knowing techniques (like using two pointers at either end for some array based problems) etc...
For the second one, you try to learn about the landscape of DS & Algo. For example, i know about the existence of red black trees, why they were invented, types of problems you can solve with them, but i won't be able to implement them from memory or without using references (even then it might take me a while). For the second type of learning, i would just do CLRS chapter by chapter, or watch any of the numerous algorithm courses on youtube from MIT, Stanford, ArsDigita. The second type of learning lets you understand and gain an appreciation for the tools you use everyday as a developer. For example, studying B-Trees will let you appreciate how rdbms are able to retrieve information so fast.
Ideally, a good developer would be well versed in DS & Algo landscape and be able to solve leetcode problems fast, but i think #2 is more necessary to becoming a better developer, although #1 helps more in the job market.
Edit: hah, I type very slow on my phone evidently. strikelaserclaw's advice here is very similar to this :)
1) Data Structures and Program Design in C by Kruse, Leung and Tondo.
2) Algorithms in C (Parts1-4 and Part5 two-volume set) by Robert Sedgewick.
3) Compared to What? An Introduction to the Analysis of Algorithms by Gregory Rawlins.
4) How to think about Algorithms by Jeff Edmonds.
5) Advanced Data Structures by Peter Brass.
Finally, study the ideas, concepts and techniques behind the C++ STL library to see how to design Data Structures and Algorithms in a clean and independent manner and yet can couple them as reqd. in myriad ways for actual usage.
My only con is that the answer key doesnt seem to be available.
See https://news.ycombinator.com/item?id=20047607
[1] https://scholar.google.com
https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf
Data structures:
- linked list (single,double)
- array list
- hash map with different implementations
- binary tree(and tree traversing, depth/breadth first search)
- red-black tree
- self balancing tree
- stack
- heap
- queue
- set (with funny set operations like intersection)
- B and B* tree
Read about their strengths and weaknesses.
With every data structure comes operations. More or less the same with a few speciality but their implementations are vastly different. You should try to implement the same operations at every data structure:
- Find an element
- Find min/max
- Add / delete an element
- Sort the data
Take time to implement the standard sorting algorithms for a list to understand the speed implications: quicksort, bubblesort, heapsort, radix sort, bucket sort
If you look for other algorithmic challenges I suggest checking out graph algorithms. Dijkstra and A* are the most famous.
1.) Go to https://leetcode.com It's a whole community built around algorithms and data structures.
2.) Pick an easy problem.
3.) Code it naively/brute force it just to get it 100% correct.
4.) Check the Discuss tab, sort by Most Posts and read the solutions and comments.
5.) Every time you hit a wall or encounter a new term (like space/time complexity, dynamic programming, binary tree etc) find a video on YouTube that explains it really well. Not enough? Ask community, consult a paper or a book.
6.) Go back to 2.) until you're ready for the next level.
Don't let your own mind sabotage you by telling you that 'this is not real world programming', 'this is for college kids'. Embrace it and you'll find it sort of interesting like solving a hard puzzle in a point & click adventure video game.