Data Structures Practice Exam
A hash table has which two major components?
"bucket" array and hash function
Problem: Inheritance Given there exists a C++ class named Person (ie class Person {...}; is declared in file: Person.h) Write the code to declare a class named Student that is derived from class Person. Include the appropriate header file. Use the public specifier. You do NOT need to have a "using namespace" line.
#include "Person.h" class Student: public Person { }
How many leaves does a tree with exactly 1 node have?
1
Problem: Quick Sorting Part C Select five different integers between 0 and 20 to demonstrate an input sequence that could cause a quicksort algorithm to run in O(n^2) time, assuming the quicksort algorithm always pivots on the leftmost entry of the sequence.
1, 2, 3, 4, 5
Problem: Merge Sort State the 3 main steps (in order) of Merge Sort and a brief description of what each does, as described in class. Recall: Algorithm mergeSort(S, C) Input sequence S, comparator C (assume C is the less than operator if it helps in the discussion) Output is a sorted Sequence (same elements as S but sorted)
1. divide: divides the sequence into two parts, roughly equal in size, S1 and S2 2. recurse: calls mergeSort on the 2 parts, or rather recursively sorts S1 and S2 3. conquer: merges the results of the 2 calls into 1 sorted result to return
Problem: Stacks Describe the output and final structure of the stack after the following operations: Assume Pop() prints out and removes the top element. Push(66) Push(55) Push(123) Pop() Pop() Push(432) Push(778) Pop() The screen output would be: _____________________ The stack would be: ______________________
123, 55, 778 432 66
What is the maximum number of nodes that a complete (and balanced) binary tree of height 5 can have?
2^(5+1) - 1 = 63
Assume a heap has been implemented using std::vector. So the root is at position 0. Given a node with index i, and assuming it has two children, with respect to index i; the node's left child is at what index?
2i + 1
Problem: Queue Data Structure Assume a queue of size 5 with values inserted in order from 1 to 5.
5 | 4 | 3 | 2 | 1 where 5 is the back and 1 is the front
Problem: Load Factor and Related Suppose that the size of the hash table is M = 1000 and the table has n = 750 item. What is the load factor? What's the equation?
750/1000 or 3/4 # of items / table size
A queue is a ___________ data structure
FIFO
Problem: Quick Sorting Part B Continue from part A. Having selected your pivot, write the "left" sub-array and "right" sub-array (and put the pivot in the middle.
Left: 4, 12 Middle: 22 Right: 36, 42, 31, 24
Problem: Runtime for Data Structures Data Structure: Queue Access element at location i: _______________
N/A
Problem: Runtime for Data Structures Data Structure: Queue Search for element:________________
N/a or O(n)
Problem: Priority Queues Part B: Assume you have implemented a linked LIST-based PQ. If the list is to remain UNSORTED, then what is the runtime of insertItem()?
O(1)
Problem: Runtime for Data Structures Data Structure: Linked List Insert: ____________
O(1)
Problem: Runtime for Data Structures Data Structure: Linked List Remove: ____________
O(1)
Problem: Runtime for Data Structures Data Structure: Ordered Array or Vector Access element at location i: _______________
O(1)
Problem: Runtime for Data Structures Data Structure: Queue Remove: ____________
O(1) pop_front
Problem: Runtime for Data Structures Data Structure: Queue Insert: ____________
O(1) push_back
Problem: Runtime for Data Structures Data Structure: Ordered Array or Vector Search for element:________________
O(lg n)
Problem: Priority Queues Part B: Assume you have implemented a linked LIST-based PQ. If the list is to remain as SORTED, then what is the runtime of insertItem()?
O(n)
Problem: Runtime for Data Structures Data Structure: Linked List Access element at location i: _______________
O(n)
Problem: Runtime for Data Structures Data Structure: Linked List Search for element:________________
O(n)
Problem: Runtime for Data Structures Data Structure: Ordered Array or Vector Insert: ____________
O(n)
Problem: Runtime for Data Structures Data Structure: Ordered Array or Vector Remove: ____________
O(n)
The worst case behavior of quicksort is __________
O(n^2)
What is the Big Oh best associated with the following code? void donald(int n, int x, int y) { for (int i = 0; i < n; ++i) { if (x < y) { for (int j = 0; j < n + n; ++j) cout << "j= " << j << endl; } else cout << "i= " << i << endl; } }
O(n^2)
You have analyzed a function and determined its runtime is characterized by E^n, subscript i=1, i what is the Big Oh for the analyzed code?
O(n^2)
Problem: Application A gaming company has hired you to work on their newest game. But they are not sure if they want you as a play tester or a programmer. To determine your skill level they have asked you the following question. The game is event based, with each event given a time to occur. What (basic) data structure would you suggest using to process the events? In one sentence, why?
Priority Queue
Problem: Priority Queues Part A: Sorting Given a sequence of N items a PQ can be used to sort the sequence. What is step 1?
Put n items in a PQ based on (key, data) using insertItem
Problem: Application A gaming company has hired you to work on their newest game. But they are not sure if they want you as a play tester or a programmer. To determine your skill level they have asked you the following question. The game needs a data structure to hold and process user input. What (basic) data structure would you suggest using to hold and process user input? In one sentence, why?
Queue
Problem: Priority Queues Part A: Sorting Given a sequence of N items a PQ can be used to sort the sequence. What is step 2?
Remove items n times using removeItem
Problem: Priority Queues Part C: Your employer has mandated you implemented a PQ using a linked list. The PQ is expected to be created in a non-time critical manner (e.g. registered passengers) but it must function quickly when it comes to removing items in order of priority (e.g. boarding passengers). Would it be better to implement the PQ using a SORTED or UNSORTED list? Briefly explain why.
Sorted because we want to be able to remove things quickly. Unsorted remove: O(n) Sorted remove: O(1)
Consider the implementation of the Queue ADT using a circular array. What goes wrong if we keep all the items at the front of a partially filled array (i.e. data[0] is always the front)
The insert method would require O(n) time
Problem: Hashing - Linear Probing The size of the table is 5 The hash function is h(x) = x % 5 Use an Open Address Hashing Method of Linear Probing Step 1: Insert element 10 [0] [1] [2] [3] [4]
[0] 10 [1] [2] [3] [4]
Problem: Hashing - Linear Probing The size of the table is 5 The hash function is h(x) = x % 5 Use an Open Address Hashing Method of Linear Probing Step 2: Insert element 14 [0] 10 [1] [2] [3] [4]
[0] 10 [1] [2] [3] [4] 14
Problem: Hashing - Linear Probing The size of the table is 5 The hash function is h(x) = x % 5 Use an Open Address Hashing Method of Linear Probing Step 3: Insert element 9 [0] 10 [1] [2] [3] [4] 14
[0] 10 [1] 9 [2] [3] [4] 14
Problem: Hashing - Linear Probing The size of the table is 5 The hash function is h(x) = x % 5 Use an Open Address Hashing Method of Linear Probing Step 4: Insert element 6 [0] 10 [1] 9 [2] [3] [4] 14
[0] 10 [1] 9 [2] 6 [3] [4] 14
Problem: Open Address Hashing: Chaining Given a hash function of h(x) = x % 4, and a table size of 5 insert the following sequence of numbers into the table using a CHAINING method to handle collisions: 20, 9, 14, 4 (process numbers left to right: 20 is first, 4 is last)
[0] 20 --> 4 [1] 9 [2] 14 [3]
Problem: Quick Sorting Part A Given the input sequence: 36, 42, 31, 4, 12, 24, 22 Apply the "median of three" rule to select the pivot in preparation for a QuickSort. You must show the 3 values selected and identify the median (the pivot value)
[0] = 36 [1] = 42 [2] = 31 [3] = 4 [4] = 12 [5] = 24 [6] = 22 data[0] = 36 data[n/2] = data[7/2] = data[3] = 4 data[n-1] = data[7-1] = data[6] = 22 22 is the pivot
Problem: Hashing Non-Numeric Given the following string to number encoding function and hash table of size 5, (use k % 5), draw the result after hashing the following values into the table. Use the CHAINING approach to resolve any collisions. k = string to number encoding = (# of characters in the key) * 3 a. deer b. bee c. snake d. razzmatazz e. dog
[0] snake -- razzmatazz [1] [2] deer [3] [4] bee -- dog
Because all elements of a stack are of the same type, you can use a(n) _____________ to implement a stack
array
Sequential and binary search algorithms are called _________ search algorithms
comparison-based
A depth first search is similar to traversing a binary tree level by level
false
A sequential search assumes that the data is in a particular order
false
For any two functions f and g, we always have either f = O(g) or g = O(f)
false
In a linked list implementation of a stack, only a fixed number of elements can be pushed onto the stack
false
In the analysis of algorithms, the key comparisons refer to comparing the key of the search item with the position of an item in the list
false
Operations such as search, insert, and delete require a linked list to be sorted
false
The elements at the top of the stack have been in the stack the longest
false
The general case in a recursive function is the case for which the solution is obtained directly
false
The height of a heap is log(n^2)
false
Problem: Queue Data Structure Assume a queue of size 5 with values inserted in order from 1 to 5. 5 | 4 | 3 | 2 | 1 Where do you add data to?
from the back
Problem: Queue Data Structure Assume a queue of size 5 with values inserted in order from 1 to 5. 5 | 4 | 3 | 2 | 1 Where do you remove data from?
from the front
Problem: Implementing a Queue Using a Linked List Queue Operation: Enqueue: Inserts an element to the back of the queue. What is the Singly and/or Doubly Linked List Operation for this?
insertBack: inserts an element on the back of the list
The unique member of an item is called the ____________ of the item
key
The sequential search is also called a _______________ search
linear
To speed up item insertion and deletion in a data set, use ___________
linked lists
In quicksort, all the sorting work is done in _____________ the list
partitioning
In a ___________ queue, customers or jobs with higher priorities are pushed to the front of the queue
priority
We use ___________________ to implement mergesort
recursion
Problem: Implementing a Queue Using a Linked List Queue Operation: Front: returns a copy of the first element of the queue, but does not remove it What is the Singly and/or Doubly Linked List Operation for this?
removeFront then insertFront must be altered a little bit for this to work
Problem: Implementing a Queue Using a Linked List Queue Operation: Dequeue: removes and returns the front of the queue What is the Singly and/or Doubly Linked List Operation for this?
removeFront: returns and removes the element at the front of the list
In heapsort, after we convert the array into a heap, the ______________ phase begins
sorting
A data structure in which the elements are added and removed from one end only is known as a ____________
stack
A data structure in which the elements are added and removed from one end only is known as a__________
stack
When a system is modeled on the Last In First Out principle, ____________ are used
stacks
Problem: Templates Write a templated function named LessThanOrEqual. Which will take two parameters: a and b (of the templated type) and return true if a <= b, else return false
template <typename T> T LessThanOrEqual (T a, T b) { return (a <= b ? a : b); }
Searching through a linked list or inserting an item in a linked list requires a ____________ of the list
traversal
A C++ class can have more than one constructor
true
A doubly linked list is a linked list in which every node has a next pointer and a previous pointer
true
A linked list in which the last node points to the first node is called a circular linked list
true
Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters
true
In a preorder traversal of a binary tree, for each node, first the node is visited, then the left tree subtree is visited, then the right subtree is visited
true
In an array, item insertion (especially if the array is sorted) and item deletion can be very time consuming, especially if the list size is very large
true
The analysis of algorithms enables programmers to decide which algorithm to use for a specific application
true
The use of a queue structure ensures that the items are processed in the order they are received
true