You are on page 1of 3

COMP201 – Data Structures

SI Session 2 – Week 3

Question One
State whether the following is True or False. If False, state a reason why.

1. The List, Set and Map interfaces are all sub-interfaces of the Collection interface.
False. The Map interface is not a sub interface of the collection interface.

2. When storing items in a HashSet, each item is stored in the order in which they were inserted.
False. The items are stored in a Hash table which may not be in the order in which they were
inserted.

3. If {30, 10, 50, 40, 60, 10} was added to a LinkedList, the order will remain the same.
True

4. If {30, 10, 50, 40, 60, 10} was added to a TreeSet, the order will remain the same.
False. The new order will be {10, 30, 40, 50, 60}

5. In a complete binary tree, the greatest number of comparisons done to find an element is n,
where n represents the total number of items present.
False. There will be log2(n) + 1 comparisons.

Question Two
1. What is the difference between a queue and a stack?
A stack stores elements in a Last in, first out manner. A queue stores elements in a First in, first
out manner

2. In the queue interface, what is the difference between the peek(), poll() and remove() methods?
The peek method retrieves the head element without removing it from the queue. The poll
method retrieves and removes the head element and if none present returns a null, whereas
the remove method retrieves and removes the head element and if none present, throws an
exception.
3. Consider the code below. Fill in the blanks and give the output where necessary.

import java.util.*;

public class StacksAndQueues{

public static void main(String[] args){


// 3.1 Create a stack that holds integers
Stack<Integer> stack =new Stack<>();
stack.push(10);
stack.push(36);
stack.push(25);
stack.push(96);
int sum = 0;
// 3.2 complete the missing portion of the while
while( !stack.isEmpty() ){
// 3.3 Is peek the correct method to use? No. It will loop forever. Use pop().
int n = stack.peek();
sum = sum + n;
// 3.4 What is the value of sum after 4 iterations (using peek) 384
}
}
}

Question Three
1. Consider the three concrete Map implementations and identify which would be best in each
scenario.
a. You have user defined classes and would like control over the way they are stored in the
Map.
TreeMap

b. You want efficient retrieval of data stored in the Map


HashMap

c. You want the Map data to remain in the order in which it was entered.
LinkedHashMap
2. In the construction industry, there are many materials that are used in the development of a
building. You have been approached a construction company to develop a mini application that
will keep track of the materials needed for each project. Each project is assigned a unique ID.
They have placed utmost importance in efficient retrieval and would also like the system to
avoid adding duplicates of materials needed. The materials should also be sorted in alphabetical
order.
Example, if Project 14567 have material specifications such as Bricks, Roof Sheeting, Cement,
Glass, Bricks, it should only capture Bricks, Cement, Glass and Roof Sheeting.
Hint: Use a container within the Map and pay careful attention to the data structures that you
choose to satisfy the requirements.

Suggested Solution

import java.util.*;
import java.io.*;

public class Construction{

public static void main(String[] args) throws IOException{


// File Reading
Scanner key = new Scanner(new File("construction.txt"));

// Map to store details


Map<Integer, Set<String>> details = new HashMap<>();

while(key.hasNext()){
// Read Line
String[] data = key.nextLine().split("#");
// Set to store materials
Set<String> materials = new TreeSet<>();
int ID = Integer.parseInt(data[0]);

for(int i=1; i<data.length; i++){


String material = data[i];
materials.add(material);
}

details.put(ID, materials);
}
System.out.println(details.toString());
}
}

You might also like