Data Structures Final Exam

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which of the following is a correct C++ implementation for push operation is Stack data structure? Note: the initial top value is -1.

bool Stack::push(int x){ if ( isFull() ){ return false; } else{ top++; stackArray[top] = x; return true; } }

The performance of priority queues depends on its ___________

implementation. • We need fast ways to insert an element to priority queues and delete the min/max element.

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 45. Which (possibly multiple) of the following sequences could be the sequence of nodes examined? 8, 7, 6, 5, 4, 3, 2, 1.

impossible

in inheritance, The derived class _______________________

inherits all features from the base class and can have additional features of its own.

main methods in priority queues

insertItem(k, o) : inserts an item with key k and object o • deleteMin() : removes the object with the smallest key • minKey() : returns, but does not remove, the smallest key of an item minElement() : returns, but does not remove, the element of an item with smallest key size() isEmpty()

main difference between virtual function and pure virtual function is

is that 'virtual function' has its definition in the base class and also the inheriting derived classes redefine it. The pure virtual function has no definition in the base class, and all the inheriting derived classes has to redefine it.

inheritance is an __________ relationship. ..... with examples

is-a A car is a vehicle. A surgeon is a doctor. A dog is an animal. A binary search tree is a tree A math teacher is a teacher

priority queue stores a collection of _____.

items

If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be _________________

private

When inheriting a CLASS, we can use what keywords

public, protected or private class Animal { // code }; class Dog : private Animal { // code }; class Cat : protected Animal { // code };

Inheritance encourages code ________________________________

reusability. You don't have to create everything from scratch, you can reuse features from an existing class (base class).

An embedded system has a limited memory that can fit a single array [0 .. maxSize]. Two stacks were needed to accomplish a particular task in this embedded system; a double stack data structure has been implemented. In this data structure, two stacks are placed in a single array. Every stack has its own space in this shared array. The two stacks grow from opposite ends of the array. The variables top1 and top2, ( top1 < top 2), point to the location of the topmost element in each of the stacks. How can we check if the two stacks are full? What is the condition that utilizes the shared array efficiently.

top1 = top2 -1

Priority Queue with sorted array/list

• insert: • deleteMin: • Sorted array/lists give us fast delete (O(1), it's just the first element!), but we have to either search the whole linked list or move the whole list (for array) on an insert (O(n) ).

Is this a good hash function: h( value ) = ( 2 * value ) % table_size , table size is 6

NO This is a bad function because it will never map values to odd locations in the table (1, 3, and 5), it is not uniformly distributed. Also the table size is small, this will increase the chance of having collisions when we have large set of data, pigeonhole principle. Using prime numbers will reduce the chance of having collisions. A good practice for selecting the table size is to select a prime number close to the table size, for example, 101 instead of 100.

where are values in a hash table stored?

NOT STORED IN ARBITRARY LOCATIONS stored in key index derived from the value itself The location (key) where a value is stored in the hash table depends on the value itself. i.e. the key is derived from the value.

Ways to implement priority queues

Naive - arrays - linked list Binary Search Trees

What is the complexity of the heapify function in binary heaps?

O( log(n) )

Priority Queue vs. Normal Queue

Priority Queue not all lines are equal, EX: Emergency Room elements in the queue are served based on their priority

can you use priority queues and heaps together?

Priority queues usually implemented using heap for better performance.

Protected access modifier

The access modifier protected is especially relevant when it comes to C++ inheritance. Like private members, protected members are inaccessible outside of the class. However, they can be accessed by derived classes and friend classes/functions. We need protected members if we want to hide the data of a class, but still want that data to be inherited by its derived classes.

What is the summation of the following infinite series summation: 1, 1/2, 1/4, 1/8, 1/16

The infinite series summation formula is: a/(1-r) where a=1,r=(1/2) = 1/(1-(1/2)) =2;

Can members in the same class access . . . public protected private

YES YES YES

linear probing

a way to solve a hash table collision search the next empty location in the array by looking into the next cell until we find an empty cell.

quadratic probing

a way to solve a hash table collision similar to linear probing; the difference is that if you were to try to insert into a space that is filled you would first check 1​2​​=1 element away, then 2​2​​=4 elements away, then 3​2​​=9 elements away then 4​2​​=16 elements away and so on.

Heap is an __________________________; we can draw it to look like a tree, but recognize that it is the array that implements this abstraction and that it is stored and processed in primary memory (RAM). • No 'holes' in the array.

abstraction

floyds method of building heap

buildHeap(){ for (i=size/2; i>0; i--) percolateDown(i); }

why is heapsort a good sorting method

considered to be one of the best sorting methods being in-place with no quadratic worst-case scenarios. Finding the min, max, both the min and max, median, or even the k-th largest element can be done in linear time using heaps.

what does hash table use to store values

hash function

Priority Queue with unsorted array/list

insert • deleteMin: • Unsorted array/list give us fast inserts( O(1) ), but we have to search the whole list to delete ( O(n) ).

do we need to store pointers for a parent child relationship in a heap

no

A derived class can access all the _____________ members of its base class

non-private Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

priority queues item is a _____________

pair (key, object)

virtual functions and pure virtual functions are both concepts of

run time polymorphism

virtual function vs. pure virtual code syntax

virtual: virtual type myFunction(...); pure virtual: virtual type myFunction(...) = 0 ;

double hashing

way to solve hash table collisions a second hash function is used to determine the location of the next spot. For example, given hash function H1 and H2 and key. do the following: Check location hash1(key). If it is empty, store the value in it. If it is not empty calculate hash2(key). check if ( hash1(key) + hash2(key) ) is open, if it is, store the value in it repeat with ( hash1(key) + 2*hash2(key) ), ( hash1(key) + 3*hash2(key) ) and so on, until an opening is found.

Characteristics of a heap

• A heap is 'complete' binary tree (the last row is filled left to right) • Usually implemented as an array • Each node in a heap satisfies the 'heap condition,' which states that every node's key is larger than or equal to the keys of its children.

decreaseKey priority queue function

• Given the position of an object in the queue, increase its priority (lower its key). Low key -> high priority

BST Priority Queue

• Regular BST: • insert: • deleteMin: • Insert and delete the minimum takes O(log n) - average case. • O(n) for worst case.

Applications of priority queues

• Standby flyers • Auctions • Routing algorithms

heap structure property

• complete tree with fringe nodes packed to the left • result: depth is always O(log n); next open location always known

increaseKey priority queue function

• given the position of an an object in the queue, decrease its priority (increase its key).

heap order property

• parent's key is less than children's keys • result: minimum is always at the top

linear probing

A simple re-hashing scheme in which the next slot in the table is checked on a collision.

hash function

Function which, when applied to the key, produces a integer which can be used as an address in a hash table.

The ___________ of a node is the number of edges from the node to the deepest leaf.

HEIGHT

• In priority queues, we are interested in finding the objects with the _________ priority.

HIGHEST • This could be similar to finding the minimum number an an array, a linked list, or a binary search tree. • à assign a priority key for each object, the lower the key the highest the priority.

advantage of hash table

Hash tables are good for storing and lookup, the complexity is O(1). Assuming a good hash function is selected.

disadvantage of hash table

Hash tables are not good for sorting data, it is recommended to use other data structures if sorting is needed.

what is inheritance

Inheritance is one of the key features of object oriented programming in C++. It allows programmers to create a new class (derived class) from an existing class (base class).

4 ways to solve collisions

Linear Probing quadratic probing chaining double hashing

advantages of counting sort

Linear time complexity. O(n+k) is the upper bound complexity for this algorithm; n is the input size and k is the input range.

3 access modifiers

public private protected

What is the best data structure to be used for implementing the following: building indexes for faster lookup

trees

can a heap be represented in arrays

yes

priority queue delete min function

• pqueue.deleteMin() • Start by taking out the min value in O(1) time. (min always at the root) • Now, we have a hole at the top, and a node that isn't proper for a complete tree at the bottom. • So, we fix it by putting the value in the hole, and moving the hole until we restore the heap-order property. (maintain a complete tree) -> heapify the heap: maintain the heap prosperities.

________________ is a sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Bubble sort

The ___________ of a node is the number of edges from the root to the node.

DEPTH

what is collision in hash table

It is possible to map two different values to the same location; i.e two values have the same key index. This is called collision.

What is the complexity of the following recurrence equation:

O( n*log(n) ) This recurrence can be solved using the master method case 2. Complexity is O(n*log(n))

• Binary search trees provide _______ complexity access to any element, assuming balanced trees.

O(log n)

Complexity of building a heap

O(n)

What is the complexity of in-order traversal algorithm?

O(n)

The overall complexity of deleting a key member function in the LinkedList class is: void LinkedList::deleteKey(const string delData);

O(n) Deleting a key requires searching before deleting; O(n) is the correct answer.

Which of the following is the correct destructor implementation for the following Queue class:

Queue::~Queue(){ delete [] qArray; }

Good hash function for a string

The good and widely used way to define the hash of a string s of length n is: hash(s) = ( s[0] + s[1]*p + s[2]*p2 + ... + s[n-1]*pn-1) mod m where p and m are some chosen, positive numbers. It is called a polynomial rolling hash function. It is reasonable to make p a prime number roughly equal to the number of characters in the input alphabet. For example, if the input is composed of only lowercase letters of the English alphabet, p=31 is a good choice. If the input may contain both uppercase and lowercase letters, then p=53 is a possible choice.

how to add entry to a heap

To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward.

how to remove item from a heap

To remove the biggest entry, move the last node onto the root, and perform a reheapification downward

What is the best data structure to be used for implementing the following: Data Compression

Trees

rules When using public inheritance with classes

When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

rules when using private inheritance with classes

When deriving from a private base class, public and protected members of the base class become private members of the derived class.

rules When using protected inheritance with classes

When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

Can members in the outside class access . . . public protected private

YES NO NO

what is a hash table

a data structure that maps keys to values, the keys are used to access (insert/find) the values

remove priority queue function

given the position of an an object in the queue, remove it.

when is counting sort an efficient algorithm

very efficient algorithm provided that the range of the input data is well-defined, finite and small.

quadratic probing

A re-hashing scheme in which a higher (usually 2nd) order function of the hash index is used to calculate the address.

disadvantages of counting sort

Requires prior knowledge of the data range It works only for discrete values like integers Inefficient if the input range is large

Binary search trees have ________________________ relationship, each node is related to all other nodes (a node is compared with other nodes to find it)

STRONG ORDER

hash table

Tables which can be searched for an item in O(1) time using a hash function to form an address from the key.

clustering

Tendency for clusters of adjacent slots to be filled when linear probing is used.

what makes a good hash function

Uniformly distribute the values in the table Be deterministic -- give the same result for the same value always Generate different hash values for similar data -- hash values for "John" and "Johny" will be far from each other in the table Use only the data being hashed Use all of the data being hashed (for example, when hashing phone numbers, it is better to use all 10 digits instead of 3 digits representing the area code)

base classes with virtual and pure virtual functions

VIRTUAL: Base classes containing virtual functions are NOT abstract classes. Objects can be instantiated from these classes. PURE VIRTUAL: Base classes containing virtual functions are abstract classes. Objects can NOT be instantiated from these classes.

derived classes virtual vs. pure virtual

VIRTUAL: Derived classes may or may not redefine virtual member functions inherited from the base class. PURE VIRTUAL: Derived classes must redefine pure virtual member functions inherited from the base class; otherwise, derived classes become abstract classes like the base class.

Virtual vs. pure virtual member functions

Virtual member functions are defined in the base class. Pure virtual member functions are not defined in the base class; only the function prototype is written (ending with =0).

Heaps are "____________ Ordered". . . . . WHY? and what are the properties of it?

WEAKLY • We know how a binary search tree is developed - with lesser keys to the left; greater keys to the right as we descend. • Because of this, we have nice, neat algorithms for binary search trees. • Here: No Strong Ordering • But for nodes in a heap, we don't have this strong ordering - and this can cause us some difficulty. • Cannot assert much: • We can only assert as one descends in the heap, nodes will be in descending order • Equivalently, nodes below a node are <= the parent node. • No Convenient Search Mechanism: • Because of weak ordering, there is no convenient searching for a specified key as we have in binary search trees. • Don't have enough info at a node to decide whether to descend left or right. • Delete: • So to delete a node with a specific key there are issues • No real slick way to find it. There is some 'randomness' in heap's organization • Sufficient Ordering: Yet, there is 'sufficient ordering' to allow • quick removal (yes, a delete) of the maximum node and • fast insertion of new nodes.

collision

When a hash function maps two different keys to the same table address, a collision is said to occur.

Is this a good hash function: h(value) = (13*value) % 101

YES Using prime numbers will reduce the chance of having collisions. A good practice for selecting the table size is to select a prime number close to the table size, for example, 101 instead of 100.

Can members in the derived class access . . . public protected private

YES YES NO

chaining

a way to solve hash table collisions use linked list to store values mapped to the same key.

Given an empty stack, what is the sequence of popped numbers out of the stack after performing the following operations: push (1), push (0), pop, push (1), push (0), pop, pop, pop, push (0), pop.

0 0 1 1 0

We learned in the class that a stack can be implemented using arrays or linked lists. Another (inefficient) way is to use queues. What is the minimum number of queues needed to implement a stack?

2 or 1

A list of eight numbers 10, 20, 30, 40, 50, 60, 70 and 80 are pushed into a stack in reverse order, i.e., starting from 80. The stack is popped four times and each popped number is enqueued into a queue.Two numbers are dequeued from the queue and pushed back into the stack. When the stack is popped once now, what is the popped number?

20

A list of eight numbers 10, 20, 30, 40, 50, 60, 70 and 80 are pushed into a stack in reverse order, i.e., starting from 80. The stack is popped four times and each popped number is enqueued into a queue.Two numbers are dequeued from the queue and pushed back into the stack. When the queue is dequeued once, what is the dequeued number?

30

TRUE/FALSE: A Binary Tree is a linear data structure made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root.

A binary tree is a non-linear data structure.

Which of the following is NOT correct about Queues? a. A queue has two ends; one for insertion and for removal. b. A queue is a first-in first-out structure c. Queues have limited access, not all elements in the queue are accessible in O(1). d. None, all statements are correct.

ALL ARE CORRECT

Which of the following is NOT correct about Queues? a. Queues have limited access, not all elements in the queue are accessible in O(1). b. A queue is a first-in first-out structure c. A queue has two ends; one for insertion and for removal. d. None, all statements are correct.

All are correct

TRUE/FALSE: All leaf nodes in a tree have the same depth

FALSE

TRUE/FALSE: The root node in a binary heap tree is stored at location hArray[1]; where "hArray" is the name of the array representing the binary heap.

FALSE

TRUE/FALSE:Trees can't be implemented using arrays.

FALSE

TRUE/FALSE: In order to use binary search algorithm in linked lists, the linked list must be ordered. This will make the search complexity O( log(n) ).

FALSE Binary search algorithm can't work on linked lists because linked list don't support random access. (i.e. we can't access the middle node in a linked list in O(1))

TRUE/FALSE: Given a large array of random real numbers, array size is n, it is possible to implement an efficient algorithm to find the minimum and maximum difference between

FALSE Finding the minimum difference requires sorting the array, while finding the maximum difference requires finding the max and min numbers. We need to use Merge sort or Quicksort to find the minimum difference, this will make the overall complexity O(n*log(n))

TRUE/FALSE: We need to update both head and tail pointers when inserting a new node at the beginning of a non-empty list.

FALSE Only the head pointer will be updated. If the list is empty, then both head and tail pointers will be updated.

TRUE/FALSE: Since the queue requires inserting at one end and removing from another end, linked lists can't be used to implement queues; ONLY arrays can be adapted to implement queues.

FALSE Queues can be implemented using both arrays or linked lists.

TRUE/FALSE: The running time complexity for inserting a node at the beginning of a linked list is O(1), but the running time complexity for inserting a node at the end of a linked list is O(n) because we need to walk through all nodes to reach the end of the list.

FALSE The complexity of inserting at the beginning of linked list is O(1) using the head pointer, and it is O(1) complexity to insert at the end of the list using the tail pointer.

TRUE/FALSE: Any adjacent indexes ,i and i+1 where i>1, in the heap array are sibling nodes in the binary heap tree.

False

TRUE/FALSE: We need to update both head and tail pointers when inserting a new node at the beginning of a non-empty list.

False

TRUE/FALSE: The best case scenario when using Merge sort is sorting an already sorted list, the complexity is Ω(n).

False The best case scenario for Merge sort algorithm is Ω(n*log(n)).

Which is the best sorting algorithm to sort a nearly sorted array?

Insertion Sort Insertion sort is the best for nearly sorted data, the upper bound complexity will be O(n). The Bubble sort have extra overhead due to comparisons and shifting. Merge sort and Quicksort runs in O(n*log(n)) in the best case.

Which of the following statements is NOT true about Merge sort algorithm? a. Merge sort requires additional memory, it is an out-of-place sorting algorithm. b. Merge sort always divides a problem into two equally sized subproblems. c. The complexity of dividing the problem into two subproblems is O(n). d. The complexity of combining two solved subproblems is O(n).

Merge sort is not an in-place sorting algorithm, it requires an additional memory. The correct answer is: The complexity of dividing the problem into two subproblems is O(n).

Which of the following statements creates a Node pointer (named newNode) to a new allocated node?

Node *newNode = new Node;

Which of the following asymptotic notations can be formally defined as: For a given function g(n), we can define a set of functions f(n), such that: ________ = { f(n): there exist positive constants c and no such that 0 ≤ f(n) ≤ c * g(n) for all n ≥ no}

O( g(n) ) This definition finds the upper bound for a given function g(n) ; it is O( g(n) ).

Which of the following is correctly ordered from the fastest to slowest? O(3^n), O(log(n)) , O(n^2*log(n)) , O(n) , O(n^3) , O(n*log(n)) , O(log2(n)) , O(1), O(2^n), O(n^2) ,

O(1), O(log(n)) , O(log2(n)) , O(n) , O(n*log(n)) , O(n2) , O(n2*log(n)) , O(n3) , O(2n), O(3n)

What is the complexity of the following recurrence equation: . T(n) = 2 T(n-1) +1 -- you don't need to use substitution/master methods to solve it; think of a problem with similar recurrence.

O(2^n) This recurrence describes towers of Hanoi problem, it is an exponential complexity.

What is the tightest time complexity to find a number in the following data structures? Balanced Binary Search Trees

O(logN)

What is the tightest time complexity to find a number in the following data structures? sorted array

O(logn)

What is the tightest time complexity to find a number in the following data structures? binary trees

O(n)

What is the tightest time complexity to find a number in the following data structures? nonsorted array

O(n)

What is the tightest time complexity to find a number in the following data structures? sorted linked list

O(n)

What is the tightest upper bound complexity of exam1 function: void exam1( Array A, int n) { for(int i = 1 ; i <= n ; i*=2){ for( int j = n ; j > n-i ; j--){ cout<< A[j] << endl; } } }

O(n)

Which of the following tree traversal algorithms the root node will be the last node to be traversed? a. Post-order b. Level-order c. Pre-order d. In-order

Post Order In post-order algorithm the parent node is traversed after its children (left / right / parent). So the root node in post-order algorithms will be the last to be traversed.

Given the following values: capacity = 10 front = 7 rear = 6 Which of the following is TRUE about this circular Queue? a. The queue is empty b. The queue is full c. The queue has 7 elements d. The queue has 1 element e. The queue has 6 elements

QUEUE IS FULL A full circular queue has only ONE empty space to distinguish it from an empty queue. A circular queue is full when front = (rear+1) % capacity. In this example, (rear+1) % capacity = (6+1)%10 = 7 = front ==> full queue

What is the best data structure to be used for implementing the following: Implementing first-come first-serve CPU scheduling algorithm in OS

Queues

Which of the following is TRUE about the best case scenario for the Quicksort algorithm? a. Quicksort best case scenario complexity is O(n*log(n)); it happens when we have a totally random array. b. Quicksort best case scenario complexity is O(n); it happens when we have an already sorted array. c. Quicksort best case scenario complexity is O(n*log(n)); it happens when we have an already sorted array. d. Quicksort best case scenario complexity is O(n); it happens when we have a totally random array.

Quicksort best case scenario complexity is O(n*log(n)); it happens when we have a totally random array.

What is the best data structure to be used for implementing the following: Keeping track of recursive function calls

Stacks

Which of the following recurrence equations describes the behavior of binary search algorithm?

T(n) = T(n/2) + 1 The binary search algorithm divides the problem into two subproblems and works on one of the subproblems, it takes O(1) to divide the problem.

TRUE/FALSE: All leaf nodes in a tree have the same height.

TRUE

TRUE/FALSE: In object-oriented programing, constructors and destructors are called implicitly.

TRUE

TRUE/FALSE: The depth of a node is the number of links from that node to the root.

TRUE

TRUE/FALSE: Creating arrays requires reserving a consecutive space in the memory. All information regarding this space is stored in a structure called dope vector.

TRUE The dope vector stores information about the reserved consecutive space to support random access in O(1) complexity.

TRUE/FALSE: The worst-case running time for Binary Search algorithm is O(n); this happens when the input array is not sorted.

The correct answer is 'False'. The binary search algorithm works on sorted arrays only.

The complexity of finding the maximum number is a sorted array is:

The correct answer is: O(1) The complexity is O(1) because the maximum number will be always the last number is the list. We don't need to sort the array.

Which of the following is not an application of queues? a. Printing queue in a shared printer b. Undo operation in a text editor c. Search algorithm in graphs d. Keyboard buffer

Undo operation in a text editor The undo operation in a text editor requires a stack not a queue.

Which of the following member functions is a correct implementation for finding a key in a linked list? The correct function must return true if the key is in the list and false if the key is not in the list.

bool LinkedList::findKey(const string key){ if(head==nullptr){ return false; } Node* current; current = head; while(current != nullptr && current ->data !=key){ current = current -> next; } if(current == nullptr){return false;} else{return true;}

Enqueue operation inserts an element at the ________________

c. rear end of the queue In queues, insertion is done at the rear end of the queue. Insertion is called Enqueue.

A circular queue is full when:

front = (rear+1) % capacity

A circular queue is empty when:

front = rear

Dequeue operation removes an element from the _______________

front end of the queue In queues, removing is done from the front end of the queue. Removing is called Dequeue.

When using arrays to implement a binary heap, assume that "hArray" is the name of the array, how can we find the following: Number of nodes in the heap

hArraay[0]

When using arrays to implement a binary heap, assume that "hArray" is the name of the array, how can we find the following: The first available empty location in the array

hArraay[0] + 1

What C++ statements are written in LinkedList class constructor?

head = nullptr; tail = nullptr;

When using arrays to implement a binary heap, assume that "hArray" is the name of the array, how can we find the following: The parent of the node at index (i)

i/2

What is a correct C++ implementation for the dequeue operation in a circular queue?

int Queue::dequeue(){ if ( isEmpty() ){ throw "Queue is EMPTY ... \n"; } else{ int result = qArray[front]; front = (front+1) % capacity; return result; } }

In binary search trees the nodes are arranged in the following order: for each node, all elements in its left subtree are:

less-or-equal to the node (<=) A binary search tree (BST) or is a type of binary tree where the nodes are arranged in the following order: for each node, all elements in its left subtree are less-or-equal to the node (<=), and all the elements in its right subtree are greater than the node (>).

Mark the following data structures as linear or non-linear: arrays

linear

Mark the following data structures as linear or non-linear: linked lists

linear

Mark the following data structures as linear or non-linear: queues

linear

How many times the outer loop in exam1 function is executed? (assume n is a power of 2 number) void exam1( Array A, int n) { for(int i = 1 ; i <= n ; i*=2){ for( int j = n ; j > n-i ; j--){ cout<< A[j] << endl; } } }

log2(n)+2

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 45. Which (possibly multiple) of the following sequences could be the sequence of nodes examined? 1, 2, 3, 4, 5, 6, 7, 8.

possible

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 45. Which (possibly multiple) of the following sequences could be the sequence of nodes examined? 60, 20, 26, 27, 40, 44.

possible search sequence

how does counting sort work

works by counting the appearances of every number in the input list

Given the following values: capacity = 10 front = 9 rear = 0 Where a new element will be enqueued in this circular queue?

At queueArray[ 0 ] Enqueue operation inserts a new element at the rear of the queue, rear contains the index of first empty space in the queue. In this circular queue, rear = 0 means that the element will be enqueued in at queueArray[ 0 ].

The following pseudocode is an incorrect implementation for the parentheses matching algorithm. It works most of the times, but it fails sometimes. Which of the following input will be incorrectly marked as "matched" by this algorithm? Stack S while ( input is not empty ) { read a character from input if ( the character is a '(' ) push it on the top of the stack else if ( the character is a ')' and the stack is not empty ) pop a character off the stack else print " parentheses are not matching" exit } print "parentheses are matched"

( ( ) ) ( ( ) The algorithm does not check if the stack is empty after reading all input; if there is anything left in the stack the algorithm will skip it, causing to print wrong results.

What is the summation of the following numbers: 1, 2, 3, 4, ... , n-3, n-2, n-1

((n-1)(n))/2 we have (n-1)/2 pairs and each pair summation is n

Given the following values: capacity = 10 front = 9 rear = 0 How many elements are there in this circular queue?

1

When implementing Queue data structure using single linked lists, which one of the following choices will get us O(1) complexity for both enqueue and dequeue operations?

Enqueue using tail pointer, dequeue using head pointer When we dequeue using tail, we need to make tail points at the node before the last node. There is no pointer pointing at the node before the last, we need to start from the beginning of the list till we reach to the node before the last node. This will make dequeue operation run in O(n) complexity. The ideal implementation is to have O(1) complexity for both enqueue and dequeue operations.

Which of the following is TRUE about Binary Trees? a. Every node in a binary tree has exactly two children b. Every node in a binary tree has at least two children c. Every node in a binary tree has at most two children d. Every node in a binary tree has an arbitrary number of children

Every node in a binary tree has at most two children Every node in a binary tree has at most two children, nodes with no children are called leaf nodes.

TRUE/FALSE: Master method can be used to solve any recurrence equation.

FALSE The master method is used on recurrence equations on the form: T(n) = a*T(n/b) + f(n) , and f(n) is a positive increasing function.

TRUE/FALSE: Given the following values: capacity = 10 front = 9 rear = 6 When a dequeue operation is performed on this circular queue, the front will be updated to 10.

FALSE This is a circular queue of capacity = 10; when front and rear reach 9 (the end of queue array) they must be reset to 0. So, the front will be updated to 0 not 10.

Which of the following is a correct statement to instantiate an object from this generic Stack: template< class Type, int MaxStackSize = 10 > class Stack{ private: int stackSize; int top; Type* stackArray; public: Stack(); ~Stack(); bool push(Type x); Type pop(); Type peek(); bool isEmpty(); bool isFull(); void display(); };

Stack <int> myStack;

TRUE/FALSE: The logarithm of product rule is: loga(x*y) = loga(x)+loga(y)

TRUE

TRUE/FALSE:Any pair of siblings in a binary heap tree are located next to each other in the heap array.

TRUE

What is the recurrence equations describes the worst case scenario for Quicksort algorithm?

The correct recurrence equation is: T(n) = T(n-1) + n The worst case in Quicksort algorithm is sorting a sorted list; the subproblem size is (n-1), and the partition takes (n) comparisons to find the correct pivot location.

WHAT DOES THIS FUNCTION DO? void functionExam (Queue q, Stack s){ count = 0 while( s is not empty){ if( q is not full){ q.enqueue ( s.pop() ) count++ } else{ print "queue is full" exit with error } } for (i = 0; i < count ; i++) { s.push( q.dequeue() ) } }

The stack contents will be reversed

True/False: Constructors are used to initialize the instantiated objects and are accessible from outside the class.

True

Which of the following statements is a correct delete statement in C++? Assume we want to delete the node that the currentPtr pointer is pointing at. The currentPtr is defined as follows: Node *currentPtr;

delete currentPtr;

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 45. Which (possibly multiple) of the following sequences could be the sequence of nodes examined? 5, 2, 1, 10, 39, 34, 77, 63

impossible

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 45. Which (possibly multiple) of the following sequences could be the sequence of nodes examined? 60, 20, 26, 27, 40, 44, 42.

impossible

What is the complexity of the following closed form: T(n) = 4 ^log2(n)

n^2 RULE: y^loga(x) = x^loga(y) T(n) = 4^log2(n) = n^log2(4) = n^2

What should we write inside "insertAtBeginning" function to update data/next in the new created node? void LinkedList::insertAtBeginning( const string addData ){ // some code here to create newNode pointer ... what should be written here to update data/next? // some code here to attach the new node to the list }

newNode->data = addData; newNode->next = nullptr;

counting sort is a ___________ based algorithm

non-comparison

Mark the following data structures as linear or non-linear: binary search trees

nonlinear

Mark the following data structures as linear or non-linear: binary trees

nonlinear

What is the correct implementation for the "isEmpty" member function? The member function is part of this generic Stack:

template< class Type, int MaxStackSize > bool Stack<Type, MaxStackSize>::isEmpty(){ if( top == -1 ){ return true; } else{ return false; } }

Which of the following is a correct implementation for "displayList( )" member function?

void LinkedList::displayList(){ Node* curr = head; while( curr != nullptr){ cout<<"("<<curr->data<<") -> "; curr = curr->next; } cout<<"00"<<endl; } // end LinkedList::displayList()


संबंधित स्टडी सेट्स

REL 140: Indian Boarding Schools Quiz

View Set

Adv. Accounting Chapter 9 Vocabulary

View Set

14.11.5 security troubleshooting

View Set