cop3530 midterm

Ace your homework & exams now with Quizwiz!

Compare and contrast singly linked lists and doubly linked lists in terms of operations and memory overhead.

In a singly linked list -each node knows when the next node comes -lower memory overhead In a doubly linked list -knows when prev and next node -traversal in both direction -deletion at head

Part 2 (5 points) Consider a hash table storing integer keys that handles collision using double hashing. If N = 15 h(k) = k mod 15 d(k) = 11 -k mod 11 Insert the following keys in order into the hash table below 32, 64, 18, 33, 19 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Part 2 (3 points) Assuming your hash table is implemented as an array, write C++ code to implement the double hashing approach above. Assume you only have to implement the insert method and you have an array called table with each location initialized to -1. This method should take as input the int key. In the method you will need to do the following: compute the hash code detect a collision compute d(k) place the data correctly Make sure your code treats the array as a circular array, and wraps around.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 321864. 19. 33 Part 2: void insert(int key) { int index = key % 15; if (table[index] == -1) { table[index] = key; } else { int step = 11 - (key % 11); int newIndex = (index + step) % 15; while (table[newIndex] != -1) { newIndex = (newIndex + step) % 15; } table[newIndex] = key; } }

What output is displayed after the following segment of code executes: stack <int> s; int a = 22, b = 44; s.push(2); s.push(a); s.push(a + b); b = s.top(); s.pop(); s.push(b); s.push(a - b); s.pop(); while (!s.empty()) { cout << s.top() << endl; s.pop(); }

66 22 2

What is the load factor of a hash table in C++? Why is it important?

A load factor of a hash table tells us how full the hash table is and its important because it helps with performance of the speed and the efficiency of space used.

Explain the concept of amortized analysis and how it is applied in algorithm analysis.

Amortized analysis is a technique used to find the average time complexity of an operation. It helps find the average cost overtime rather than looking at each individual thing, it helps understand how fast or slow the program is running.

Describe a scenario where using a queue would be more appropriate than using a stack. Ensure that you show how data is manipulated in each data structure when you justify your choice for the scenario you specify.

An example is having a shared printer and multiple users trying to print something at once using a queue. It will help ensure that every print job will get printed in the order they are received to prevent unfairness and delays.

Which of the following operations does not have a time complexity of O(1)? a. Using a collision resolution strategy b.Calculating a hash value c.Indexing into a hash table based on a hash value d.Inserting an arbitrary element into an unsorted linkedlist

a. Using a collision resolution strategy

Why do we consider the worst-case running time when analyzing the time complexity of an algorithm? (select all that apply) a.Considering the worst-case time complexity of an algorithm helps us ensure that the algorithm will always perform efficiently regardless of the input. b.The worst-case running time always reflects the actual running time of the algorithm. c.The worst-case scenario is usually the same as the average-case scenario. d.We don't, we always consider the average case running time. e.The worst-case scenario is easier to calculate than the average-case scenario. f.The worst-case scenario helps in understanding the upper bound on the running time.

a.Considering the worst-case time complexity of an algorithm helps us ensure that the algorithm will always perform efficiently regardless of the input. e.The worst-case scenario is easier to calculate than the average-case scenario. f.The worst-case scenario helps in understanding the upper bound on the running time.

What output will be produced by the code below? #include <iostream> #include <stack> int main() { std::stack<int> stack; stack.push(10); stack.push(20); stack.pop(); stack.push(30); stack.pop(); stack.push(40); stack.push(50); stack.pop(); std::cout << "Elements in the stack after all operations: "; while (!stack.empty()) { std::cout << stack.top() << " "; stack.pop(); } std::cout << std::endl; return 0; } a.Elements in the stack after all operations: 40 10 b.40 30 c.Elements in the stack after all operations: 20 10 d.Elements in the stack after all operations: 50 40 30 20 10

a.Elements in the stack after all operations: 40 10

Which of the following operations is NOT typically performed on a stack? a.Insertion in the middle b.Pop c.Push d.Deletion from the top

a.Insertion in the middle

A stack can be implemented using either arrays or a linked lists. a.True b.False

a.True

In a doubly linked list, each node contains pointers to both the next and previous nodes. a.True b.False

a.True

A program P reads in 500 integers in the range [0..100] representing the scores of 500 students. It then prints the frequency of each score above 50. What would be the best way for P to store the frequencies? a.A dynamically allocated array of 550 numbers b.An array of 50 numbers c.An array of 100 numbers d.An array of 500 numbers

b.An array of 50 numbers

Which data structure requires a contiguous block of memory? a.Linked List b.Array c.All of the above d.Queue

b.Array

What is the primary advantage of using a doubly linked list over a singly linked list? a.Lower memory consumption b.Efficient insertion at both ends c.Simpler implementation d.Faster traversal

b.Efficient insertion at both ends

A stack is an example of a non-linear data structure. a.True b.False

b.False

In the linked list implementation of a stack, in the push operation, if new nodes are inserted at the front of the linked list, then when popping, nodes must be removed from the end of the linked list. a.True b.False

b.False

Queues follow the Last In, First Out (LIFO) principle. a.True b.False

b.False

What is the time complexity of accessing an element in an array? a.O(n log n) b.O(1) c.O(log n) d.O(n)

b.O(1)

What does considering the amortized processing time of an algorithm help us understand? a.The time taken by the algorithm when the input size is at its maximum b.The average time taken by a sequence of operations performed by the algorithm c.The maximum time taken by the algorithm in any single operation d.The minimum time taken by the algorithm in any single operation

b.The average time taken by a sequence of operations performed by the algorithm

A hash tables can perform many operations like insert, delete, find and others very efficiently, this can only happen if the hash function and load factor have certain properties. What is the most important property? a.All of the above b.The keys are distributed uniformly c.It is fast d.Each key is mapped to a unique location

b.The keys are distributed uniformly

The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. #include <iostream> // Link list node struct Node { int data; struct Node* next; }; static void reverse(Node** head_ref) { Node* prev = nullptr; Node* current = *head_ref; Node* next; while (current != nullptr) { next = current->next; current->next = prev; prev = current; current = next; } //ADD A STATEMENT HERE } What should be added in place of "//ADD A STATEMENT HERE", so that the function correctly reverses a linked list. a.*head_ref = next; b.*head_ref = NULL; c.*head_ref = prev; d.*head_ref = current;

c.*head_ref = prev;

Which of the following is NOT a desirable property of a hash function h(k)? a.It should be computable in O(1) time. b.The range of h(k) should include a wide variety of integers. c.All of these are desirable properties. d.If x1, ... , xn are the items to be hashed, then the numbers h(x1), ... , h(xn) should be uniformly distributed over the integers.

c.All of these are desirable properties.

Assuming the code below has no errors and the necessary methods have been implemented, what will the output of the code be? #include <iostream> #include <queue> int main() { std::queue<int> queue; for (int i = 0; i<40; i++){ if(i%4 ==0){ queue.enqueue(i); } } std::cout << "Elements in the queue after loop completes are: "; while (!queue.empty()) { std::cout << queue.front() << " "; queue.dequeue(); } std::cout << std::endl; return 0; } a.Elements in the queue after loop completes are: 4 b.4 c.Elements in the queue after loop completes are: 0 4 8 12 16 20 24 28 32 36 d.Elements in the queue after loop completes: 0 1 2 3

c.Elements in the queue after loop completes are: 0 4 8 12 16 20 24 28 32 36

Which of the following data structures does NOT requires contiguous require contiguous memory allocation? a.Array b.Stack c.Linked List d.Queue

c.Linked List

Which if the following represent the highest runtime complexity? a.O(1) b.O(n) c.O(n2) d.O(log n)

c.O(n2)

Which data structure would be most appropriate to use for round-robin scheduling (in which the scheduler cycles repeatedly through all ready processes)? a.Array b.Linked List c.Queue d.Hash Table

c.Queue

Which data structure uses Last In, First Out (LIFO) ordering? a.Doubly linked list b.Singly linked list c.Stack d.Queue

c.Stack

Which of the following would be the best option to use to serve as the key for storing information in a key-value pair? a.birthday b.first name c.student ID d.favorite food

c.student ID

If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed? a.ABDC b.ABCD c.DCAB d.DCBA

d.DCBA

Which of the following operations cannot be performed efficiently on a singly linked list? a.Insertion at the Tail b.Deletion at the Head c.Insertion at the Head d.Deletion at the Tail

d.Deletion at the Tail

What operation does the following code snippet perform? void methodName(int value) { Node* newNode = new Node(value); newNode->prev = tail; newNode->next = nullptr; if(tail != nullptr) { tail->next = newNode; } tail = newNode; if(head == nullptr) { head = newNode; } } a.insert a node at the tail of a singly linked list b.delete a node at the head of a singly linked list c.insert a node at the head of a doubly linked list d.Insert a node at the tail of a doubly linked list

d.Insert a node at the tail of a doubly linked list

Which of the following is true about linked list implementation of stack? a.In push operations, if the new nodes are inserted at the tail, then when performing pop operations the nodes must be removed from the front of the linked list. b.Both of the above c.In push operations, if the new nodes are inserted at the front of the linked list, then the pop operations nodes must be removed from the tail. d.None of the above

d.None of the above

What is the time complexity of inserting an element at the front of a singly linked list? a.O(n log n) b.O(n) c.O(log n) d.O(1)

d.O(1)

Write a C++ method to find the sum of all elements in a queue q. Assume you have all the supporting code you need including code that implements enqueue, dequeue, empty, front NOTE: you do NOT need to preserve the information in the queue.

int sumOfQueueElements() { int sum = 0; while (!q.empty()) { sum += q.front(); q.pop(); } return sum; }

Match the hash table collision resolution strategies with their description. linear probing double hashing separate chaining place data in the next available space use an additional function to determine where to place data use an auxiliary linked list to store collided data

linear probing-place data in the next available space double hashing-use an additional function to determine where to place data separate chaining-use an auxiliary linked list to store collided data

Match the following data structures with their respective characteristics: stack queue array linked list Follows Last In, First Out (LIFO) ordering Supports First In, First Out (FIFO) ordering Supports constant-time access to elements. Can be implemented using dynamic memory allocation.

stack-Follows Last In, First Out (LIFO) ordering queue-Supports First In, First Out (FIFO) ordering array-Supports constant-time access to elements. linked list-Can be implemented using dynamic memory allocation.

Assuming all the supporting code that is necessary is available, what operation is the code below performing on a linked list? void function (int val, int newVal) { if (head == nullptr) { return; } Node* current = head; Node* prev = nullptr; while (current != nullptr && current->data != val) { prev = current; current = current->next; } if (current == nullptr) { return; } Node* newNode = new Node(newVal); if (prev == nullptr) { newNode->next = head; head = newNode; } else { prev->next = newNode; newNode->next = current; } }

​​this code is a function that takes 2 values it goes through the list to find the first value and then it creates a new node with the second value and add it right before it , it then makes sure the list is not empty and makes sure all the pointers are updated.


Related study sets

BA1 Unit 2 What's the capital of ...?

View Set

British Literature I: Introduction and Anglo-Saxon Exam Preparation

View Set

Chapter 15 Smartbook Review - Exam 5

View Set

Chapter 15 Maternity practice questions

View Set

Basic Communication Quiz Hand-In

View Set

Chapter 13 Thoat, Thorax, and Visceral Conditions

View Set