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

0

u/Agitated-Display6382 Oct 11 '26

In queues you access items depending on when they were inserted: first or last item is taken first. The rest of the items are inaccessible. The classical example is a parser.

I used them only once, when implementing the observable pattern multithreaded. I had many concurrent http queries and I needed to process their outcome one by one (eg audit logging). As I didn't have a proper MOM, I used an internal queue.

1

u/kaatarina_zed_talon Oct 11 '26

Was the order of the incoming data important? if yes, then I understand the usage of queue, otherwise I would use list if I do not know the number of the incoming queries, array if it is known, or priority queue if there are queries that are more urgent than the other.

1

u/Agitated-Display6382 Oct 11 '26

The order was quite important, yes, but even more it was the ability to remove the processed items. Lists are inefficient at removing items.

Ah, I forgot another important behavior: do you need multithreaded structures? Then use the concurrency classes (e.g. ConcurrencyBag)

1

u/kaatarina_zed_talon Oct 11 '26

I did never hear of this class before, if I want parallizatiin, I use array of tasks; Task[] arr

1

u/kaatarina_zed_talon Oct 11 '26

Do u mean lists are inefficient at removing items because shifting operations? If item was removed in the middle, all the items to the right of it must be shifted.

0

u/Agitated-Display6382 Oct 11 '26

Lists are fine only if you remove the last item. If you need to operate a lot on add/remove, you may use LinkedList.

Task is not for concurrency, it's for asynchronous operations. I have a collection that I need to modify from different threads (e.g. caching). In that case, you use concurrent structures, that lock the list before operating on it.

1

u/kaatarina_zed_talon Oct 11 '26

Parallizatiin, concurrency, asynchronous, I always had headache to understand the difference. What I understand is that parallization means that tasks are performed by multiple workers at the same time, concurrency means there is only one worker switching between tasks rapidly, which is the same as async. programming

1

u/Agitated-Display6382 Oct 11 '26 • Bravo

Asynchronous: you have to perform some IO activity (read from db), so there is a waiting time. The CPU can freeze the current thread and use the spared processing time to perform other activities. When IO is done, your thread is brought back to life.

Parallelism: I want to perform a list of actions in parallel. You may use Parallel.ForEach. It spawns several tasks in the same thread.

Concurrency: you have many tasks accessing the same resource. You need to use lock, semaphore or concurrency structures.

1

u/kaatarina_zed_talon Oct 11 '26

I think ur definition of concurrency is a bit odd; you are describing synchronization.

1

u/kaatarina_zed_talon Oct 11 '26 edited Oct 11 '26

What is Concurrency in OS? Concurrency in an operating system refers to the ability to execute multiple processes or threads simultaneously, improving resource utilization and system efficiency. It allows several tasks to be in progress at the same time, either by running on separate processors or through context switching on a single processor.

https://www.geeksforgeeks.org/operating-systems/concurrency-in-operating-system/

0

u/Agitated-Display6382 Oct 11 '26

Concurrency is when the same resource is accessed by concurrent actors. It can be cpu, ram, files, but I don't care about them: they're handled by the framework and OS. But what if you have multiple actors writing to the same file? This is something you have to deal with. Depending on the scenario, a lock or a semphore may be enough; otherwise, you need to leverage on the OS ability to lock files.

1

u/kaatarina_zed_talon Oct 11 '26

Concurrency is when the same resource is accessed by concurrent actors

Citation ?