Data Structures Flashcards

1
Q

What’s the difference between a Vecor and an Array list?

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

What is the difference between ArrayList and HashSet?

A
  • ArrayList is backed by an array while HashSet is backed by a hashmap.*
  • ArrayList allows duplicates, HashSet does not.*
  • ArrayList insert by the order of insertion. HashSet inserts randomly.*
  • ArrayList is index-based(get,set), while HashSet is object-set*
  • HashSet is a Set while ArrayList is a List*
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When should we prefer an ArrayList over LinkedList and vice versa?

A

ArrayList - when we more freuently need to search for elements rather than add/remove elements.

LinkedList - when we more frequently need to add/remove elements rather than search for elements.

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

What are the similarities and dissimilarities between HashMap and ArrayList?

A

Similarities:

  • Both are not synchronized
  • Both iterators are fail-test. Thas is, if they detect any structural change they will throw an error.
  • Both allow Null
  • Both allow duplication
  • Both are internally implemented using an Array
  • Both get method take O(1)

Dissimilarities

  • HashMap implements Map interface
  • ArrayList Implements List interface
  • HashMap stores two objects - key and value
  • ArrayList stores only a value
  • Keys of HashMap must implement equal and hasCode methods.
  • ArrayList does not have to but it’s recommended it to have equal method because its contain method uses it.
  • HashMap doesn’t support ordering
  • ArrayList supports ordering
  • HashMap doesn’t allow key duplicates
  • ArrayList get() is always O(1). HashMap is O(n) in the worstcase.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the difference between Set, List and Map?

A

These are interfaces

  • List - ordered, indexed, duplicates
  • Set - unordered, no duplicates
  • Map - unordered, duplicates only with values.

When to use

  • List - we need order and duplicates and it’s find for us to search using an index.
  • Set - we don’t want duplicates
  • Map - we want to find elements using a key. For instance, looking for a student using its ID
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between an HashMap and HashTable?

A

Similarities

  • Both implement Map Interface
  • Both work on the principal of Hashing
  • Both satisfy put and get in O(1) if objects are distributed uniformly
  • They are part of the Collection framework

Dissimilarities

  • HashMap is not thread-safe. HashTable is
  • Since HashMap is not thread-safe, it outperforms HashTable.
  • HashTable is obsolete. ConcurrentHashMap is better
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

HashMap and ConcurrentHashMap differences.

A
  • HasMap is not thread-safe. CHashMap is.
  • We can make HashMap synchronized by wrapping it with Collections.synchornizedMap(HashMap) which returns a Collection almost equivalent to HashTable. That is, every modification follows a lock of the Map object. On the other hand, ConcurrentHashMap divides the map and locks only parts of it. It is less safe, but of better performance.
  • With single threaded both are comparable while HashMap is slightly better
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Scalability?

A

An application has good scalability if it can maintain its performance while its volume of data or requests increase.

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

What’s the difference between HashTable, Concurrent HashMap and ConcurrentHashMap?

A
  • HashTable locks the Map on iteration which causes a degrade on performance when data is large. ConcurrentHashMap uses segmentation. How large it becomes only parts of it are locked while readers can access the other parts.
  • ConcurrentHashMap does not allow null values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Differences between HashMap and LinkedHashMap

A
  • Both are similar in performance
  • LinkedHashMap extends HashMap
  • LinkedHashMap maintains an order. Default is order by insertion though by access is also optional.
  • Access order is of course affected by the methods get, put and putall. When an entry is accessed it moves towards the end of the doubly Linked list. It is used to implement an LRU cache.

It is a good compromise between HashMap and TreeMap. With TreeMap we get increased cost of iteration due to sorting and performance drops to log(n). With LinkedHashMap time is constant.

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

Differences between HashMap and TreeMap

A
  • TreeMap maintains by default a natural increasing order over the keys, though it is possible to define a comparator. We can use methods that will sort from a given name A to a given name Y(at attahed)
    *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe the SortedSet interface

A

Extends Set interface to handle sorted sets in ascending order.

important methods:

  • headSet(Object start) - all elements <= start
  • tailSet(Object end) - all elements >= end
  • subSet(Object start, Object end) - all between
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What’s the purpose of the Map.Entry interface?

A

I have a Map and i want to recieve a set of the combined object <key> to get information about and possibly change.</key>

  • The method entrySet() returns a set of object of this type.
  • methods:
    • getValue
    • getKey
    • setValue
    • compare
    • hashCode
How well did you know this?
1
Not at all
2
3
4
5
Perfectly