Professional Documents
Culture Documents
COMP201 SI Worksheet 3 - Suggested Soln
COMP201 SI Worksheet 3 - Suggested Soln
Question One
For each of the following, state whether the statements are true or false. If false, provide a reason.
1. If two strings s1 and s2 are equal, then their hash codes are the same.
True
2. It is possible to traverse from index 0 to index n-1 in a set, where n-1 is the last element in the
set.
False. You cannot traverse a set via indexes. To traverse a set, you must first obtain an iterator,
which will then allow you to traverse the set from beginning to end.
3. If you need to store non duplicate elements in the order of insertion, the best option is to use a
HashSet.
False. You should rather use a LinkedHashSet.
4. If you need to frequently insert and delete elements at the beginning of a list, the best option is
to use an ArrayList.
False. A LinkedList will be more appropriate.
5. Suppose there is a method that loops through each item in a list to find a specific value. In this
case, the estimated worst time complexity is O(n).
True
Question Two
For the code snippets below, calculate the number of times the printing code will be displayed if n=10,
n=20 and n=30. Thereafter, use the Big O notation to estimate the time complexity.
1.
3.
Question Three
For the code below, work out the time complexity.