Department of Information Technology: Data Structure Semester IV (4IT01) Question Bank Prepared by Prof. Ankur S. Mahalle
Department of Information Technology: Data Structure Semester IV (4IT01) Question Bank Prepared by Prof. Ankur S. Mahalle
UNIT I
2) What are list of areas in which data structures are applied extensively?
3) For each of the following patterns P and texts T, find the number C of comparisons to
find INDEX of P in T using the “Slow Algorithm:
a. P=abc, T=(ab)5 ii)P=abc, T=(ab)2n iii) P=aaa, T (aabb)3 .
4) Consider algorithm, which finds the location LOC and the value MAX of the largest
element in an Array DATA with n elements. Consider the complexity function C (n),
which measures the number of times LOC and MAX are updated in steps:
a. Describe and find C (n) for the worst case.
b. Describe and find C (n) for the best case.
c. Find C(n) for the average case when n=3, assuming all arrangements of the
elements in data are equally likely.
Ans: a) The worst case occurs when the elements of DATA are in increasing order, where
each comparison of MAX with DATA[K] forces LOC and MAX to be updated. In this case
C(n)=n-1.
b) The best case occurs when the largest element appears first and so when the comparision
of MAX with DATA[K] never forces LOC and MAX to be updated. Accordingly , in this
case, C(n)=0
c) Let 1,2 and 3 denote, respectively, the largest, second largest and smallest elements of
DATA. There are six possible ways the elements can appear in DATA, which correspond to
the 3!=6 permutations of 1,2,3. For each permutation p, let n p denote the number of times
LOC and MAX are updated when the algorithm is executed with input p. The six
permutations p and the corresponding values np follow:
Permutation p: 123 132 213 231 312 321
Value of np: 0 0 1 1 1 2
Assuming all permutations p are equally likely
0+0+1+1+1+2 5
C(3)= =
6 6
5) Consider the pattern P=aba. Use the slow pattern matching algorithm to find the number
of comparisons C to find the index of P in each of the following text: i)a10 ii) (aba)10.
6) Linked storage of strings is more efficient than other forms of storage. Justify the
statement with an example.
7) The complexity of an algorithm depends not only on the size n of input data but also on
particular data. Justify the statement with an example.
8) Consider the array A: 8 9 7 5 4 3 11. Write an efficient algorithm to find sum of all even
and odd numbers in A.
10) Write an algorithm for linear search and obtain an expression for its complexity.
11) Consider a text string T= (abcde)5. Determine the number of comparisons C required to
find the index I of the following pattern strings P1 in the text string T:
a. P1-abcde ii)P2-cde iii) eabcd iv)ijkf
12) Write a procedure FIND (DATA, N, LOC1, LOC2) which finds the location LOC1 of
largest element and the location LOC2 of the element in an array DATA with N>1
elements.
15) Consider the pattern P=abc. Using the slow pattern matching algorithm, find the number
C comparisons to find the INDEX of P in each of the following texts T:
a. a10 ii) (aba)10 iii) d10 iv) (cbab)10 where n>3
16) For each of the following pattern P and text T, find the number of C comparisons to find
INDEX of P in T using slow pattern matching algorithm.
a. P=a b c, T=(ab)5 ii)P=ab c, T=(ab)20 iii)=a a a, T=(a a b b)3
19) Write the algorithm for linear search and explain its complexity?
21) What are the various operations associated with word processing? How they are
implemented using basic string operations?
22) Describe in brief the meaning of static, semistatic and dynamic variables. Give the
advantages of each.
23) Consider the pattern P=aaabb. Construct the table and the corresponding labeled directed
graph used in the ‘fast’ pattern matching algorithm.
24) Find the table and corresponding graph for the second pattern matching algorithm where
the pattern P=ababab.
25) Describe in brief traversing, sorting and searching. Define in one line inserting and
deleting.
UNIT II
2) Explain and write the algorithmic steps for linear search and find its complexity
5) What is bubble sort? And write steps of bubble sort algorithm for following example: 32
51 27 85 66 23 13 57
6) Write an algorithm Consider the string S=” AMRAVATI”. Apply bubble sort to arrange
the characters in S in alphabetic order. Show all the passes. Also find the number of
comparisons and number of exchanges.
10) Using bubble sort find the number of comparisons and the number of interchanges which
alphabetize the letters in NAGPUR. Show all intermediate passes.
11) Consider the following multidimensional arrays: X (-5:5, 3:33) Y (3:10, 1:15, 10:20)
a. Find the length of each dimension and the number of elements in X & Y.
b. Suppose Base (Y) =400 and there are W=4 words per memory location, find the
effective indices E1, E2, E3 and the address of Y [5, 10, 15] assuming
i. Y is stored in row major order
ii. Y is stored in column major order.
13) Using bubble sort algorithm, find the number C of comparisons and the number D of
interchanges which alphabetize the n=6 letters in PEOPLE.
15) Suppose multidimensional array A and B are declared using A (-2:2, 2:22) and B (1:8,
-5:5, -10:5)
i. Find the length of each dimension and the number of elements in A and B.
ii. Consider the element B [3, 3, 3] in B. Find the address of the element, assuming Base
(B) =400 and w=4 words/memory location.
17) Consider two matrices A and B of the order m x p and p x n respectively give and explain
the algorithm for matrix multiplication of A and B.
18) What is a pointer array? What is the need of pointer array? Explain with example.
20) Modify the binary search algorithm, so that it becomes a search and insertion algorithm.
21) Suppose a hospital wants to keep record of 40 new born babies which contains the
following data items: Name, Sex, Birthday, Father, Mother. Suppose father Birthday is
group item with subitems Month, Day and Year and Father and Mother are group items
with subitems Name and Age. Give the record structure for the above example.
22) Explain why a 2D array is not suitable representation for sparse matrix? Suggest and
explain an efficient representation for a sparse matrix.
23) Name data structure which efficiently stores the contents of two dimensional arrays when
all the elements above the main diagonal are zero. Obtain an expression for L such that
B[L]=Ajk
UNIT III
2) Discuss the advantages, if any, of a two-way list over a one-way list for each of the
following operations:
i. Traversing the list to process each node.
ii. Deleting a node whose location LOC is given.
iii. Searching an unsorted list for a given element ITEM
iv. Searching a sorted list for a given element ITEM.
v. Inserting a node before the node with a given location LOC.
vi. Inserting a node after the node with a given location LOC.
4) Explain linked list and insert a node into middle of linked list with example.
8) Let LIST be a linked list containing integers. Write an algorithm to find the sum of
elements on the LIST.
11) Write the advantages of a two-way list over a one way list for the following operations:
i. Deleting a node whose location LOC is given.
ii. Inserting a node before the node with a given location LOC.
12) Write a procedure SORT (INFO, LINK, START) which sorts a list without changing
values in INFO.
13) Write an algorithm to insert ITEM in singly linked list so that ITEM follows the node
with location LOC or inserts ITEM as the first node when LOC=NULL.
START 1 Kiran 7
5 2 6
3 Deepak 11
4 Manoj 12
5 Arun 3
AVAIL
6 0
10 7 Lalit 4
Determine the changes in 8 Gouri 1 the data structure if:
i. Walter is added to 9 Sahil 0 the list and then
ii. Kiran is deleted and 10 2 then
iii. Rohit is added and 11 Fatima 8 then
iv. Sahil is deleted. 12 Nancy 9
Draw the diagrammatic representation after every change.
16) Text P(x) denote the following polynomial P(x)=9x3+7x2-3x+8. Give the diagram to
represent P(x) by header list. Draw an array representation of this header list.
18) What is meant by garbage collection? Explain the overflow and underflow condition.
START 1 A 2
4 2 B 8
3 6
4 C 7
AVAIL
5 D 0
3 6 0
7 E 1
8 F 5
i. Find sequence of characters in linked list.
ii. Suppose F and then C are deleted from list and then G is inserted at the beginning
of the list. Find final structure
iii. Suppose G is inserted at the beginning of list and then F and and then C are
deleted from structure. Find the final structure.
20) Suppose LIST is header circular list in memory. Write an algorithm which deletes the last
node from LIST.
21) Let P(x)=2x8-5x7-3x2+4 Give a diagram to represent p(x) by header list. Draw an array
representation of this header list.
22) What is header linked list? Explain with example different kinds of header linked lists.
23) Suppose NAME1 is a list in memory. Write an algorithm which copies NAME1 into a list
NAME2.
25) Explain a two way list is a linear collection of data elements, draw schematic diagram of
such list. Write advantages of two-way header lists over one way header lists.
UNIT IV
1) Describe the algorithm for TOWERS of HANOI with their steps.
2) Define the following:
i. DEQUES
ii. PRIORITY QUEUES
iii. STACK
3) Consider following arithmetic expression P in postfix notation: P: 5, 6, 2, +,*, 12, 4, 1, -
Evaluate the expression and hence write the algorithm evaluation of postfix expression.
4) Write algorithm for linked representation of stack (PUSH and POP)
5) Consider the postfix expression AB+CDF/-*. Let A=1, B=2, C=3, D=4, and F=5.
Evaluate the expression and hence write the algorithm for evaluation of postfix
expression.
6) What is queue? Assuming array representation writes an algorithm for insertion and
deletion from a queue.
7) Consider the infix expression (A+B)*C-D/F. Covert the expression to reverse the polish
notation. Show all the steps. Hence write the algorithm for the same.
8) What is stack? Write an algorithm for push and pop operations on stack.
9) Consider the following arithmetic expression Q:A+(B*C-(D/E+F)*G)*H. Using the
algorithm for the transforming infix to postfix transform Q to postfix form. Show the
symbol scanned; stack contents and intermediate expression at each step.
10) Write procedure for inserting into a linked queue.
11) What is priority queue? Give array representation of priority queue.
12) What is priority queue? Give one way list representation of a priority queue.
13) Consider following stack where STACK is allocated N=4 memory cells:
STACK : aaa bbb ccc
TOP :1 2 3
Describe the stack as the following operations take place:
i. Push(STACK, ddd)
ii. Pop(STACK, item)
iii. Push(STACK, eee)
15) Consider following stack where STACK is allocated N=6 memory cells:
STACK: AA DD EE FF,-------,--------
Describe the stack as the following operations take place:
i. Push(STACK, GG)
ii. Push(STACK, KK)
iii. Pop(STACK, item)
iv. Push(STACK, LL)
v. Pop(STACK, item)
vi. Push(STACK, TT)
vii. Push(STACK, RR)
16) Suppose S is the following list of 14 alphabetic characters: DATASTRUCTURES.
Suppose the characters in S are to be sorted alphabetically. Use quick sort algorithm to
find the final position of first character D. Show intermediate steps.
17) Write an algorithm to evaluate postfix expression and solve following with the help of
algorithm P:3,6,2,+,*,12,3,1,-
18) Suppose S consists of the following n=5 letters: A B C D E. Find the number of
comparisons to sort S using quick sort.
Unit VI
1. Explain merge sort and sort the following 14 elements:
66, 33, 40, 22, 55, 88, 60, 11, 80, 20, 50, 44, 77, 30.
2. Explain the algorithm for topological sort and find a topological sort T of a graph S
without cycles.
3. Sort the following elements by using Radix sort algorithm and find its complexity: 348,
143, 361, 423, 538, 128, 321, 543, 366.
4. Assume that an array A contains 8 elements. A: 80, 20, 25, 10, 90, 100, 70, 60. Apply
selection sort to sort the elements of A in ascending order. What will be the best case for
this algorithm?
5. Assume that array A contains 8 elements :
77, 88, 99, 100, 10, 20, 25, 18. Apply selection sort to sort the numbers in ascending
order.
6. Assume that an array A contains 8 elements :
A: 90, 80, 70, 20, 15, 5, 7, 8. Apply insertion sort to A so that all the elements of A are
arranged in descending order.
7. Sort the following 8 elements by using insertion sort. Also explain the complexity of
insertion sort algorithm:
77, 33, 44, 11, 99, 22, 66, 55.
8. Write an algorithm for breadth-first search.
9. Write an algorithm for depth-first search.
10. Mention the search technique in which the search time is independent of the number of
elements in the collection. Explain the different challenges for this technique.
11. What is collision? What are the different collision resolution techniques? Explain any two
of them.
12. Draw and explain the linked representation of the following graph:
A D
B C
13. Suppose the following graph G represents the daily flights between coties of some airline,
find minimum path P from A to J.
F C B
D E G
J K
14. Find the adjacency matrix A of the following graph G. Also explain whether G is strongly
connected or not?
X Y
Z W
15. What is meant by Hash function? Give the three methods for calculating Hash function.
16. Suppose the table T has 11 memory locations. T[1], T[2], . . . . .T[11] and suppose the file
F consists of 8 records, A, B, C, D, E, X, Y and Z : with following hash addresses:
Record: A, B, C, D, E, X, Y, Z
H(K) : 4, 8, 2, 11, 4, 11, 5, 1
Calculate the average number S of probes for successful search and average number U of
probes for an unsuccessful search.
17. Explain the method for traversing through the graph: with example.
18. Give and explain Warshall’s algorithm with example.
19. Explain Warshall’s algorithm for finding the shortest path with suitable example.
20.
Assignment Questions
Unit II
Que.1: Let A be an n x n square matrix array. Write a module which
a) Finds the number NUM of nonzero elements in A
b) Finds the SUM of the elements above the diagonal, i.e., elements A[I,J] where
I<J
c) Finds the product PROD of the diagonal elements (a11, a22,…..,ann)
Solution:
a)
1. Set NUM:=0
2. Repeat for I=1 to N:
3. Repeat for J=1 to N:
If A[I,J]!=0, then: Set NUM=NUM+1
[End of inner loop]
[End of outer loop]
4. Return.
b)
1. Set SUM=0
2. Repeat for J=2 to N:
3. Repeat for I=1 to J-1:
Set SUM=SUM+A[I,J]
[End of inner Step 3 loop]
4. Return
c)
1. Set PROD=1
2. Repeat for K=1 to N:
SEt PROD= PROD*A[K*K]
[End of loop]
3. Return
Que.2: Consider the following multidimensional arrays: X (-5:5, 3:33) Y (3:10, 1:15,
10:20)
a. Find the length of each dimension and the number of elements in X & Y.
b. Suppose Base (Y) =400 and there are W=4 words per memory location, find the
effective indices E1, E2, E3 and the address of Y [5, 10, 15] assuming
i) Y is stored in row major order
ii) Y is stored in column major order.
Solution:
a) The length of a dimension is obtained by:
Length = upper bound – lower bound + 1
Hence find Li of the dimensions of A, L1, L2, L3
b) Find effective index Ei is obtained form Ei=ki-LB, Where Ki is the given index and LB is
the lower bound. Hence found E1, E2, E3
c) The address depends on whether the programming language stores B in row-major order or
column-major order.
Formulas:
Base(C) Denotes the address of the first element of C, and w denotes the number of words
per memory location.
Que3. : Prove the following identity, Which is used in the analysis of various sorting and
searching algorithms:
n(n+1)
1+2+3+-------+n=
2
Solution: Writing the sum S forward and backward, we obtain :
S=1+2+3+. . . .+ (n-1) + n
S=n+ (n-1) + (n-2) +. . . +2+1
We find the sum of the two values of S by adding pairs as follows:
2S=(n+1)+(n+1)+(n+1)+. . . +(n+1)+(n+1)
There are n Such sums, so 2S=n(n+1). Dividing by 2 gives us our result