Sample Q & A
Sample Q & A
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.
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]
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]
(q) Given the following scenarios, state and explain the most suitable ADT to use. [6
marks]
(s) Deleting characters from text editor using back space key.-------Stack
(u) Using examples, distinguish between a statically allocated array and dynamically
vs.
(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.
Linear ≈ n
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.
(gg)Delete a specified item from S, leaving the order of the remaining items
unchanged. [2
marks]
(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]
(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]
pivotpos = FindPivot(A,l,r);
swap(&A[l],&A[pivotpos]);
pivotpos = l;
pivot =
A[pivotpos];
swap(&A[l],&A[pivotpos]);
return pivotpos;
}
if (r-l>threshold) {
delay(CompareDelay);
pivot = partition(A,l,r);
QuickSort(A,l,pivot-1,threshold);
QuickSort(A,pivot+1,r,threshold);
}
}
(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 .
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.