Sets Flashcards

(48 cards)

1
Q

What is a set in data structures?

A

A collection of unique elements with no specific order.

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

What is the key property of a set?

A

It does not allow duplicate elements.

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

What are common operations on a set?

A

Insert, delete, search, union, intersection, difference.

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

What is the time complexity of insert in a hash set?

A

O(1) average case.

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

What is the time complexity of search in a hash set?

A

O(1) average case.

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

What is the time complexity of delete in a hash set?

A

O(1) average case.

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

What is a hash set?

A

A set implemented using a hash table.

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

What is a tree set?

A

A set implemented using a balanced binary search tree.

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

What is the time complexity of operations in a tree set?

A

O(log n).

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

What is a subset?

A

A set where all elements are contained in another set.

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

What is a superset?

A

A set that contains all elements of another set.

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

What is the union of two sets?

A

A set containing all distinct elements from both sets.

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

What is the intersection of two sets?

A

A set containing only elements present in both sets.

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

What is the difference of two sets?

A

A set containing elements in the first set but not in the second.

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

How do you check if an element is in a set?

A

Use the contains or β€˜in’ operator depending on the language.

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

What is the space complexity of a set?

A

O(n), where n is the number of elements.

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

What is the benefit of using a set over a list?

A

Faster lookups and guaranteed uniqueness.

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

What is a multiset?

A

A collection that allows duplicate elements.

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

What is the main disadvantage of sets?

A

They do not maintain insertion order (in hash sets).

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

What is an ordered set?

A

A set that maintains elements in sorted or insertion order.

21
Q

What happens if you try to insert a duplicate into a set?

A

The duplicate is ignored.

22
Q

What is the mathematical definition of a set?

A

A well-defined collection of distinct objects.

23
Q

How do you iterate through a set?

A

Using a for-loop or an iterator.

24
Q

What is a power set?

A

The set of all subsets of a given set.

25
What is the size of the power set of a set with n elements?
2^n.
26
Can sets contain other sets?
Yes, if the inner sets are immutable (hashable).
27
How is a set different from a dictionary?
A set stores only keys; a dictionary stores key-value pairs.
28
How do you remove an element from a set?
Use the remove or discard method.
29
What is the difference between remove and discard?
Remove raises an error if the item is missing; discard does not.
30
What is a frozen set?
An immutable version of a set.
31
How do sets help in finding duplicates?
By checking if an element already exists before adding.
32
What is a use case for sets in string manipulation?
Finding unique characters in a string.
33
How do you compute the symmetric difference of two sets?
Elements in either set, but not both.
34
What is the result of intersecting a set with an empty set?
An empty set.
35
What is the result of the union of a set and an empty set?
The original set.
36
Are sets ordered in Python?
No, but since Python 3.7, insertion order is preserved in practice.
37
Can sets be used as dictionary keys?
No, but frozen sets can.
38
What is the advantage of sets in graph problems?
Efficient membership checks for visited nodes.
39
Can a set be null?
No, it can be empty but not null.
40
What is the syntax to create a set in Python?
Use set() or curly braces: {1, 2, 3}.
41
How do you clear all elements from a set?
Use the clear() method.
42
What is the use of sets in database operations?
To perform union, intersection, and difference on result sets.
43
Can you convert a list to a set?
Yes, using set(list_name).
44
How do sets handle collisions?
Via the underlying hash function and collision resolution of the hash table.
45
What is the expected time complexity of checking membership in a set?
O(1) on average.
46
What data structure underlies a hash set?
A hash table.
47
What is the cardinality of a set?
The number of elements in the set.
48
Are set operations associative?
Yes, operations like union and intersection are associative.