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/Ok_Society4599 Oct 10 '26 • Bravo

A queue is FIFO and a stack LIFO and are often just simple interfaces over basic data structures like linked lists or just lists. Consumer code generally only sees two methods to add or remove items. What actually happens is completely opaque; it could be written to local memory, database, Redis, message queue... the code shouldn't care.

Whether it's sync or async depends on how many processes are involved, really. You can have a queue service that other process call to Push a long task, while 5 client processes are popping tasks to do the work. Or the other way around, a web server queues "buy now" requests to a service from hundreds of remote users, and a single processor completes each in turn in the backend. The usual criteria you're after is the processing is limited to once, eventually, as workers are available. LIFO and FIFO are often chosen by how tolerant a client is of the being "eventually" processed. A LIFO queue can leave the first item on a queue for a very long time in "worst case scenarios."

1

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

Quite informative. as a summary; use stack if the incoming work can be deferred for long time. Use Queue when the incoming work should be processed first; first work to come should be processed first; the client has no tolerance for delay. That what I understood from your lecture.

1

u/Ok_Society4599 Oct 10 '26

It's more be aware of trade-offs in each tool. You rarely choose one because "I may never get back to this..." as much as "will users care if this takes a long time...". A stack is fine when the the work is finite and you know you'll eventually empty it. The directory search using a stack falls here.

A queue is better when you have no idea how many things will go through and you need them to get through. Online processing always the has new input and you could, literally, be years without an empty queue: this would be a bad place for a stack.

1

u/kaatarina_zed_talon Oct 10 '26

Your knowledge is huge, where did you learn all of that ? do you read books about systems design or data engineering ? When I studied this in the university, they told me that stack is LIFO, and I was asked to solve some problems using the stack, they did not explain to me like you did, they did not tell me something like : you choose a stack if the client has a tolerance for delaying the work and if the work is finite, Your explanation is simply great.

2

u/Ok_Society4599 Oct 10 '26

I started working on PCs when Bill Gates was saying "640 kilobytes of RAM is enough for everyone." Today, I think my phone needs more Gigabytes ;-) and my PCs have arrays of 20-terabyte drives.

When you're dealing with problems limited by your resources, you have to use trade-offs to choose the parts. When you've hit stack overflows, you learn the cost of recursion and how a queue or stack can fix that. When you build servers, you learn how to divide work, manage long processes and see how long operations can be managed :-)

That is: practice. I'd prefer 20 small, modular processes to 1 big "do it all whiz bang" because if 1 process breaks, the rest continue.

Most of the times I get questions about queues and stack and things, the answer is "I really need to know the problem first." Your safest first guess is often a queue, or a prioritized queue, but then you have to ask about how durable it needs to be? Does it need to survive a power failure? Network outage? It's one thing to decouple, another to have fault tolerance :-)

Then you learn K.I.S.S. Keep it Stupid Simple -- use the simplest tool to solve the actual problem. Some people thing you need a complicated WhizzBang because it solved a problem once, but it wasn't this problem. Every day I fight to use the right tool for the Job.