C949- Data Structures and Algorithms I
Items were added sequentially in this stack starting with "dog": "bird" "rabbit" "cat" "dog" What is the return value of the pop operation?
"bird"
Four words were added to an initially empty linked list in the following order: orange, carrot, banana, and apple. Which word is at the beginning of the list?
"orange"
Which value is appropriate for the variable middle given the pseudocode? function mystery() { string last; string first; char middle; int phone; float rate; }
'D'
Which value is appropriate for Test1 given the expression? char Test1;
'L'
Items were added sequentially in this stack starting with 'ham': 'sausage' 'toast' 'eggs' 'ham' What is the correct order of contents after the push operation is performed with the value 'bacon'?
'bacon' 'sausage' 'toast' 'eggs' 'ham'
Items were added sequentially onto the stack starting with 'red': 'green' 'yellow' 'blue' 'red' What is the stack after a pop operation?
'yellow' 'blue' 'red'
A stack s, a queue q, and a max value priority queue p each have a single 3 in them. Next s.push(4), q.push(4), and p.push(4) are executed. What is the triple (s.pop(), q.pop(), p.pop())?
(4,3,4)
What is displayed when n = 2 in this pseudocode? for(int i = 2; i <= n; i++){ for(j = 0; j <= n;){ display j; j = j + n/2; the division is integer division, decimal part neglected } }
0, 1, 2
What are the official indexes for the list list01 given this declaration? int[ ] list01 = {0, 2, 4, 6, 8, 10};
0, 1, 2, 3, 4, 5
Which sequence of letters represents preorder traversal of the nodes of this tree? A / \ B C / \ / \ D E \ / \ F G H / I
A B C D F E G I H
list comprehension
A Python construct for creating a list in terms of conditions on other lists new_list = [expression for name in iterable]
Which type of sorting algorithm is demonstrated in this pseudocode? for i from 0 to N - 1 if a[i] > a[i + 1] swap( a[i], a[i + 1]) end
Bubble
Which type of sorting algorithm is demonstrated in this pseudocode? def shortSort(alist): exchanges = True passnum = len(alist)-1 while passnum > 0 and exchanges: exchanges = False for i in range(passnum): if alist[i]>alist[i+1]: exchanges = True temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp passnum = passnum-1
Bubble
A large set of floating point numbers that are in range from 0.0 to 1.0 and are uniformly distributed across the range need to be sorted. Which sort procedure is useful when the input is uniformly distributed over the range?
Bucket
When should a dictionary be used instead of a list?
When the program uses key-value pairs as its data
What is a characteristic of keys in an associative dictionary data type?
They are unique and immutable.
What is a hierarchical data structure?
Tree
any(list)
True if any element in the list is True.
all(list)
True if every element in list is True (!= 0), or if the list is empty.
Which category of data does ("FB", 75.00, 75.03, 74.90) represent in the pseudocode? import datetime def middle(stock, date): symbol, current, high, low = stock return (((high + low) / 2), date) mid_value, date = middle(("FB", 75.00, 75.03, 74.90), datetime.date(2014, 10, 31))
Tuple
This stack reads left to right with the top to the right: 'green' 'yellow' 'blue' 'red' What could be the stack after a push operation?
['red','blue','yellow', 'green', 'purple"]
Given this data dictionary in Python: dict = {'white':0x0000, 'black':0x1111} Which command/function generates the output ['white','black']?
dict.keys()
binary tree
each node has up to two children, known as a left child and a right child.
Which command helps to speed up comparisons using dictionary keys during a dictionary (d) lookup in this pseudocode clip? h = hash(key) for pair in d: if h == pair[0]: return pair[1]
hash(object)
Given a set of numeric data and two declared variables: small and max, what is the logical first step in an algorithm that finds the smallest number?
Checking that the list contains at least one number
Which command will return true if x is in a list, otherwise return false?
Contains(Object x)
Which method can be used to take a value out of a dictionary?
D1[key].remove(value)
Which data structure allows inserting and deleting data elements at both the front and the rear?
Deques
Which data type does the mystery function return? return_type mystery (int R) { int NumUnits = R;return NumUnits * 3.14; }
Double
What is an attribute of a binary tree?
Each node has at most two children.
my_list[:]
Get a copy of the list.
my_list[:end]
Get a list from beginning of list to end (minus 1). Code: my_list = [5, 10, 20, 40, 80]print(my_list[:4]) Output: [5, 10, 20, 40]
my_list[start:end]
Get a list from start to end (minus 1). Code:my_list = [5, 10, 20]print(my_list[0:2]) Output: [5, 10]
my_list[start:]
Get a list from start to end of the list. Code: my_list = [5, 10, 20, 40, 80]print(my_list[2:]) Output: [20, 40, 80]
my_list[start:end:stride]
Get a list of every stride element from start to end (minus 1). Code: my_list = [5, 10, 20, 40, 80]print(my_list[0:5:3]) Output: [5, 40]
max(list)
Get the maximum element in the list.
min(list)
Get the minimum element in the list.
sum(list)
Get the sum of all elements in the list.
Which data structure allows elements to be inserted and deleted from one end and provides no direct access to the other end?
Stack
Which data structure allows insertion and removal from only one end of the data structure?
Stack
Which data structure uses a last in, first out (LIFO) removal of items?
Stack
What is true about a data structure implemented using linked allocation?
Storage is allocated using pointers to new locations as needed.
Which data type is appropriate for this array to store the given data? a = ["AF", "71", "BC", "157", "BA", "253"]
String
Which data type should be used for this object? days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
String
Which data type should be used for this variable? phoneNum = "212-555-1212"
String
What is true about garbage collection?
It reclaims memory from data structures implemented using linked allocations.
Which Java method is used to read bytes from a standard file?
Java.io.FileInputStream
Which abstract data type (ADT) has elements of the same type so that the elements can be retrieved based on the index or position?
List
What does the method any(b) return in Python if b is a dictionary?
Returns True if any key of the dictionary is true.
At the end of obj, what is the time complexity of inserting in this pseudocode? void DynamicArrayAppend(DynamicArray *obj, const void *input) { if (obj->logicalSize == obj->capacity - 1) { obj->capacity *= 2; obj->internalArray = realloc(obj->internalArray, obj->capacity * obj->itemSize); } obj->logicalSize += 1; DynamicArraySet(obj, obj->logicalSize - 1, input); } DynamicArray *obj = DynamicArrayCreate(sizeof(int));for (int i = 0; i < n; i++) { int number = rand() % 10; DynamicArrayAppend(obj, &number); }
O(1) or O(n)
What is the time complexity of the instructions in this pseudocode? for (i = 0; i < N; i++){ for (j = i+1; j < N; j++) { ... // sequence of statements that do not alter N } }
O(N^2)
What is the time complexity of this pseudocode? Algorithm Algo1(A) Input: An array A storing n ≥ 1 integers Output: The sum of the elements in A s=A[1] for i=1 to n do s=s+A[i] return s
O(n)
What is the time complexity of this pseudocode? double sumCol(double table[][], int numRows, int numCols, int col) { double cSum = 0; for (int row = 0; row < numRows; row++) { cSum += table[row][col]; } return cSum; }
O(n)
What is the typical run time for an insertion sort?
O(n^2)
What is the time complexity of this pseudocode? Algorithm Algo3(A, B) Input: Arrays A and B, each of them storing n≥1 integers Output: Count array B[i], where B[i] equals the sum of A[1] to A[i], i=1 to n c=0 for i=1 to n do for j=1 to n do s=A[1] for k=1 to j do s=s+A[k] if B[i]=s then c=c+1 return c
O(n^3)
What is displayed in step 3 if midterm = 60 and final = 65 in this pseudocode? Step 1: Declare midterm, final as integer Step 2: average = (midterm+final)/2 Step 3: if (average < 50) then Print "Fail" Else Print "Pass" endif
Pass
Which command will retrieve an item from the top of the stack?
Pop()
What is the logical last step in an algorithm that averages the high temperatures for 10 days and displays the average high temperature?
Printing the temperature
How many buckets are needed when sorting 13 numbers that have 15 digits each, using the radix-sort algorithm?
10
How many times in this pseudocode is the function F called? Main Declare K as Integer K = 3 Set Result = F(K) Write Result End Program Function F(N) as Integer If N == 1 Then Set F = 1 Else Set F = N * F(N - 1) Set N = N - 1 End If End Function
3
How many times will count++ execute when i = 3, in this pseudocode? int count = 0; int N = 4; for (int i = 0; i < N; i++) for (int j = 0; j < i; j++) count++;
3
Given: heapList = [22, 33, 44, 55, 66] Which index is the right child of item 22?
44
What is the output of the pseudocode below if the variables declared in the main program are global? Main Declare X as Integer, Y as Integer Set X = 1 Set Y = 2 Call Sub(X, Y) Write X Write Y End Program Subprogram Sub(Integer Num1, Integer Num2 as Reference) Declare X as Integer Set Num1 = 3 Set Num2 = 4 Set X = 5 Write X End Subprogram
5 14
The reference of the head of the doubly linked list is passed to the reverse() method: 1<-->2<-->3<-->4<-->5<-->6 What is the modified linked list when complete?
6<-->5<-->4<-->3<-->2<-->1
What is displayed in Step 5 if A = 15 and B = 5 in the pseudocode below? Step 1: Start Step 2: Read A, B Step 3: C= A*B Step 4: D=A/B Step5: Print C Step 6: Stop
75
What are the array elements corresponding to the mid-values in the first and second iterations of a binary search in an array arr = {45, 77, 89, 90, 94, 99, 100} and key = 100?
90 and 99
What are the mid-values in the first and second levels of recursion in this binary search? int arr = {46, 76, 89, 90, 94, 99, 100} and key = 99
90 and 99
Which data set is represented using the dictionary data type?
A set of students and their test scores
Which command will insert object x at position index in a list?
Add(int index, Object x)
What is the most efficient data type to use for this data set of a fixed size in Java? a = [0, 0, 1, 4, 7, 16, 31, 64, 127]
Array
Which data structure is indexed?
Array
Which data structure may only store homogeneous data elements?
Arrays
Which type of operation is represented in the pseudocode? int x,y,z; x=y=z=100;
Assignment
Which type of sorting algorithm is demonstrated in this code? int partition( void *a, int low, int high ) { int left, right; void *pivot_item; pivot_item = a[low]; pivot = left = low; right = high; while ( left < right ) { /* Move left while item < pivot */ while( a[left] <= pivot_item ) left++; /* Move right while item > pivot */ while( a[right] > pivot_item ) right--; if ( left < right ) SWAP(a,left,right); } /* right is final position for the pivot */ a[low] = a[right]; a[right] = pivot_item; return right; }
Quick
What is a characteristic of quick sort?
Recursively breaks down a problem into two or more subproblems of the same or related type
What is an attribute of a bubble sort algorithm?
Ideal for small number of n
An array soc of size 1009 is used where the index is an integer in [0,1008] and the hash-function key%1009. Where will the data associated with the key given by the last 4 social security digits '2023' be stored?
In soc[5]
What is the logical first step in an algorithm that extracts all the positive values from a given list of numbers?
Initialize the result to an empty list
Which data type is appropriate for the given data set? a = [1, 717, 23, 12, 314, 6]
Int
Which statement describes a queue data structure?
It is a sequence of elements in which insertions can take place only at the back end and deletions can take place only at the front end.
What is the effect on the object Computing regarding garbage collection? Computing obj = new Computing(); obj = null;
It is automatically available for garbage collection.
singly-linked list
a data structure for implementing a list ADT, where each node has data and a pointer to the next node
doubly-linked list
a data structure for implementing a list ADT, where each node has data, a pointer to the next node, and a pointer to the previous node. The list structure typically points to the first node and the last node
hash table
a data structure that stores unordered items by mapping (or hashing) each item to a location in an array (or vector)
dict method
a function provided by the dict type that operates on a specific dict object.
list nesting
a list inside another list Ex: my_list = [[5, 13], [50, 75, 100]]
Priority queue
a queue where each item has a priority, and items with higher priority are closer to the front of the queue than items with lower priority. Ex: Heap
Set
an ADT for a collection of distinct items. Ex: Binary search tree, hash table
List
an ADT for holding ordered data. Ex: Array, linked list
Bag
an ADT for storing items in which the order does not matter and duplicate items are allowed. Ex: Array, Linked List
Queue
an ADT in which items are inserted at the end of the queue and removed from the front of the queue. Ex: Linked List
Stack
an ADT in which items are only inserted on or removed from the top of a stack. Ex: Linked List
Deque
an ADT in which items can be inserted and removed at both the front and back. Ex: Linked list
Dictionary (Map)
an ADT that associates (or maps) keys with values. Ex: Hash table, binary search tree
dictionary
another type of container object that is different from sequences like strings, tuples, and lists. References to objects as key-value pairs - each key in the dictionary is associated with a value
algorithm
sequence of steps to solve a computational problem or perform a calculation
Which Big-O notation represents the time complexity of a bubble sort?
O(n^2)
