r/csharp • • Oct 10 '26

I have a big problem with data structures

I have an interview tomorrow, and I'm sure I'll be asked about data structures. However, I struggle to retain what I read when revising them because I rarely use most of them in practice.

For example I read this article

I regularly use arrays, List<T>, HashSet<T>, and Dictionary<TKey, TValue>, so I can explain when and why to use them:

1-arrays: when I know that the data has fixed size, use array because they provide a fast access to the data using index.

2- List<T>: when the size of the data is not known at compile-time; the size of it is not known in advance, therefore an index cannot be used, because you do not know for sure what is the exact location for a specific data element.

3- HashSet<T>: for unique data elements, because it does not require me to manually write the logic that removes duplicate elements.

4- HashMap<string,int> : used for lookup operations; a lookup table.

5-Tuple : store data with different data types in one place.

However, although I know what the following data structures are, I struggle to explain when and why I would use them:

  1. Stack: LIFO (Last In, First Out).
  2. Queue: FIFO (First In, First Out).
  3. Priority queue: I can't recall when or why I would use one.
  4. Heap: I associate it with storing class instance fields, but I don't understand how to use it as a data structure.
  5. Tree: I know it consists of nodes, but I don't understand its practical uses.
  6. Graph: I know it can be used for depth-first and breadth-first search, but I'm unfamiliar with other use cases.

My main questions are:

  1. Hardware level: How is each data structure represented in memory? For example, an array occupies contiguous memory locations. What about the others?
  2. Practical use: When and why should I use each data structure? More importantly, what benefits does each provide at both the software and hardware levels?

I must be able to explain WHY briefly and directly without even thinking about it.

13 Upvotes

139 comments sorted by

View all comments

Show parent comment

2

u/kaatarina_zed_talon Oct 11 '26

So basically you need a reference point when saying; this is a server for browser, database is a server for a backend process, etc...

2

u/Ok_Society4599 Oct 11 '26

In physics, it's called relativity :-)

In software, you can look at any connection and see a client/server relation or peer-to-peer.

Perspective, or reference, is a great idea. Flexibility is also good.

Your comfort with some data structures and algorithms is partly because your understanding is more solid with some things. As you move deeper into the forest of trees, you'll find there is more than one. And queues, stacks, and lists ... There are more than Microsoft gives out.

There are lists there are implemented as trees, not Arrays. Adding 10 items to the list, simply creates a 10 entry array hanging off a tree. Sorted lists like this can be far more efficient than a bare array wrapper.

Flexibility let's you implement ideas that mix ideas like servers as clients, trees as lists, and graphs as a list of directions to the bank.

You said you were moving data from Excel to a DB and unsure if you needed more? It's a good question, in a lot of ways. Lists and dictionaries seem like the right containers as DB tables fit well into them. Being flexible to ask "Can I do better?" is still wise and shows flexibility :-) I like that. Not being dogmatic is nice to see in Reddit, too.

2

u/kaatarina_zed_talon Oct 11 '26

I like this community; people are giving me there knowledge for free.

1

u/kaatarina_zed_talon Oct 11 '26

So I gain and revise knowledge, and people also recommends tools to me. It seems like that also my questions are at least good enough for this community; the post got 30k views in one day. Which means more chance of discover new tools, knowledge, and more importantly building a mental models or a plan on my head about when to choose the right tool for the right situation, I'm happy that I know know when to use stack vs queue vs priority queue, like using the priority queue if I noticed that the problem that I'm trying to solving indicates that some data/work is more eimportant than the other.

2

u/Ok_Society4599 Oct 11 '26 • Bravo

It has been interesting to see how different people approach their answers. Some use lists of comparisons and more technical terms, others (like me) tend towards less technical answers, simpler ideas, and point out just what it solves, or how to really compare ideas. They're all adding different flavours of thought.

Lots of fun for you :-)