how to learn data structures

15
Home Write Notifications 5 Hemanth 990 FOLLOWERS Last asked: 29 Jun, 2013 QUESTION TOPICS

Upload: hemanth

Post on 11-Jan-2016

215 views

Category:

Documents


1 download

DESCRIPTION

Ways to learn Data Structures

TRANSCRIPT

Page 2: How to Learn Data Structures

Algorithms

Computer Science

Edit Topics

QUESTION STATS

Views191,205 Followers 990 Edits

How do I strengthen my knowledge of data structures and algorithms?I have had two courses one on data structures and another on algorithm design. Due to a combination of factors, both have left me with a fractured knowledge of the subject with lots of gaps in between. What would be a good way to go about filling the gaps and making progress? I want to improve my knowledge and problem solving ability.

Whenever I open a topic to go through, I know some parts of it. This makes it even harder to study it as I start skipping parts and then getting stuck somewhere down the line. Moreover, I am not at all feeling confident by going at it this way.Re-Ask

Follow990CommentShare26Downvote

22 ANSWERSASK TO ANSWER

Hemanth KumarEdit Biography • Make AnonymousWrite your answer, or answer later

Robert Love, Software Engineer at Google2.2k upvotes by Marc Bodnick, Lars Strojny, Paarth Patel, Siddharth Tyagi, (more)

I see it time and again in Google interviews or new-grad hires: The way data structures and algorithms—among the most important subjects in a proper computer science curriculum—are learnt is often insufficient. That's not to say students read the wrong books (see my recommendation below) or professors teach the wrong material, but how students ultimately come to understand the subject is lacking.

The key to a solid foundation in data structures and algorithms is not an exhaustive survey of every conceivable data structure and its subforms, with

Page 3: How to Learn Data Structures

memorization of each's Big-O value and amortized cost. Such knowledge is great and impressive if you've got it, but you will rarely need it. For better or worse, your career will likely never require you to implement a red-black tree node removal algorithm. But you ought be able—with complete ease!—to identify when a binary search tree is a useful solution to a problem, because you will often need that skill.

So stop trying to memorize everything. Instead, start with the basics and learn to do two things: Visualize the data structure. Intuitively understand what the data

structure looks like, what it feels like to use it, and how it is structured both in the abstract and physically in your computer's memory. This is the single most important thing you can do, and it is useful from the simplest queues and stacks up through the most complicated self-balancing tree. Draw it, visualize it in your head, whatever you need to do: Understand the structure intuitively.

Learn when and how to use different data structures and their algorithms in your own code. This is harder as a student, as the problem assignments you'll work through just won't impart this knowledge. That's fine. Realize you won't master data structures until you are working on a real-world problem and discover that a hash is the solution to your performance woes. But even as a student you should focus on learning not the minutia details but the practicalities: When do you want a hash? When do you want a tree? When is a min-heap the right solution?

One of the questions I ask in Google engineering interviews has a binary search tree as a potential solution (among others). Good candidates can arrive at the binary search tree as the right path in a few minutes, and then take 10-15 minutes working through the rest of the problem and the other roadblocks I toss out. But occasionally I get a candidate who intuitively understandstrees and can visualize the problem I'm presenting. They might stumble on the exact algorithmic complexity of some operation, but they can respond to roadblocks without pause because they can visualize the tree. They get it.

As for a book, there is but one: Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein, otherwise known as CLRS.

If you want another text, perhaps one with more examples in a specific language, I recommend Robert Sedgewick's Algorithms in C++ orAlgorithms in Java, as appropriate. I prefer CLRS as a text, but you might find these a better teaching aid.  Written 30 Jun, 2013 • 162,593 views • Asked to answer by Anonymous

Upvote2.2k

DownvoteComments18+Share79

Pawan Bhadauria, Avid coder...285 upvotes by Aashish Tripathee, Lavin Deepak Sharma, Fanis Antoniou, Jatin Lahoriya, (more)

Page 4: How to Learn Data Structures

As Robert Love said, intuitively understanding how different data structures work & when to use those is extremely important. This helps us shape decisions which ultimately impacts the performance of the program. Since this aspect has already been explained, I wanted to make an effort to illustrate this a little further using some basic examples which I feel should help createa base for further exploration and for interviews.

At a high level we have two abstract data types namely dictionaries, containers [Priority queues are special containers since they don't have fixed order of data retrieval and storage]. In below example, I am using a container with numbers and not dictionaries.

Consider this. You are dealing with a set of million numbers. Some are already in your kitty and some are coming down the line. You picture an array of numbers in mind. Fair enough. Now since the numbers are coming in no particular order your array looks something like this:

[2,3,1,11,9,6,32,31,5…………..]

This array contains critical data and all search, insertion & deletion are frequent, specially search and insertion. So your manager wants great data structure that supports efficient search and insertion. Great. You look at your first visualization, which is an array and find time complexity for these operations as follows:

search - O(n)insertion - O(1)deletion - O(n) [since we don't want to create holes in array so will have to move data other wise it can be O(1)]

You think about it and ask a question. What good a O(1) insertion is when people will have to do lookup in O(n) time? You might think that this linear time doesn't look that bad for small set but it will create havoc for large data set as you might have to iterate through all items while doing the search in worst case. This clearly will not work for search which forms critical part of the program. So what do you do? You feel that until you provide some order to the data set, it will be very difficult to do anything other then linear search because it is hard to predict anything until you have gone through all items. Fair enough. So you get an idea. Why not just sort the array or keep the array sorted with data? If we do that, we can use binary search to search for an item in logarithmic time O(log n) time. Great!! So you visualize array like this now:

[1,2,3,5,6,9,11,31,32…………..]

But what happens to the maintenance cost of the array which was nothing earlier [O(1)for both insertion and deletion]? That will definitely change. Lets say for simplicity that you know how many items are there and initialized array to full size [leaving aside dynamic extension which we might have to do if data size grows], you are still faced with this situation for finding the right place to insert items and rearranging structure to keep it

Page 5: How to Learn Data Structures

sorted. So both insertion and deletion become a bottleneck and result in O(n) time. At this point you can ask a question to yourself. If your program only had large number of reads but very infrequent writes, this might work for you :-). But clearly this is not the case. Remember what we said "all search, insertions & deletion are frequent, specially search and insertion". So with sorted array you reached here:

search -  O(log n)insertion - O(n)deletion - O(n)

When the time complexity of a operation changes from O(n) to O(log n), it is amazing. Try to feel it. Your program while searching for items in unsorted array of size 1 million, would take 1 million unit of time in worst case while doing linear search. Compare this with, searching in sorted array with 16 units of time (log (1 million) = 16 approx.) in worst case. This is a loose explanation but I hope it sends a signal why data structure enthusiast are crazy about logarithmic time. It is for this reason that if you do a recursive binary search, you will be able to find an item among 1 millions items in... (more)Upvote285

DownvoteComments13+Share5

Lakshmi Narasimhan Parthasarathy, Most of my time is spent in creating ... (more)39 upvotes by Naveen Tirupattur, Shobhit Agarwal, Neil Traft, Vineet Naik, (more)

I had the same problem as you had. I didn't study algorithms and data structures formally. I'd go with every point mentioned by Robert Love and I'd like to resonate the same.Identifying what data structure or algorithm is the right fit to the problem is the key skill IMHO. Steve Yegge mentions this in his post, Get that job at Google. I'm just quoting it here,

For instance, if they ask you about coloring U.S. states in different colors, you get major bonus points if you recognize it as a graph-coloring problem, even if you don't actually remember exactly how graph-coloring works.

And if you do remember how it works, then you can probably whip through the answer pretty quickly. So your best bet, interview-prep wise, is to practice the art of recognizing that certain problem classes are best solved with certain algorithms and data structures.

This is a skill which can be acquired. Start solving problems from any competitive context, likeUVa Online Judgehttp://www.topcoder.com/

That should really keep you occupied for a while. Reward yourself if you identified the solution correctly, i.e. the apt data structure or algorithm for

Page 6: How to Learn Data Structures

the problem. I've seen that many problems are simplified versions of real life issues and require a more intuitive approach, so just a textbook knowledge of algorithms wouldn't be of great help.

As always, each algorithm related answer ends with a book recommendations section. Here's mine.The Algorithm Design Manual by Steven S SkienaQuoting from the above article by Steve,

My absolute favorite for this kind of interview preparation is Steven Skiena'sThe Algorithm Design Manual. More than any other book it helped me understand just how astonishingly commonplace (and important) graph problems are – they should be part of every working programmer's toolkit. The book also covers basic data structures and sorting algorithms, which is a nice bonus. But the gold mine is the second half of the book, which is a sort of encyclopedia of 1-pagers on zillions of useful problems and various ways to solve them, without too much detail. Almost every 1-pager has a simple picture, making it easy to remember. This is a great way to learn how to identify hundreds of problem types.

Introduction to Algorithms by Thomas H. Cormen, the CLRS book mentioned in other answers, very comprehensive.Concrete Mathematics: A Foundation for Computer Science (2nd Edition) by Ronald L. Graham, if you are the math-typeProgramming Pearls (2nd Edition) by Jon Bentley, for a more practical view.  Written 1 Jul, 2013 • 8,116 views

Upvote39

DownvoteComments3Share2

Edward Mengel, Analytical Product Manager28 upvotes by Anya Deason, Dusan Babic, Harpreet Singh, Jatin Kathuria, (more)

The answer depends on how technical you want to get:

High Level: search for algorithm visualization sites likeSorting Algorithm Animations or sortvis.org - sorting algorithm visualisation

Mid Level:Take Tim Roughgarden's Algorithm courses on Coursera. Do all the homeworks in a language of your choosing.

Deeper Mid Level: Read Amazon.com: Introduction to Algorithms (9780262033848): Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein: Books cover to cover

Low Level: Pick a language and start implementing algorithms in it. Repeat, and try to do it better the second time. If you can write the code without referring to anything, you have it down.

Page 7: How to Learn Data Structures

Deeper Low Level: Start searching for Academic papers on optimizations for Algorithms. Many of the core Algorithms we learn in school, have many optimizations applied in production libraries or attempted by academics. One great real world example: Timsort  Written 6 Jun, 2014 • 4,033 views

Upvote28

DownvoteCommentShare

Puneet Maheshwari, Software engineer@ Google24 upvotes by Sanchit Kalhan, Vivek Yadav, Deepak Pandey, Leonid S. Knyshov,(more)

Simplest answer is practice practice lots of coding. Even if you understand all theoritcal data structures, you will need to start applying them to feel confident. I highly recommend reading a algorithm topic and solve lots of problems related to it. My personal favorite is topcoder, where you can filter problems based off a topic. Make a genuine effort to solve those problems even if takes more time and do not read solution unless you really have exhausted all possibilities. While reading solution you should think why the person solving problem could think that way and why you failed to do so? The more you do it better you will get. Good luck!  Written 3 Feb, 2014 • 4,978 views

Upvote24

DownvoteComments1+Share

Amandeep Gautam6 upvotes by Mohammad Rafay Aleem, Deepanker Kumeria, Mark Lewis, Mahesh Lagadapati, (more)

Follow CLRS. This book covers everything: from basics to advanced, run-time analysis of algorithms.  It is a really nice read and if you want to excel just do the exercises: they are just great.

If you want to see lectures there is a course on MIT OCW on data structures(some of them are taken by Prof. Charles himself). Course with the book will remove any fracture you have.

And for what to read, just follow the lecture contents: Here is the link of a older version:Introduction to Algorithms (SMA 5503) of course and the newer one:Introduction to Algorithms.  Written 29 Jun, 2013 • 3,019 views

Upvote6

DownvoteComment1Share

Page 8: How to Learn Data Structures

Rachel Fong, software engineer / MIT 201213 upvotes by Seyram Komla Sapaty, Deepanker Kumeria, Vijayakumar Ramdoss,Vleria Rskh, (more)

Can't beat CLRS for a comprehensive overview/understanding.

For a nice motivating way to practice algorithmic application (it's one thing to understand the concept of dynamic programming, and another thing entirely to know when and how to apply it to different scenarios), you might try algorithmic competitions such as USACO and Topcoder. I was much more comfortable with my mental toolkit of algorithms and data structures in high school, when I did those regularly, than now working in software, where architecture is the main focus and only the bare basics of data structures / algorithms are necessary (hash tables, very occasional graph search or basic binary tree).  Written 2 Jul, 2013 • 4,636 views

Upvote13

DownvoteCommentShare

Ariel Krakowski, Working on Learneroo.com9 upvotes by Thierry Backes, Gautam Kumar, Virendra Oswal, Ryan Fox Squire,(more)

If you already know some Algorithms, you won't want to use a video course. Instead, you'll want a resource that lets you quickly jump to the parts you're interested in and lets you practice what you know. You could skim through a good Algorithms book, such as the Algorithms Design Manual. I would suggest trying out my Algorithms tutorials on Learneroo. It lets you easily skip to what you're interested in, and you can practice implementing and applying the different algorithms and data structures. 

(cf. my answer to What are the most learner-friendly resources for learning about algorithms?)  Written 5 Nov, 2014 • 1,810 views

Upvote9

DownvoteCommentShare

Sambit Mishra, Software Engineer , Cisco Systems2 upvotes by Pankaj Saxena and Rachit Mathur

Algorithms and Data Structures,  in a way go hand in hand. In order to implement the algorithms we use Data Structures. So you can say Data Structures are the tools to implement the algorithms that we learn. Now there could be various ways to implement an algorithm based on the Data Structure you are choosing. So your choice will determine how much space and time it takes . This is the so called "Space and Time Complexity". 

Page 9: How to Learn Data Structures

So I would suggest read initially the Basic Data Structures like Arrays , Linked List , Stacks , Queues , Trees . [ Very Important ].

Once you are comfortable with these , move on to read the algorithms. Start with the basic ones like Searching , Sorting Algorithms. Then move on to the next advanced algorithms. While you are reading the algorithms try to implement them . This will give you a chance to use your knowledge to decide which Data Structures should be used . What are the advantages and disadvantages of one data structure over the other [ space v/s time ] .  

You could use "Introduction to Algorithms by Cormen"  for Algorithms.  Written 2 Jul, 2013 • 2,093 views

Upvote2

DownvoteComment1Share

Yash Singla, Programmer, teacher4 upvotes by Ravi Kapoor, Chris Weathers, Pritika Mehta, and Rachit Mathur

I would recommend the book "Introduction to Algorithms by Cormen,  Leiserson, Rivest and Stein". Start with Data Structures first, read a chapter and then implement and use the structure in any program on your computer. Make sure you use all the different variations of the structure, this way you will know when you have missed something in the book.After completing DS, start with algorithm. Implement the examples given in the book. Once you are done with the examples, pick up some questions from net that can be solved using that particular algorithm. Solving more questions will help you understand the algorithm even better.

There is a course on coursera titled "Algorithms: Design and Analysis, Part 1" by Tim Roughgarden from Stanford, that is going to start on 1st july. I would highly recommend attending the course. Taking regular classes helps in fighting procastination and also you can practice on practical exercises from the course.  Updated 29 Jun, 2013 • 2,846 views

Upvote4

DownvoteCommentShare

Pradeep Pujari, IR/ML and NLP Engineer2 upvotes by Jason Daniels and Rachit Mathur

I went through exactly same situation. If you just read the text book, you will feel that it is easy. So, it is recommended to practice in java. Always ask the question "Can it be done better?" Designing the algorithm by implementing a real life problem and relate underlying data structure will clarify the

Page 10: How to Learn Data Structures

concepts. I found the course by Kevin Wayne and Robert Sedgewick on coursera very useful.  Written 3 Jul, 2013 • 1,594 views

Upvote2

DownvoteCommentShare1

Jason Daniels9 upvotes by Pasquale Ferrara, Datt Goswami, Sambit Mishra, Randriamanampisoa Riana, (more)

I do it by studying and getting hands on practice on non-trivial problems. I don't wait to have a course on it to study it. And if I need to know it want to know it badly I keep studying it, day and night until it clicks. The practical hands on part is absolutely essential to my learning.

So how can you do it?1. Keep track of how you learn certain types of things best.

2. Apply that knowledge to this problem domain. (i.e. LEARNING about data structures and algorithms) In otherwords, learn how to learn this subject. 

3. When possible build off of something you do know well.

4. Involve others who do "get it." Learn from them. Choose competent yet patient people.

5. Stick with it. This is not an easy subject to master. Mastering it will help you in the long run even if you never work for Google or anyone that big.  Written 19 Jul, 2014 • 1,858 views

Upvote9

DownvoteShare

There's more on Quora...

Pick new people and topics to follow and see the best answers on Quora.

Update Your Interests

RELATED QUESTIONS

What does actually mean " good knowledge of data structures and algorithm ", what recruiters mean by this?

How do I visualize some basic data structures and algorithms?

What knowledge is obtainable from data structures and algorithms?

How do I improve my coding skills and improve my knowledge of algorithms and data structures in my 2 month summer holiday?...

What are the best resources for me to increase my knowledge in data structure and algorithms?

Page 11: How to Learn Data Structures

I want to study data structures and algorithms. But my programming knowledge is not so good. What can I do?

I have to revise Data Structures and Algorithms in a month's time with as much deep knowledge I can. How shall I go about ...

How do I improve my knowledge in string data structure?

Where can I find difficult algorithm/data structure problems?

How can I gain expert level knowledge of data structures and algorithms in 1 year?

More Related Questions

Top Stories from Your Feed

Read In Feed

Answer promoted • 16 Jul

Why do Bengaluru youth feel insecure about speaking in Kannada? What should be done to promote such a rich language?

Rakshith S Ponnathpur, Swalpa aDjust maaDkoLalla, ond kelsa ... 32 upvotes by Sughosh Kaushik, Preethi Sridhara,Anushka Aggarwal, Kiran Kumar, (more)

Two major reasons 

1) Many of the times it's not insecurity but uncertainty 

I'll tell you what happens many of the times. You meet a person for the first time and since this is Bengaluru, you ha...

Read In Feed

Top content on Quora

What are some uncommon advantages Indians can have over people of other nationalities?

Makarand Apte, Enthusiast in Math, Music and Coding14.5k upvotes by Shaily Gandhi, Udayaditya Dwivedi, Jitendra Choudhari, Rajat Tham, (more)

"I have to make a personal visit to Cambridge for 3 days during my UK trip. The hotels in Cambridge seem to be too expensive", my friend was telling me.

My aunt overheard our conversation and said, "...

Read In Feed

Top content on Quora

Page 12: How to Learn Data Structures

I failed three years in IIT and betrayed my parents. Should I die (see details)?

Abhishek Thakkar, Graduating this year after 16yrs9.5k upvotes by Dhruv Narayanan, Ravindra Dupakuntla, Ila Chauhan, Shreya Yadav, (more)

EDIT 2: I am new to Quora writing, it doesnt allow me to write replies to some messages (the button is simply not there, and then there's no way to track or flag something for later reply. I've rep...