CS261 Data Structures - Midterm
What is the algorithmic complexity of the following code? struct node* current1 = lst1->firstLink->next; struct node* current2 = lst2->firstLink->next; int found = 0; while (current1 != lst1->lastLink) { while (current2 != lst2->lastLink) { if (current1->value == current2->value) found++; else current2 = current2->next; } current2= lst2->firstLink->next; current1 = current1->next; }
O(n^2)
What is the algorithmic complexity of a function that takes exactly 2n^4 + 4n^3 - 6n log n + 100 steps?
O(n^4)
What is the algorithmic complexity of the following code? for (int i = 0; i * i < n; i++) { // do something }
O(sqrt(n))
What type of collection would be appropriate for names of patients waiting for an operating room in a hospital emergency ward?
Priority queue
What type of collection would be appropriate for files being held until they can be printed on a printer?
Queue
Give an example input condition that cannot be specified using only a type.
Range restriction.
Regression testing
Re-executing the earlier test harness to ensure changes made during integration testing have not inadvertently introduced new errors.
What type of collection would be appropriate for the names of students enrolled in a class?
Set
What type of collection would be appropriate for URLs for recently visited web pages in a browser?
Stack
struct link members of a linked list implementation of a queue interface
TYPE value; struct link* next;
struct link members of a linked list implementation of a stack interface
TYPE value; struct link* next;
struct link members of a linked list implementation of a bag interface
TYPE value; struct link* next; struct link* prev;
struct link members of a linked list implementation of a deque interface
TYPE value; struct link* next; struct link* prev;
Black box testing
Testing that considers only the structure of the input and output values, and ignores the algorithm used to produce the result.
White box testing
Testing that considers the structure of the functions.
How is the memory representation of a linked list different from that of a Dynamic Array?
The Dynamic Array uses a large block of memory. This means that memory allocation is much less common, but when it occurs much more work must be performed. The linked list allocates a new link every time a new element is added. This makes memory allocation more frequent, but as the memory blocks are small less work is performed on each allocation.
What makes an ADT description abstract? How is it different from a function signature or an interface?
The description just provides a metaphor for how it behaves. An interface, on the other hand, lists out exactly the functions that a file may have.
What is an iterator? What purpose does the iterator address?
The iterator is a facilitator object, that provides access to the container values.
Abstraction
The process of identifying the most important or inherent qualities of an object or model and ignoring or omitting the unimportant aspects (information hiding).
Encapsulation
The provision of an interface to allow or simplify access for the user.
Unit testing
The testing of individual functions before you have a working application.
In what way does the precision of instructions needed to convey an algorithm to another human being differ from that needed to convey an algorithm to a computer?
To explain algorithms in pseudo-code, we need only to emphasize the important properties of the algorithm, and downplay incidental details that do nothing to assist in the understanding of the procedure.
What does it mean to perform a depth-first search? What data structure is used in performing a depth-first search?
To move deeply into the structure before examining alternatives. A depth-first search is the type of search a single individual might perform in walking through a maze.
The order that elements are placed into this ADT is completely unimportant.
bag
This ADT include add, contains, and remove behaviors.
bag
Resizing is never required when using this data structure.
linked list
This ADT is used to design searching a maze using breadth-first search.
queue
This ADT would be ideal to implement a collection of files being held until they can be printed on a printer.
queue
What is the algorithmic complexity of the following code? for (int i = 0; i * i < n; i++) { for (int j = 1; j * j < n * n; j++) { printf("i = , j = " i, j); } }
sqrt(n) * n O(n sqrt(n))
What is the algorithmic complexity of the following code? for (int i = 1; i * i < n; i += 2) { // do something } for (int j = 1; j < n; j *= 2) { // do something }
sqrt(n) + log(n) O(sqrt(n))
This ADT can simplifies the allocation of memory for program variables.
stack
struct linkedList members of a linked list implementation of a stack interface
struct link* firstLink;
struct linkedList members of a linked list implementation of a queue interface
struct link* firstLink; struct link* lastLink;
struct linkedList members of a linked list implementation of a bag interface
struct link* frontSentinel; struct link* backSentinel; int size;
struct linkedList members of a linked list implementation of a deque interface
struct link* frontSentinel; struct link* backSentinel; int size;
Primary tools used to manage and manipulate complex systems.
1. The ability to deal with abstract ideas. 2. Associated concept of information hiding.
What are some ways used to describe the outcome, or result, of executing an algorithm?
1. The result type 2. Documentation (comments)
What are two reasons for keeping the elements of a collection in sorted order?
1. To allow a fast search by means of the binary search algorithms. 2. To be able to merge two sorted collections quickly.
In considering the execution time of algorithms, what are two general types of questions one can ask?
1. Will the algorithm terminate for all legal input values? 2. Is there a more accurate characterization of the amount of time it will execute?
Big-oh complexity of a linked list implementation of a queue interface
1. addBack = O(1) 2. front = O(1) 3. removeFront = O(1) 4. isEmpty = O(1)
Big-oh complexity of a dynamic array implementation of a deque interface
1. addFront = Best: O(1), worst: O(n), average: O(1)+ 2. addBack = Best: O(1), worst: O(n), average: O(1)+ 3. front = O(1) 4. back = O(1) 5. removeFront = O(1) 6. removeBack = O(1) 7. isEmpty = O(1)
Big-oh complexity of a linked list implementation of a deque interface
1. addFront = O(1) 2. addBack = O(1) 3. front = O(1) 4. back = O(1) 5. removeFront = O(1) 6. removeBack = O(1) 7. isEmpty = O(1)
Classic operations of a stack
1. void Push(stack, newEntry) 2. void Pop(stack) 3. TYPE Top(stack) 4. int isEmpty(stack)
Classic operations of a bag
1. void add(bag, newEntry) 2. void remove(bag, entry) 3. int contains(bag, entry)
Classic operations of a queue
1. void addBack(queue, value) 2. TYPE front(queue) 3. void removeFront(queue) 4. int isEmpty(queue)
Classic operations of a deque
1. void addFront(deque, value) 2. void addBack(deque, value) 3. TYPE front(deque) 4. TYPE back(deque) 5. void removeFront(deque) 6. void removeBack(deque) 7. int isEmpty(deque)
Consider an empty but initialized stack using a dynamic array with initial capacity of 4. How many total units will it cost to call 10 consecutive push operations?
22 cost units
Interface
A collection of functions that are united in serving a common purpose. The interface file (traditionally ending in .h), contains only function prototypes.
Test suite
A collection of test cases.
Algorithm
A description of how a specific problem can be solved, written at a level of detail that can be followed by the reader.
Name a form of encapsulation.
A function.
What is a recursive algorithm? What are the two sections of a recursive algorithm?
A recursive algorithm calls a version of itself. 1. There must be a base case, which does not have a recursive call. 2. The recursive/inductive case, which does have this recursive call.
Test harness
A special purpose main method used in testing.
How is an assertion different from an assertion statement?
Assertions are written as comments, and are not executed by the program during execution. They need not be written in an executable form. The assertion statement, on the other hand, takes as argument an expression, and typically will halt execution and print an error message if the statement is not true.
Come up with another example from everyday life that illustrates the behavior of each of the six classic abstractions.
Bag: bag of marbles. Set: venn diagram. Stack: stack of plates. Queue: line into the movie theatre. Deque: peas in a straw. Priority queue: triage. Map: dictionary.
How is a set different from a bag?
Both are simply unordered lists. They are different in that a bag can contain the same thing, while a set must contain unique things.
In what ways does the analysis of recursive algorithms mirror the idea of mathematical induction?
Both work by establishing a base case, then reducing other inputs until they reach the base case.
Integration tesing
Combining calls on functions into complex programs.
What is the most powerful way to gain confidence in the correctness of a function or program?
Develop a proof.
Main characteristic of a deque
Double ended queue
Boundary testing
Exercising values that are just at the edge of the limits.
Main characteristic of a queue
FIFO - first-in, first-out
How is a priority queue different from a queue?
For a queue, relative time is the only factor. For a priority queue, there may be other factors.
What is an example of an algorithm in which termination would not be immediately obvious?
In Euclid's GCD algorithm, there is no definite limit on iterations, but either n or m becomes smaller on each iteration. That means m+n satisfies the three conditions!
What is the activation record stack? What values are stored on the activation record stack?
It is the spaced used by a running program to store parameters and local variables. Each time a function or method is invoked, space is set aside for these values. This space is termed an activation record.
Main characteristic of a stack
LIFO - last-in, first-out
What type of collection would be appropriate for names and associated Employee records in a company database?
Map
Big-oh characterization
Measures the behavior of algorithms as inputs become ever larger.
Big-oh complexity of a binary search
O(log n)
What is the algorithmic complexity of the following code? for (int i = 0; i < n; i *= 2) { // do something }
O(log(n))
What is the algorithmic complexity of the following code? for (int i = n; i > 0; i /=2) { // do something }
O(log(n))
What is the algorithmic complexity of the following code? int i = n; while (i > 1) { i = floor(i/2); }
O(log(n))
What is the algorithmic complexity of merge sort?
O(n log n)
What is the algorithmic complexity of a function that takes exactly n + n * log(n^2) + 2 steps?
O(n log(n))
Big-oh complexity of a linear search
O(n)
What is the algorithmic complexity of the following code? for (int i = 0; i < n; i++) { // do something }
O(n)
What is the algorithmic complexity of bubble sort?
O(n^2)
Characteristics of a linked list implementation of a queue interface
1. Doubly linked list 2. First link is sentinel
Characteristics of a linked list implementation of a bag interface
1. Doubly linked list 2. Front and back sentinel
Characteristics of a linked list implementation of a deque interface
1. Doubly linked list 2. Front and back sentinels
Big-oh complexity of a dynamic array implementation of a stack interface
1. Push = Best: O(1), worst: O(n), average: O(1)+ 2. Pop = O(1) 3. Top = O(1)
Big-oh complexity of a linked list implementation of a stack interface
1. Push = O(1) 2. Pop = O(1) 3. Top = O(1)
Give three examples of abstraction from real life.
1. Table of contents 2. Menu 3. Class schedule
In order to be successful, an algorithm must possess the following:
1. Input pre-conditions. 2. Precise specification of each instruction. 3. Correctness. 4. Termination, time to execute. 5. Description of the result or effect.
The three levels of abstraction in the study of data structures
1. Interface 2. Application 3. Implementation
When analyzing algorithms, what two questions are important?
1. Is the algorithm correct? 2. How fast does it run?
A basic tool used to prove that an algorithm terminates is to find a property or value that satisfies the following three characteristics:
1. It can be placed into a correspondence with integer values. 2. It is nonnegative. 3. It decreases steadily as the algorithm executes.
List the pros of a dynamic array.
1. Its the only core data structure designed to hold a collection of elements. 2. Random access. You can quickly get to any element. 3. Hides memory management.
List functions from most costly to least.
1. N! 2. 2^n 3. N^d, d > 3 4. N^3 5. N^2 6. N sqrt n 7. N log n 8. N 9. sqrt n 10. Log n 11. 1 (constant)
What are the limitations of big-oh characterization?
1. Not as useful when n is small. 2. It does consider memory usage.
Characteristics of a linked list implementation of a stack interface
1. Null terminated 2. Singly linked list 3. No sentinel
What are two different techniques used to specify the input conditions for an algorithm?
1. Argument types (will make sure that the arguments passed are of the correct type) 2. Run-time checking (e.g. range restriction)
Five applications of the stack
1. Back and forward buttons in a web browser 2. Buffered character input 3. Checking balanced parenthesis 4. Conversion of infix to postfix 5. Evaluation of a postfix expression
Big-oh complexity of a dynamic array implementation of a bag interface
1. Add = Best: O(1), worst: O(n), average: O(1)+ 2. Remove = Best: O(1), worst: O(n), average: O(1)+ 3. Contains = O(n)
Big-oh complexity of a linked list implementation of a bag interface
1. Add = O(1) 2. Remove = O(n) 3. Contains = O(n)
The elements of this ADT can only be added or removed from the end points.
deque
This ADT would be ideal for implementing a finite length undo in an application like Photoshop.
deque
One function __________ another if as the input gets larger the second will always be larger that the first, regardless of any constants involved.
dominates
If you have the actual timings ("wall clock time") for an algorithm with one input size, you can use the big-oh to estimate the execution time for a different input size using this algorithm:
f(n1)/f(n2) = t1/t2
This ADT provides the ability for a user to loop over elements and maintains encapsulation for the developer.
iterator