Key - Practice Questions - Exam 2 (2)

.docx

School

Arizona State University *

*We aren’t endorsed by this school

Course

310

Subject

Computer Science

Date

May 26, 2024

Type

docx

Pages

5

Uploaded by ColonelDanger14164

Report
CPSC2108- Data Structures Practice Sheet 1. Write a method that takes an array of keys and an array of values and creates a corresponding Hashmap. public HashMap makeMap(E[] keys, E[] values) { HashMap map = new HashMap(); for(int i = 0; i < keys.size(); i++) { map.put(keys[i], values[i]); } return map; } 2. A hash table has buckets 0 to 9 and uses a hash function of key % 10. If the table is initially empty and the following inserts are applied in the order shown, the insert of which item results in a collision? HashInsert(hashTable, item 55) HashInsert(hashTable, item 90) HashInsert(hashTable, item 95) First and third 3. Knowing that hash table size is 10 and knowledge of the expected keys is 10, 20, 30, 40,…. How do you think the hash function key % 10 will perform (well or poor)? Why? Poor. High collision rate. 4. Suppose that you would like to create an instance of a new Map that has an iteration order that is the same as the iteration order of an existing instance of a Map. Which concrete implementation of the Map interface should be used for the new instance? A. TreeMap B. HashMap C. LinkedHashMap D. The answer depends on the implementation of the existing instance. Option C Explanation: The iteration order of a Collection is the order in which an iterator moves through the elements of the Collection.   5. We have a hash table of size 7 to store integer keys, with linear probing and a hash function
h(x) = x mod 7 (x mod 7 return the remainder of the integer division with 7). Show the content of the hashtable after inserting the keys 0,11,3,7,1,9 in the given order. 6. Mark all properties that are TRUE for a hashtable with n elements? (a) an ideal hash table using array doubling has worst-case time complexity of O(1) for every insert operation (b) an ideal hash table using array doubling has average-case time complexity of O(1) for lookups (c) can be used to sort an array of n real numbers with average-case time complexity O(n) (d) it is possible to have different keys being hashed to the same position in the array (b) and (d) are true 8. Indicate the complexity for all methods that is not marked x (Big O) 4. Method Definition 5. LinkedList <E> 6. ArrayList <E> 7. CircularQueues <E> 8. LinkedStack<E > 9. get(int index) O (n) O(n) n/a n/a add (int data) O(1) O(n) n/a n/a remove () O(1) O(1) O(1) O(1) peek() x x O(1) O(1) 9. Suppose a single-linked list contains three Nodes with data “him”, “her”, and “it” and head references the first element. What is the effect of the following fragment (use drawing to illustrate your answer): Node<String> nodeRef = tail; nodeRef.data = ”she”; 10. Would we use a stack or a queue to solve a postfix expression? Explain how would you use your chosen data structure to evaluate a postfix expression. Stack. Check the book for explanation. 11. Pick the queue implementation (circular array, single-linked list, double-linked list) that is most appropriate for each of the following conditions. This implementation is normally most efficient in use of storage. ( single linke dlist ) This is an existing class in the Java API. ( double linked list ) Storage must be reallocated when the queue is full. ( circuler queue ) 12. Consider the stack below to indicate the result of each operation and show the new stack if it is changed. names.push("Jane"); 0 5 4 3 2 1 0 1 11 9 3 7 0 tail head she her him
String top = names.pop(); String nextTop = names.peek(); names.push("Alie"); Jane Phillip Alie Phillip Dustin Phillip Dustin Robin Dustin Robin Bebbie Robin Bebbie Rich Bebbie Rich Rich 13. Assume that a linked queue q of capacity 5 contains the five characters +, *, -, & and # (all wrapped in Character objects), where + is the first character inserted. Assume that + is stored in the first position in the queue. What is the value of head.data? What is the value of tail.data? head.data= + tail.data = # 14. Remove the first element from the queue in the above question and insert the characters \ then %. Draw the new linked queue. What is the value of head.data? What is the value of tail.data? head.data = * tail.data = % 15. Determine the order of magnitude (big-O) for an algorithm whose running time is given by the equation T(n)= 3n 4 – 2n 4 log n + 100n + 37 O( n 4 ) 16. Write a main function that creates two queues of Integer objects and a stack of Integer objects. Store the numbers -1, 15, 23, 44, 4, 99 in an array. Read the numbers from the array and store them in the first queue. The tail of the queue should store 99. Write a loop to get each number from the first queue and store it in the second queue and the stack. Use stack and queue operations to implement this 17. Implement the pop method for the ArryStack ADT 18. Implement the enqueue method for the LinkedQueue ADT
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help