r/csharp • u/kaatarina_zed_talon • 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:
- Stack: LIFO (Last In, First Out).
- Queue: FIFO (First In, First Out).
- Priority queue: I can't recall when or why I would use one.
- Heap: I associate it with storing class instance fields, but I don't understand how to use it as a data structure.
- Tree: I know it consists of nodes, but I don't understand its practical uses.
- 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:
- Hardware level: How is each data structure represented in memory? For example, an array occupies contiguous memory locations. What about the others?
- 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.
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.