## Self-Review Questions

1. An iterator over an ArrayList object returns the items in
a)   insertion order
b)   comparable order
c)   random order

2. An iterator over a HashSet object returns the items in
a)   insertion order
b)   comparable order
c)   random order

3. An iterator over a TreeSet object returns the items in
a)   insertion order
b)   comparable order
c)   random order

4. Implement a method that prints a given ArrayList using an Iterator interface
`public void printList( ArrayList<Integer> data )`

```public void printList(ArrayList <Integer> data){
Iterator<Integer> it = data.iterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
}
```
5. Consider the following sequence of integer keys to be hashed into a hash table using the hash function H(key) = key modulo tablesize:
`11,  4,  5,  12,  25,  18`

Insert each key above, from left to right, into the hash table below using linear probing to resolve collisions.

 0 1 2 3 4 5 6 7 25 18 11 4 5 12

6. Given an array of size n. One of its elements is repeated twice. Design an efficient algorithm that would find that duplicate.

Create a hash set and traverse the array. Check if each element is already in the hash set, and if it is, then you've found the duplicate. Otherwise, add the element to the hash set.
7. Given an array of integers. Design an efficient algorithm that finds two numbers x and y such that x + y = 0. Consider two cases a)sorted array; b)unsorted array.

a)Do the same thing as in the previous question, but check if the element's complement is in the hashset.
b)Do the same thing as in the previous question, but check if the element's complement is in the hashset.
8. An anagram is a word or phrase formed by reordering the letters of another word or phrase. Here is a list of words such that the words on each line are anagrams of each other:
barde, ardeb, bread, debar, beard, bared
bears, saber, bares, baser, braes, sabre
baste, betas, beast, tabes, beats, bates, abets
caster, carets, recast, reacts, cartes, caters, traces, crates
caret, crate, react, trace, cater, recta, carte

Build a Map whose key is a sorted word (meaning that its characters are sorted in alphabetical order) and whose values are the word's anagrams.

9. Given an array of strings
```String[] words = {"Fred","bob","Tom","Mark","john","Steve"};
```

sort them ignoring the case.

```private class CompareIgnoreCase implements Comparator{
public void compare(Object o1, Object o2){
String str1 = ((String)o1).toLowerCase();
String str2 = ((String)02).toLowerCase();

return str1.compareTo(str2);
}
public void equals(Object obj){
String str1 = ((String)o1).toLowerCase();
String str2 = ((String)02).toLowerCase();

return str1.equals(str2);
}
}

Arrays.sort(words, new CompareIgnoreCase());

```
10. Given an array of email addresses
```String[] words = {"Fred@cmu.edu","Bob@yahoo.com","Tom@gmail.com",
"Mark@ucla.edu","John@pit.edu", "Steve@microsoft.com"};
```

sort them by company/institution names.

```private class CompareCompany implements Comparator{
public void compare(Object o1, Object o2){
String[] str1 = ((String)o1).split("@.");
String[] str2 = ((String)02).split("@.");

return str1[1].compareTo(str2[1]);
}
public void equals(Object obj){
String str1 = ((String)o1).split("@.");
String str2 = ((String)02).split("@.");

return str1[1].equals(str2[1]);
}
}

Arrays.sort(words, new CompareCompany());
```