S23-solutions (3)

.pdf

School

Rutgers University *

*We aren’t endorsed by this school

Course

112

Subject

Computer Science

Date

Apr 26, 2024

Type

pdf

Pages

9

Uploaded by AgentCaribouMaster1086 on coursehero.com

CS112 Data Structures - Final - Spring 2023 Name: _____________________________________ NetID: __________________________ 1 WRITE your name and NetID on EVERY page. DO NOT REMOVE the staple on your exam. WRITE NEATLY AND CLEARLY. If we cannot read your handwriting, you will not receive credit. Please plan your space usage. No additional paper will be given. This exam is worth 150 points. This exam has 10 pages, make sure you have all the pages. Problem 1 – Miscellaneous (25 points) a) (10 points) Answer TRUE or FALSE for each of the following statements. 1. The worst-case running time to search in a singularly linked list with n items is O(1) time. False 2. Deleting the first node in a circular linked list with n items takes O(1) time. True 3. Deleting the first node in a singularly linked list with n items takes O(n) time. False 4. Insertion into a balanced BST with n items takes O(log n) time. True 5. The worst-case running time to search for a value in a 2D n x n array takes O(n) time. False 6. Stacks can be used to represent the line of students waiting for office hours where students are seen in a First-In-First-Out manner. False 7. A BST guarantees O(log n) for searches on a tree with n items. False 8. Stacks can be used to reverse a list of objects. True 9. The maximum height of a 2-3 tree with n items is log 2 n. True 10. Union find can be used to determine if two items are connected. True [1 point] each correct b) (5 points) Assume a linked list of n integers, where n > 0 . What does the mystery method print? What is the running time of the method mystery in big O notation? [4 points] Prints out the minimum value in the linked list. [1 point] O(n)
CS112 Data Structures - Final - Spring 2023 Name: _____________________________________ NetID: __________________________ 2 c) (10 points) Assume a sorted array of n items with duplicates, where n > 0 . Each item could appear at most k times. When searching for an item, the search algorithm is modified to display the first and last indices of the item in the array. If the item is not found the algorithm displays -1 and -1. For example, the search for 36 displays 5 (first index) and 10 (last index). An item appears at most k times. Briefly describe the fastest search algorithm to display the first and the last indices of a sequence of non- distinct items. Your algorithm may not modify the array or use extra memory. What is the algorithm’s running time in terms of n and k ? Explain. [0 points] for any algorithm that is takes longer than log n + k Algo [4 points] Use binary search to find the item. [1 point] Scans left until it finds a distinct item, display start index. [1 point] Scans right until it finds a distinct item, display end index. Running time [1 point] binary search takes log n. [1 point] scanning left and right takes at most k checks. [2 point] running time is log n + k
CS112 Data Structures - Final - Spring 2023 Name: _____________________________________ NetID: __________________________ 3 Problem 2 – Priority Queue (25 points) a) (7 points) Suppose that you are asked to devise the fastest algorithm to find the second largest integer value in a MAX heap of n integers with duplicates. Assume that: there are at least two distinct values. you have direct access to the array that holds the heap items. you cannot modify the heap. (i) (2 points) Where could the second largest item be in the heap? The second largest value could be anywhere in the heap except at the top (first element) (ii) (3 points) Describe the algorithm. Compare every item against the top/max of the heap. (iii) (2 points) What is the worst-case big O running time? Give a one sentence description in addition to big O. [1 point] O(n) [1 point] In the worst case we will need to compare every item against the top of the heap, and the last item we compare is the second largest. b) (8 points) Briefly explain how to use a priority queue to implement a stack. What kind of priority queue is needed? How are the push and pop operations implemented? [2 point] MAX PQ [2 point] Associate each stack item with a key (number or timestamp). Start the key at a value. The last item inserted into the PQ have the largest value and therefore will be the first to be removed. [2 point] Push: insert item into the PQ and then increment the key. [2 point] Pop: remove the largest value from the PQ c) (10 points) Implement the fastest algorithm to find the minimum key in a MAX PQ. Hint: the minimum key is one of the leaves. // No points if algorithm takes longer than O(n) // The algo is supposed to investigate the leaves ONLY: indices n/2+1 to n
CS112 Data Structures - Final - Spring 2023 Name: _____________________________________ NetID: __________________________ 4
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