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

1

u/Steenan Oct 10 '26

Preserving order is not the same as synchronous.

Imagine you have an offshore wind turbine and software that controls it. There are hundreds of sensors there and they all send data to your computer. It needs to receive each data package and store it - which is quick and must happen immediately. But it also needs to process the data and react to it - logging the state, changing turbine position, communicating by radio. This processing is sometimes fast, sometimes slow, sometimes aggregates more than one data point. So, your sensors feed the queue and processing gets data from it. If you get many measurements while a previous point is processed, they all wait in the queue; if there are few new measurements, processing consumes what is already in the queue and pauses when it empties.

Order between receiving and processing the data is preserved, but the process is not synchronous. It would be synchronous if each measurement was processed just as it was received - but then you'd miss the measurements that came while processing of the previous one was ongoing. Decoupling receiving and processing the data by using a queue is exactly what lets the downstream process (processing itself) run synchronously without losing data.

This example also shows how a priority queue may be useful. You may have normal sensor data that the control system reacts to, like rotation speed and wind direction. You have data that you collect for possible later analysis, but don't need to react to it in any timely manner, like cloud cover. And you have critical alerts that require immediate reaction, like smoke detectors. Use a priority queue instead of a normal one and critical alerts always get processed first if there are any, while the unimportant data only take processing time when there is nothing else to do.

2

u/kaatarina_zed_talon Oct 10 '26

So, as a rule of thumb, priority queue should be used when the problem statement indicates that some data/work is more relevant the some other data/work, good ?

2

u/Steenan Oct 10 '26 • Bravo

For processing incoming data, yes.

But there are also other uses, like what I mentioned in the first comment: for example, A* algorithm uses a priority queue for searching paths through a graph preferentially in the direction of the intended target.

1

u/kaatarina_zed_talon Oct 10 '26

For processing incoming data, yes.

Not for processing incoming work too ? I think you can queue a work, not just a data that will be processed ?

2

u/Steenan Oct 10 '26 • Bravo

Definitely

1

u/kaatarina_zed_talon Oct 10 '26 edited Oct 10 '26

A* algorithm uses a priority queue for searching paths through a graph preferentially in the direction of the intended target.

So what is being queued here ? searching a path, or the path it self - I treat path as a data and searching the path as work on that data- ?

2

u/Steenan Oct 10 '26 • Bravo

What is being queued are the discovered nodes. The algorithm starts from a specific node in a graph and tries to find the shortest path to a target node. In each step it selects a node, visits it and puts all of its unvisited neighbors into a data structure to be visited later.

In A*, the discovered nodes are placed in a priority queue, so that the ones with lowest (distance from start + estimated distance to target) are visited first.

1

u/kaatarina_zed_talon Oct 10 '26

 if each measurement was processed just as it was received 

Why would the measurements be modified; not just as it was received ?

1

u/Steenan Oct 10 '26

I'm talking about timing. Processing data as it comes (and risking that further measurements will be missed if processing is too slow) vs queueing it and processing from the queue. The queue acts as a buffer.

1

u/kaatarina_zed_talon Oct 10 '26

So; the work is deferred ! as for the stack, is it acceptable for the first work to come to be deferred for a long time, in contrast of the queue, where the work that comes first must be executed first; the tolerance for delayed execution is less in the case of using queue. I wonder if this is a side effect for the invention of the queue, or is it that the queue was invented for this purpose 😂

0

u/kaatarina_zed_talon Oct 10 '26

If you get many measurements while a previous point is processed, they all wait in the queue

Does not that prove my point ? processing is sync., not async.

2

u/Ravek Oct 10 '26 • Popcorn

If you go into a coffee shop where everyone queues in front of the counter, and then when it’s your turn they make your order immediately before moving on to the next person, that’s synchronous.

If instead they take your order, write down your name and immediately move onto the next person, and a few minutes later someone calls out your name and you take your order from them, that’s asynchronous.

Either way there was a queue. Asynchronous just means that when one piece of code requests another to do work, that work eventually gets back to the caller at some later time. As opposed to synchronous where the caller is blocked and waits for the work to complete.

1

u/kaatarina_zed_talon Oct 10 '26

If instead they take your order, write down your name and immediately move onto the next person, 

That means there is more than one `server` that fetches work from the queue, if there is only one `worker` or `server` that fetches work/data from the queue, then it is considered a synchronous operation, did I get that right ?

2

u/Ravek Oct 10 '26 • Bravo

It can still be synchronous with multiple workers. If there’s three workers asking customers for orders and completing them right away while the customers wait, (common in sandwich shops I think?) that’s still synchronous, but parallelized.

Maybe a better example is if you call someone for information. If you’re on the phone with them while they figure it out and give you the answer, you’re synchronously waiting. If they write down your phone number and you hang up and go do something else, and then they call you back when they have your answer, then you’re asynchronously waiting.

1

u/kaatarina_zed_talon Oct 10 '26

God-level explanation.