Collections Flashcards

(28 cards)

1
Q

What is yield return

A

It makes a method able to return sequence of values one at a time. The return type must be ienumerable or IEnumerator. It is memory efficient for large collection as lazy evaluation

It is better clean code than manually implementation of IEnumerator for lazy evaluation. Easy to return early and save performance

Better memory efficient than building collection and returning

Compiler transforms into state machine that pauses at yield and return then resume

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Array vs ArrayList

A

Array is for collection of homogeneous types without fixed size.
Arraylist is for heterogeneous types with ability of dynamic sizing. It is slower due to boxing of value types. It is prone to runtime errors due to not having compile time type safety.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array vs List

A

Array and list both hold same types with compile time type safety ensured. Both are stored in contiguous memory. Both preserve order

However array is fixed size. List allows dynamic sizing with special methods like add remove etc. These should be manually done in arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

ArrayList vs List

A

List for homogeneous type safety
Arraylist for heterogeneous no type safety with boxing Unboxing overhead and is legacy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Non generic vs generic Collections

A

Boxing Unboxing of value type overhead
Prone to runtime cast errors
No compile time type safety

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List vs Dictionary

A

Order not guaranteed. Hash based key lookup. Unique keys

Index access faster. Dictionary key access can slow down if key collision

Index insert is o(n) and Dictionary is 0(1) because it just have to drop in hash based bucket of keys

List light. Dictionary heavy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

IEnumerable vs IQueryable

A

IEnumerable is root interface for all collections . It represents sequence. It operates on in memory collection within the running code with lazy evaluation. It has extension methods for linq to object

IQueryable extends IEnumerable to operate on Collections that are outside memory. It builds query expression trees for the data source provider and the data source executes it later as deferred execution. This way it can save network payload to get only filtered data. Delegate load to data source which is optimised for querying. It has extension for linq to entity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

why arrays are still used when we have List<T></T>

A

Performance-critical scenarios

When size is known in advance

Lower memory overhead

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why use yield return instead of returning a List?

A

Saves memory for large sequences, supports lazy evaluation, and keeps code concise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

IEnumerator

A

It represents a cursor that helps foreach move forward the sequence. It has current property and movenext() and reset()
It converts foreach into state machine

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why use IEnumerable

A

Stream collection. No load on memory issues. Deffered execution. Just have to read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Evolution of collection Interfaces

A

IEnumerable is the root and represents a sequence. It provides read only and forward only iteration on it. It supports Deferred execution on in memory collection

ICollection extended IEnumerable to provide collection Manipulation features like count capacity contains and add remove clear

IList extends Icollection to provide index based fast access on o(1). It guarantees order preservation die to contiguous memory. Methods like insert and remove At

DICTIONARY extends collection to key value based collection with unique and no null key. Fast key lookup which is hash based. Order is not guaranteed

Set extends collection to give a set of unique elements for easy math operations. It is unordered collection

IQueryable extends IEnumerable to delegate the query execution to external provider. It supports Deferred execution on remote source. It builds expression tree and the source executes it in an optimized manner and the remaining result usually smaller than getting whole table saves on network bandwidth and response time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where to use icollection

A

Session. Need to add remove count contains. No need for index access.

Or like blacklisting of refresh tokens. No need for order

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Where to use IList

A

Playlist.

Reoder. Select a song fast.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where to use Dictionary

A

Contact phone book.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where to use set

A

Followers on social media

17
Q

Where to use IQueryable

A

Don’t load millions of users. Query on db

18
Q

Access times of list and Dictionary

A

O(1) for list

O(1) for key but o(n) if key collisions

19
Q

Insert times for list and dictionary

A

O(n) for list

O(1) for dictionary because hash based drop

20
Q

Capacity vs Count

A

Count is the actual no of elements

Capacity is size of internal array at present. It increases to almost double when resizing.
We can set a Capacity value to save on space if known. Because at declaration it is twice the size.

21
Q

Why foreach throwing error

A

IENUMERABLE should not be modified while iteration

22
Q

Difference between clear and new

A

Clear same capacity
New resets capacity
Release memory

23
Q

Contains failing on objects

A

Because of reference equality match.

Override equals and gethashcode

24
Q

Remove not removing everything

A

Only first occurrence
Returns true if found

25
What is trimexcess
Reduce capacity to count
26
Try get value vs contains
Try get value avoids double lookup
27
Upsert in dictionary
Dict[key] : overwrite TryAdd : insert if absent
28
Not finding key but it is therr
Don't mutate key Case sensitive Unique non null keys