Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

A Short Guide to Written Exam 1 Data Structure

Data Structure

Very Very Important Questions


1. Difference between linear and Nonlinear data structure. [Pally Sa.-2018]
2. Describe infix to postfix convartion method. Advantage of stack using link list
then array. [Basic Bank(AM)-2019]
3. What are the operations performed on a data structure? What is prefix, post fix
and infix operation? [Officer(IT/ICT)-2019]
4. Draw a Binary min heap that results from inserting 9, 3, 7, 4, 8, 2, 6, 5 in that
order into an initially empty heap. Show each insertion step separetly. You do not
need to show the array representation. Just draw the trees. [Uttara Bank(AP)-2019]
5. Describe four types of sorting algorithm with example. [Combined(SO-IT/ICT)-2019]
6. Advantage of marge sort, heap sort, Insertion sort. Advantage and complixity of
them. [Basic Bank(AM)-2019]
7. Draw a flowchart to input five positive numbers, and sort them is ascending
order. [Combined 3 bank (AP)-2018]
8. Write a algorithm to find a node in a binary search tree. [Pally Sa.-2018]
9. Difference between stacks and queue with example. [Engineering college lecturer -2017]
10. Write down time complexities of different sorting algorithm. [DBBL-2016]
11. Explain BFS Traversal of a graph with an example. [DBBL-2016]

1. What is Data Structure? Write down the example of data Structure?


Data Structure
A data structure is a particular way of organizing data in a computer so that it can be used
effectively. That means a data structure is a structural relationships of elementary data items for
storing and retrieving data in computer's memory.For example, we can store a list of items having
the same data-type using the array data structure.
Examples of Data Structure: Array, Pointer, Structure, Linked List, Stack, Queue, Graph,
Searching, Sorting, Programs, etc.
A Short Guide to Written Exam 2 Data Structure

2. Difference between linear and non linear data structure?

Linear Data Structure Non Linear Data Structure


In a linear data structure, data elements are
arranged in a linear order where each and In a non-linear data structure, data elements
1.
every elements are attached to its previous are attached in hierarchically manner.
and next adjacent.

In linear data structure, single level is Whereas in non-linear data structure, multiple
2.
involved. levels are involved.

Its implementation is easy in comparison While its implementation is complex in


3.
to non-linear data structure. comparison to linear data structure.

While in non-linear data structure, data


In linear data structure, data elements can
4. elements can’t be traversed in a single run
be traversed in a single run only.
only.

In a linear data structure, memory is not While in a non-linear data structure, memory
5.
utilized in an efficient way. is utilized in an efficient way.

Its examples are: array, stack, queue,


6. While its examples are: trees and graphs.
linked list, etc.
A Short Guide to Written Exam 3 Data Structure

Applications of linear data structures are


Applications of non-linear data structures are
7. mainly in application software
in Artificial Intelligence and image processing.
development.

3. Write Down the Operations of Data Structure?


Operations on Data Structure: The basic operations that are performed on data structures are as
follows:
Insertion:Insertion means addition of a new data element in a data structure.
Deletion: Deletion means removal of a data element from a data structure if it is found.
Searching: Searching involves searching for the specified data element in a data structure.
Traversal: Traversal of a data structure means processing all the data elements present in it.
Sorting: Arranging data elements of a data structure in a specified order is called sorting.
Merging: Combining elements of two similar data structures to form a new data structure of the
same type, is called merging.

4. Write down the details of time and space complexity of algorithm?

 Introduction
In computer science, the time complexity of an algorithm quantifies the amount of time taken by
an algorithm to run as a function of the length of the string representing the input.
 Big O notation
The time complexity of an algorithm is commonly expressed using big O notation, which
excludes coefficients and lower order terms. When expressed this way, the time complexity
is said to be described asymptotically, i.e as the input size goes to infinity.
For example, if the time required by an algorithm on all inputs of size n is at most 5n3 + 3n,
the asymptotic time complexity is O(n3). More on that later.
Few more Examples:
 1 = O(n)
 n = O(n2)
 log(n) = O(n)
 2 n + 1 = O(n)
 O(1) Constant Time:
An algorithm is said to run in constant time if it requires the same amount of time regardless
of the input size.
 Examples array: accessing any element
 fixed-size stack: push and pop methods
 fixed-size queue: enqueue and dequeue methods
A Short Guide to Written Exam 4 Data Structure
 O(n) Linear Time
An algorithm is said to run in linear time if its time execution is directly proportional to the
input size, i.e. time grows linearly as input size increases.Consider the following examples,
below I am linearly searching for an element, this has a time complexity of O(n).
 More Examples:
 Array: Linear Search, Traversing, Find minimum etc
 ArrayList: contains method
 Queue: contains method
 O(log n) Logarithmic Time:
An algorithm is said to run in logarithmic time if it’s time execution is proportional to the
logarithm of the input size.
Example: Binary Search
Recall the twenty question game - the task is to guess the value of a hidden number in an
interval. Each time you make a guess, you are told whether your guess is too high or too low.
Twenty questions game implies a strategy that uses your guess number to halve the interval
size. This is an example of the general problem-solving method known as binary search
 O(n2) Quadratic Time
An algorithm is said to run in quadratic time if it’s time execution is proportional to the
square of the input size.
Examples:
 Bubble Sort
 Selection Sort
 Insertion Sort
[see more details cloud it book data structure chapter]

5. How Master algorithm solving recurance?


Master Algorithm for solving recurrence
T (n) = aT(n/b)+ϴ(nklogpn)
Where a>=1, b>1, k>=0 and p is a real number
Case 1: if a>bk then T(n) = ϴ(nlogba)
Case 2 :if a=bk then
log
If p<-1 T(n)=ϴ(n ba)
log a log 2 n
If p=-1 T(n)= ϴ(n b )
log a
If p>-1 T(n)= ϴ(n b log p+1n )
Case 3:if a<bk then
If p<0 then T(n) =O(nk)
k p
If p>=0 then T(n)= ϴ(n log n)
6. How to find location of an array?
Location of an element of a two-dimensional array
Row-major Order:
A Short Guide to Written Exam 5 Data Structure
IfLoc (A[i, j) denotes the location in the memory of the element A[i][j] orAij, then in row-major
order-
Loc (A[i, j])=Base (A) + (n (i-1)+ (j-1))* p;
Here Base (A) is starting or base address of the array A, n is the number ofcolumns and p is the
width of each cell, i.e, number bytes per cell.
Column-major Order:
In column-major order,
Loc (A[i, j]) Base (A) + (m(j - 1)+(i- 1)) *p
Here Base (A) is starting or base address of the array A, m is the number ofrows and p is the cell
width.
Example:
Base address, Base (A)=100, Size of the array=5 x 6. If the type of array isinteger then find out
Loc (A[4, 3])
Solution:
If the array is stored in row-major order:
Loc (A[4, 3]) Base (A)+ (n (i- 1)+(j - 1))* p
= 100+ (6 x 3+2)* 2 [2 bytes for each integer]
= 100 + 40
= 140
If the array is stored in memory in column-major order:
Loc (A[4, 3])= Base (A)+ (m (j-1) + (i-1))*p
= 100 + (5 x 2+3)* 2
= 100+ 26
=126
7. How arrays differ from link list?
 The size of the arrays is fixed; Linked Lists are Dynamic in size.
 Inserting and deleting a new element in an array of elements is expensive, whereas both
insertion and deletion can easily be done in Linked Lists.
 Random access is not allowed in Linked Listed.
 Extra memory space for a pointer is required with each element of the Linked list.
 Arrays have better cache locality that can make a pretty big difference in performance.

8. What is stack? Write down the basic feature of stack?

Stack:
Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure
that allows adding and removing elements in a particular order. Every time an element is added, it
goes on the top of the stack and the only element that can be removed is the element that is at the
top of the stack, just like a pile of objects.

Example: a deck of cards or a pile of plates etc.


Basic features of Stack
A Short Guide to Written Exam 6 Data Structure
 Stack can be implementing using array or link list where user is only allowed to pop or
push items.
 Stack is an ordered list of similar data type.
 Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
 push() function is used to insert new elements into the Stack and pop() function is used to
remove an element from the stack. Both insertion and removal are allowed at only one
end of Stack called Top.
 Stack is said to be in Overflow state when it is completely full and is said to be in
Underflow state if it is completely empty.

9. What are the Applications of stack?


Applications or Use of Stack
 Parsing
 Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
 Reverse a String using Stack
 Check for balanced parentheses in an expression
 Implement two stacks in an array
 Functional Calls: When a function is called all local storage for the function is allocated
on system ‘stack’ and return address also pushed on the system stack.

10. What is the pop sequence of the following operation:


push(2), push(1), pop, push(3), push(2), pop, pop, pop, push(2),pop
Stack Pop sequence
Operation

push(2) 2 -

push(1) 2,1 -

pop 2 1

push(3) 2,3 1

push(2) 2,3,2 1

pop 2,3 1,2

pop 2 1,2,3

pop - 1,2,3,2

push(2) 2 1,2,3,2

pop - 1,2,3,2,2
A Short Guide to Written Exam 7 Data Structure
11. What are Infix, prefix, Postfix notations?
 Infix notation: X + Y – Operators are written in-between their operands. This is the
usual way we write expressions. An expression such as A*(B+C)/D
 Postfix notation (also known as “Reverse Polish notation”): X Y + Operators are
written after their operands. The infix expression given above is equivalent to A B C +
*D/
 Prefix notation (also known as “Polish notation”): + X YOperators are written before
their operands. The expressions given above are equivalent to / * A+ B C D
12. What is the data structures used to perform recursion?
 Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows
whom to return when the function has to return. Recursion makes use of system stack
for storing the return addresses of the function calls.
 Every recursive function has its equivalent iterative (non-recursive) function. Even when
such equivalent iterative procedures are written, explicit stack is to be used.

13. What is queue?


Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.

14. What is a Queue, how it is different from stack and how is it implemented?
Queue is a linear structure which follows the order is First In First Out (FIFO) to access
elements. Mainly the following are basic operations on queue: Enqueue, Dequeue, Front,
Rear
The difference between stacks and queues is in removing. In a stack we remove the item the
most recently added; in a queue, we remove the item the least recently added. Both Queues
and Stacks can be implemented using Arrays and Linked Lists.

15. Write down the application of queue?

Application and uses of queue: Queue, as the name suggests is used whenever we need to
manage any group of objects in an order in which the first one coming in also gets out first
while the others wait for their turn

 Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
 In real life scenario, Call Center phone systems uses Queues to hold people calling
them in an order, until a service representative is free.
 Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.
 Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
16. Write down the different between stack and queue?
A Short Guide to Written Exam 8 Data Structure
Stack Queue
It represents the collection of It represents the collection of elements
elements in Last In First Out order. in First In First Out order.
Operations includes testing null Operations include testing null queue,
stack, finding the top element in the finding the next element, removal of
stack, removal of top most element elements and inserting the elements
and adding elements on the top of the from the queue.
stack.
element inserted first will be element inserted first will be served
accessed last in stack as it has only first
one open end
Push, pop is use for insert and delete Front and rear is use for insert and
items. delete items.
Example is plates placed one above Example is queue for bus tickets.
other.
17. Write down the details on tree?

TREE
Tree is non-linear data structure designated at a special node calledroot and elements are arranged
in levels without containing cycles.
Or
The tree is
 Rooted at one vertex
 Contains no cycles
 There is a sequence of edges from any vertex to any other
 Any number of elements may connect to any node (includingroot)
 A unique path traverses from root to any node of tree
 The elements are arranged in layers

 Node : This is an individual element (i.e., data) that is shown as a circle.


 Root : This is the highest-level node. A tree structure has only one root.
 Leaf : This is the node that does not have any lower-level node.
 Branch : This is a line that connects to each node (including root, leaf).
 Level : This is the depth of hierarchy of a tree structure
18. Construct an expression tree by using the following algebraic expression.
(a + b) / (a*b - c) + d
A Short Guide to Written Exam 9 Data Structure

19. Write down the properties of binary tree?

Properties of binary tree


 Binary tree partitioned into three parts.
 First subset contains root of tree.
 Second subset is called left subtree.
 Another subset is called right subtree.
 Each subtree is a binary tree.
 Degree of any node is 0/1/2.
 The maximum number of nodes in a tree with height 'h' is2 h+1 -1
 The maximum number of nodes at level 'k' is 2k-1.
 For any non-empty binary tree, the number of terminal nodes
 with n2, nodes of degree 2 is N0= n2 +1
 The maximum number of nodes in a tree with depth d is 2d -1.
20. Write down the application of a tree?

Appications of tree
1. Binary trees can represent arithmetic expressions.
An infix expression will have a parent operator andtwo children operands.
 Consider the expression ((3 + (7*2))-1)
 Each parenthesised expression becomes a tree.
 Each operand is a leaf, each operator is an internal node.
A Short Guide to Written Exam 10 Data
Dat Structure

2. To evaluate the expression


on tree
tree.
3. Binary trees in a famous file co
compression algorithm Huffman coding tree
 Each character is stored
tored iin a leaf
21. Write down the basic of somsome graph?

Basics of Graph
A graph is a set of nodes (vertic
(vertices) and edges. The node that holds data iscalled
lled vertex
ve and the
line connecting two vertices
es is ca
called edge. If G denotesa graph, G= (V, E) wherehere V denotes set
of vertices and E denotes set
et ofed
ofedges.
Undirected Graph: If eachh edge of a graph is undirected (without direction), the graphgr is called
undirected graph. The undirected
irected edge is alsocalled unordered edge.
Directed Graph: If each edge oof a graph is directed (with direction), thegraph h is called
ca directed
graph.
Weighted Graph: If the value
alue (c
(cost) of each edge is given, the graph iscalled weighted
weight graph.
Path: A path is a sequencee of vevertices where each pair of successive verticesisis connected
con by an
edge
Connected Graph: A graphh is ca called connected, if there is a path between eachh pair of vertices.
Cycle: A cycle is a path where
here fi
first and last vertices are the same.

22. Write an algorithm which


hich ssearch a value linearly?

Algorithm for linear searching:


ching:
1. Input a list and an ite
item
A[1..n], item=x,
m=x, location=0;
2. Search the list to find the target element
for(i=1;i<=n;n; i=i
i=i+1)
{
If(A[i] =item)
{
Print “found”
Location =I;
Stop searching;
hing;
}
A Short Guide to Written Exam 11 Data Structure
}
3. If(i>n) print”Not Found”
4. Output: “Found ” or “Not Found”
23. Calculate the time complexity of linear search?

The time complexity 1 + 2 + 3 + ⋯+


=
( + 1)
=
2

= +
=O(n)
24. Write an algorithm which search a value divide and conquer process?
Or
Write a algorithm to find a node in a binary search tree.
Algorithm for binary searching:[divide and conquer process]

1. Input A[1…m],x (where we find x in the A list)


2. So, first =1 , last=m;
3. while(first<= last)
{
mid=(first+last)/2;
i. if(x=A[mid]), then print “mid”
stop searching;
ii. else if (x<A[mid]) then last=mid-1
iii. else first =mid+1
}
4. If (first> last) print “Not found”
5. Output: “mid” or “Not found”

You might also like