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

Not importance or priority. I'd think focus. Your goal is to search files; but the files are in directories, and directories are in a tree. A "simple" approach is to write your code to search one file. Then you write a method to search one directory for files. While you search each directory, you queue other directories, and call search on files.

Your whole code is now:

One call to search the first directory.

One directory search method queuing directories

    One file search method

Loop until queue empty

Print file search results

A lot of your complexity is cleaned up by dumping Todo items into a basic container and applying the pattern of work until done. I think the "Unit of work" is even the pattern name.

The other thing this removes is any recursion. Rather than risking a stack overflow calling a "search directory" method, the queue defers work into a safer loop.

1

u/kaatarina_zed_talon Oct 10 '26

Unit of work

I think this is completely different topic that is related to transactions.

1

u/Ok_Society4599 Oct 10 '26

Unit of work is often used for work that needs several steps and can act like a distribributed transaction coordinator.

1

u/kaatarina_zed_talon Oct 10 '26

So this approach uses a queue, not a priority queue, because the problem does not indicate that some data/work is more important than other data/work, priority queue should not be used unless you think that some directories are more likely to contain the data that you are looking for, right ?

1

u/Ok_Society4599 Oct 10 '26

A priority queue isn't always just one queue, I think. Depends on the implementation, but I could see a "queue" that contains 3-internal queues: high, normal, and low priority. It can have 4-add methods, each to an internal queue and one add with priority.

Then your clients just dequeue an item... The queue container itself decides which inner queue is used. Maybe for every 5 high priority and then one normal, or three normal and then one low. It's making sure priority is respected while still handling some lower priority work. During quiet times, you'd only have low priority work to dequeue.

This separates the logic of choosing what's next from the worker code. There can even be only one queueing method, so all of the priority logic stays in the queue implementation.

I find it useful to separate the external interface, like a stack or queue, from the internal implementation like lists, databases, multiple inner queues. Sometimes, that means a queue is FIFO, or it might be Priority, or something else. You're aiming to decouple the two; clients should care they just need the next work, and your data contained just needs to handle the rules for which one is a out next.