0% found this document useful (0 votes)
450 views11 pages

Sample Q & A

This document discusses various data structure and algorithm concepts: 1) It defines key terms like data structure, abstract data type, cyclic structure, and data abstraction. 2) It explains computing issues like time complexity, space complexity, and Big O notation that influence data structure choice. 3) It provides an example of how binary search works and its properties. 4) It represents a prefix expression as a binary tree, postfix form, and infix form. 5) It discusses algorithm analysis and the metrics of time and space complexity.

Uploaded by

wanjaba
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
450 views11 pages

Sample Q & A

This document discusses various data structure and algorithm concepts: 1) It defines key terms like data structure, abstract data type, cyclic structure, and data abstraction. 2) It explains computing issues like time complexity, space complexity, and Big O notation that influence data structure choice. 3) It provides an example of how binary search works and its properties. 4) It represents a prefix expression as a binary tree, postfix form, and infix form. 5) It discusses algorithm analysis and the metrics of time and space complexity.

Uploaded by

wanjaba
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 11

JOMO KENYATTA UNIVERSITY OF AGRICULTURE & TECHNOLOGY

ICS 2105: DATA STRUCTURES AND ALGORITHMS

Previous Sample questions and Answers

(c) Explain the following terms


[8 marks]

(d) Data Structure

A data structure is a particular way of storing and organizing data in a


computer so that it can be used efficiently.

(e) Abstract Data Type

If for a particular collection of data only the structure of data and the functions
to be performed on the data is defined but the implementation is not defined,
then such a
collection of data is called Abstract data type.

(f) Cyclic Structure

A data structure is cyclic if some of its references form a loop; that is, there's an
object that can be reached by following references from itself.
(g) Data Abstraction

The process by which data and programs are defined with a representation
similar to its meaning (semantics), while hiding away the implementation details.
Abstraction tries to reduce and factor out details so that the programmer can
focus on a few concepts at a time

(h) Explain three computing issues which influence the choice of the data structure.

[6
marks]

(i) Time complexity (with explanation)

(j) Space complexity (with explanation)

(k) Big O notation (with explanation)


(l) Using an example, explain how binary search works. What properties must the
input have?
[6 marks]

We usually need to search a value over an array of data. For example, searching whether
a book is in the library in a library indexing system. However, examining each element in
the array to find the value is too time consuming for a large array. Binary search provides
a much faster way to solve the searching problem. Here's how it works. In order to
perform binary search, the array to be searched should be first sorted. We start by looking
at the middle component. If the value it holds is too high, go to the middle of the bottom
half of the array and look again. If the middle components' stored value was too low, go
to the middle of the top half instead. Then, repeat this 'split the remainder' step until you
find the value you want, or decide that it doesn't exist.

(m)Represent the prefix expression -*E/BD-CA as a binary tree. Also write the
postfix form and the fully parenthesized infix form of the expression.
[6 marks]

Postfix: EBD/*CA- -

Infix : E(B/D)-(C-A)
(n) Define the term “Algorithm analysis”. Explain two important quantitative metrics
of interests in algorithm analysis. [4 marks]

The “analysis” deals with performance evaluation (complexity analysis).

Analysis of algorithms then is The theoretical study of computer-program performance


and resource usage.

(o) Time complexity

(p) Space complexity

(q) Given the following scenarios, state and explain the most suitable ADT to use. [6
marks]

(r) Serving customers in a banking hall------Queue

(s) Deleting characters from text editor using back space key.-------Stack

(t) Checking if expression has the correct set of delimiters.----Stack

(u) Using examples, distinguish between a statically allocated array and dynamically

allocated array. Use a computer programming language of your choice. [6 marks]

int main(int argc,char* argv)


{
char str[strlen(argv[1])+1];
return 0;
}

vs.

int main(int argc,char* argv)


{
char* str=malloc(strlen(argv[1])+1);
return 0;
}

(v) With the aid of examples, explain why an array-based implementation requires
less memory than a pointer-based implementation.
[6 marks]

(w) Write a recurrence relation that describes the worst-case time required by binary
search.

[2
marks]

O (n)

(x) Differentiate between double ended queue and circular linked list. Use
illustrations.

[4
marks]

In a standard linked list, you can only traverse the list in one direction. It may be
useful however, to be able to go both forwards and backwards when traversing a
linked list. In order to allow this, we need each Node to contain not one, but two
references to Nodes - one to the next Node in the list and one to the previous Node in
the list.

OR
Like an ordinary queue, a double-ended queue is a container. It supports the
following operations: enq_front, enq_back, deq_front, deq_back, and empty.
By choosing a subset of these operations, you can make the double-ended queue
behave like a stack or like a queue. For instance, if you use
only enq_front and deq_front you get a stack, and if you use
onlyenq_front and deq_back you get a queue.

A circularly linked list may be a natural option to represent arrays that are naturally
circular, e.g. the corners of a polygon, a pool of buffers that are used and released
in FIFO order, or a set of processes that should be time-shared in round-robin order.
In these applications, a pointer to any node serves as a handle to the whole list.
With a circular list, a pointer to the last node gives easy access also to the first node,
by following one link. Thus, in applications that require access to both ends of the list
(e.g., in the implementation of a queue), a circular structure allows one to handle the
structure by a single pointer, instead of two.

A circular list can be split into two circular lists, in constant time, by giving the
addresses of the last node of each piece. The operation consists in swapping the
contents of the link fields of those two nodes. Applying the same operation to any two
nodes in two distinct lists joins the two list into one. This property greatly simplifies
some algorithms and data structures, such as the quad-edge and face-edge.

(y) Use O notation to explain the computational complexity of:

(z) Inserting new element at the end of the linked-list

Linear ≈ n

(aa)Inserting new element at the middle of the linked-list

N-Log-N ≈ n log n

(bb)Convert the following infix expression into a binary tree: (a*b)+(k/m) [4 marks]
(cc)Explain three types of time complexities. [6 marks]

Worst-case Complexity

The running time for any given size input will be lower than the upper bound except
possibly for some values of the input where the maximum is reached.

Average-case Complexity

The running time for any given size input will be the average number of operations over
all problem instances for a given size.

Best case complexity

(dd)Assume there is a stack S and an auxilliary stack T. write pseudo-code to show


how the following operations can be performed using only ADT stack operations:

(ee)Displaying the contents of S in reverse order. [6 marks]

Use pop(object) from the top in the pseudo-code

(ff) Count the number of items in S. [4 marks]

Use the method size() in the pseudo-code

(gg)Delete a specified item from S, leaving the order of the remaining items
unchanged. [2
marks]

Use pop(object) method in the pseudo-code

(hh)Differentiate between a queue and a priority queue and give an example for each.

[4
marks]
In real life a queue is a line of customers waiting for service of some kind. In
most cases, the first customer in line is the next customer to be served. There are
exceptions, though. For example, at airports customers whose flight is leaving
imminently are sometimes taken from the middle of the queue. Also, at
supermarkets a polite customer might let someone with only a few items go first.
The rule that determines who goes next is called a queueing discipline. The
simplest queueing discipline is called FIFO, for "first-in-first-out." The most
general queueing discipline is priority queueing, in which each customer is
assigned a priority, and the customer with the highest priority goes first,
regardless of the order of arrival. The reason I say this is the most general
discipline is that the priority can be based on anything: what time a flight leaves,
how many groceries the customer has, or how important the customer is. Of
course, not all queueing disciplines are "fair," but fairness is in the eye of the
beholder.
The Queue ADT and the Priority Queue ADT have the same set of operations and
their interfaces are the same. The difference is in the semantics of the operations:
a Queue uses the FIFO policy, and a Priority Queue (as the name suggests) uses
the priority queueing policy.
As with most ADTs, there are a number of ways to implement queues Since a
queue is a collection of items, we can use any of the basic mechanisms for storing
collections: arrays, lists, or vectors. Our choice among them will be based in part
on their performance--- how long it takes to perform the operations we want to
perform--- and partly on ease of implementation.
(ii) What is the order of magnitude for the following expression (2n + 3)2 [4 marks]

Explain the following: O (n)2

(jj) Write a pseudo-code function Swap[L,I,J] that interchanges the items currently in
position I and J of a list. Define the function in terms of the ADT list operations,
so that it is independent of any particular implementation of the list. Assume that
the list has items at positions I and J. What impact does this assumption have on
your solutions?

[6 marks]

Use quicksort algorithm

int FindPivot(int A[],int l, int r)


{
switch (choice) {
case 1: return l;
case 2: return pivot2(A,l,h)
case 3: return l+random(r-l);
}
}

int partition(int A[], int l, int r)


{
int i,pivot, pivotpos;

pivotpos = FindPivot(A,l,r);
swap(&A[l],&A[pivotpos]);
pivotpos = l;
pivot =
A[pivotpos];

for (i = l+1; i <= r; i++) {


if (A[i] < pivot) {
pivotpos++;
swap(&A[pivotpos],&A[i]);
}
}

swap(&A[l],&A[pivotpos]);
return pivotpos;
}

void QuickSort(int A[], int l,int r,


int threshold)
{
int i,
pivot;

if (r-l>threshold) {
delay(CompareDelay);
pivot = partition(A,l,r);

QuickSort(A,l,pivot-1,threshold);
QuickSort(A,pivot+1,r,threshold);
}
}

int pivot2(int A[], int l, int r)


{
int i = (r+l)/2;

if ((A[l] <= A[i]) && (A[i] <= A[r]))


return i;
if ((A[r] <= A[i]) && (A[i] <= A[l]))
return i;
if ((A[r] <= A[l]) && (A[l] <= A[i]))
return l;
if ((A[i] <= A[l]) && (A[l] <= A[r]))
return l;
if ((A[l] <= A[r]) && (A[r] <= A[i]))
return r;
if ((A[i] <= A[r]) && (A[r] <= A[l]))
return r;
}

(kk)Write a pseudo-code to insert a new item at a given position in the array of list of
items.

[6 marks]

Linked List
==========
Declaring a Linked List
-----------------------------
Integer : Data
Pointer: Next
Declaring variables
-----------------------
Pointer : Start
Start = NULL 'Nothing in the Linked List
Creating a New Node with above structure.
---------------------------------------------------
Procedure Create(Value)
If there is enough memory available then 'Only create a Node if memory
is available
Create New Node in memory 'Creating a New Node for the
Linked List
Point its Next pointer to NULL 'Making a termination point for
the Node
If this is the first Node in the List then 'There must be a starting point
for the Linked List
Point Start to the Node
Otherwise 'If other Nodes exist then find the last
Node in the List
Go to memory location pointed by Start pointer 'Begin at the Start
(first Node)
Start Loop and run until Next points to NULL 'Check each Node until Next
pointer points to NULL
Go to the Node pointed by Next pointer 'Move from Node to Node
End Loop
Point Next of last Node to the newly created Node 'When last Node found,
point its Next pointer to
New Node
Put data in Value in new Node's Data 'Put the required data
(Value) in New Node's Data
variable
Endif
Otherwise 'Out of memory. Can't create a
New Node
Output error message that there is no more memory available
EndIf
End Procedure

(ll) What is the difference between binary search and linear search algorithms? [8
marks]

Linear search is easy but takes more time to search an element as it compares all
elements sequentially.
Elements r arranged randomly.

In binary search it start searching from middle, if the searching element is not
found
in middle then it goes to 1/4, and vice versa and hence take less time than linear
search.
Elements r arranged in sorted order.

OR
Linear search is very easy and always search in sequential way.
It starts searching from the first element up to the last element until the search
finds the
number .

Let us take an example.


Let there are a list of numbers.
1,3,8,6,5,8,2,9
We have to search if 5 is there in the list or not.
So, we start from the first element from 1.
then we see 3,8,6 and then find 5.
As soon as we find the search element 5 in the list we stop searching, otherwise
we
continue the search up to the last element.

Binary search

In binary search we always start from the middle element and compare the
middle
element to the search element.
If the search element is less than the middle element then we go to the lower
half
otherwise we go to the upper half.
In binary search the nos should be sorted in ascending order at first.

Let us took an example of binary search.


Assume that we are reading a book.
The book has a huge volume.
Let we have to find the page no 120 in this book.
The book has 200 pages approx.
Then we do not start looking for the page from the first page, rather we look at
the
middle.
If the arbitrary page is 101 then we think that 120 page is more than the 101
page so we
go to the half which contains more pages. So by using this method again and
again we can finally find out the page.

You might also like