Sets Flashcards

1
Q

Is a HashSet a good choice for finding out if an array has any duplicates?

A

Yes because a HashSet has only unique keys, so if you try to add a duplicate value to a set it will return false

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

What is the most common implementation of the Set interface?

A

Hash Set

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

What is the time complexity of Hash Set add, remove, contains operations

A

O(1)

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

What is the time complexity of Tree Set add, remove, contains operations

A

O(log n)

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

When is a Hash Set a good choice?

A

When you don’t want to maintain the order

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

When is Tree Set a good choice?

A

When you want to sort the elements according to some comparator

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

What is a Tree Set

A

Parent and children nodes, ordered using red-black algorithm (which is a self balancing binary search tree)

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

What is a Linked HashSet

A

It is between as Hash Set and Tree Set and is a doubly-linked list implementation of a Hash Set

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

What is a set?

A

A set unique set of keys

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

What are the 4 key characteristics of a Tree Set?

A
  • sorts elements in ascending order
  • does not maintain insertion order
  • unique values
  • not thread safe
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should you use a HashSet instead of a TreeSet

A

When you care about performance (speed) and not order.

A TreeSet is slower as it has to sort the elements after each insertion/deletion

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

What is the time complexity of add, remove, contains for LinkedHashSet?

A

O(1)

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

How does a Tree Set compare elements?

A

Using the compareTo method by implementing the Comparable interface

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

Can a TreeSet have a null element?

A

No, it will throw a null pointer exception

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

Can a HashSet or LinkedHashSet store null values?

A

Yes, a max of one null value because all elements are unique

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

When should you use a LinkedHashSet?

A

When you want to maintain the insertion order of the elements

17
Q

Describe the internal working of a Hash Set?

A

When a HashSet is created, it internally implements a HashMap.

The ‘add’ function that is used to insert an element into the Hash Set, internally calls the ‘put’ function since a HashMap would have been internally created.

Therefore, the set takes in unique values with the help of HashMap.