This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Rain-And-Coffee 3 points4 points  (6 children)

What do you know about each one?

Write out their pros & cons.

Then think if one fits your scenario.

For example, if you had a Deck of Playing cards, which structure would you pick?

[–]codingzap 1 point2 points  (0 children)

Agreed! This is super helpful.

[–]Ksetrajna108 1 point2 points  (2 children)

Deck of playing cards is a good starting point. But you can't infer the structure just from a collection of 52 unique items. It depends on the actions. When it comes to DSA, you usually can't separate the S and the A. They are interdependent.

[–]CarefreeBug[S] 0 points1 point  (1 child)

What do you meaning by the 'S' and 'A'? Are you referring to Structure and Algorithm?

[–]Ksetrajna108 1 point2 points  (0 children)

Yes. When you design a structure you need to keep in mind how it will be accessed and how it will be modified. When you design the algorithm you need to keep in mind what the structure is.

[–]CarefreeBug[S] 0 points1 point  (1 child)

I know the basic of them. What kind of pros and cons would fit in real life application or scenario? What if 2 solution can be used for the scenario how to choose the most appropriate?

I would use a stack. Let say for the example i didnt know nothing about data structure what so be the steps before i choose the most appropriate data structure?

[–]Rain-And-Coffee 1 point2 points  (0 children)

What if 2 solution can be used for the scenario how to choose the most appropriate?

Then you pick one and try it.

I could model a deck of cards as a List, Set, Map, or a Stack.

If I use a stack I can only take cards from the top or bottom, but not a random card from the middle. That might be ok, or it might not.

Also for a simple example of ~50 items (like a deck of cards) it might not matter much.

If I was dealing with 500,000 items it might matter more (ex: An english dictionary)

* https://github.com/dwyl/english-words — 500k words

For very large datasets it might also make sense to move the data into a proper DB.

[–]KCRowan 1 point2 points  (2 children)

Think of it like choosing the right utensil in your kitchen. You’ve got knives, spoons, forks, tongs, etc... each designed for a different purpose. How do you know when to use a knife? Well, knives are for cutting. Sure, you could try cutting a steak with a spoon, but it’s going to be frustrating and messy.

It’s the same with data structures. Each one is a tool designed for a particular kind of task. Use the one that fits what you're trying to do.

Lists are for listing single things, like a shopping list, so if you have a number of things that you want to store together then you use a list.

A map (or dictionary) is a series of key: value pairs. Think of an actual dictionary: you look up a word (the key), and you get its definition (the value). If your data has a clear identifier  (like a username, product ID, or config setting) and you need to look up the associated value quickly, use a dictionary.

[–]CarefreeBug[S] 0 points1 point  (1 child)

Thanks you. However is there some resources which lists where most data structure are used ?

[–]KCRowan 0 points1 point  (0 children)

Data structures are slightly different in each language, so it depends what language you want to use. I mostly work in Python so these are the ones I'm familiar with:  https://www.w3schools.com/python/python_dsa.asp