0% found this document useful (0 votes)
2 views

Data Structures Practice Questions

The document contains practice questions and solutions on various data structure topics, including array address calculation, stack implementation, binary tree construction, sorting algorithms, graph algorithms, hashing, B-trees, AVL trees, stack applications, and minimum spanning trees. Each section provides a question followed by a detailed solution, including algorithms and calculations. The document serves as a comprehensive guide for understanding and applying fundamental data structure concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures Practice Questions

The document contains practice questions and solutions on various data structure topics, including array address calculation, stack implementation, binary tree construction, sorting algorithms, graph algorithms, hashing, B-trees, AVL trees, stack applications, and minimum spanning trees. Each section provides a question followed by a detailed solution, including algorithms and calculations. The document serves as a comprehensive guide for understanding and applying fundamental data structure concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Most Probable Practice Questions for

Data Structures
1. Array Address Calculation
Question: Consider a 3-dimensional array A[10][20][30] with base address 1000. If each element occupies 4 bytes and the array follows row-major ordering
with the first element stored at A[2][2][2], calculate the address of A[5][10][15].

Solution: In row-major ordering, the formula for address calculation is:

Address(A[i][j][k]) = Base + ((i-lower_bound_i) * n_j * n_k + (j-lower_bound_j) * n_k + (k-lower_bound_k)) * size

Given:

Base address = 1000


Lower bounds = [2,2,2]
Dimensions = [10,20,30]
Element size = 4 bytes

For A[5][10][15]:

Address = 1000 + ((5-2) * 20 * 30 + (10-2) * 30 + (15-2)) * 4


Address = 1000 + (3 * 600 + 8 * 30 + 13) * 4
Address = 1000 + (1800 + 240 + 13) * 4
Address = 1000 + 2053 * 4
Address = 1000 + 8212
Address = 9212

2. Stack Implementation and Applications


Question: Write an algorithm to convert an infix expression to postfix expression. Convert the expression A+(B*C-(D/E^F)*G)*H to its equivalent postfix
notation.

Solution:

Algorithm for Infix to Postfix conversion:

1. Initialize an empty stack and result string


2. For each character in the infix expression:
a. If character is operand, add to result
b. If character is '(', push to stack
c. If character is ')', pop from stack and add to result until matching '(' is found
d. If character is operator, pop operators from stack and add to result as long as
they have higher or equal precedence, then push current operator
3. Pop any remaining operators from stack and add to result

Converting A+(B*C-(D/E^F)*G)*H:

Scan Stack Output


A empty A
+ + A
Scan Stack Output
( +( A
B +( AB
* +(* AB
C +(* ABC
- +(- ABC*
( +(-( ABC*
D +(-( ABC*D
/ +(-(/ ABC*D
E +(-(/ ABC*DE
^ +(-(/^ABC*DE
F +(-(/^ABC*DEF
) +(- ABC*DEF^/
* +(-* ABC*DEF^/
G +(-* ABC*DEF^/G
) + ABC*DEF^/G*-
* +* ABC*DEF^/G*-
H +* ABC*DEF^/G*-H
end empty A B C * D E F ^ / G * - H * +

Result: ABCDEF^/G-H*+

3. Binary Tree Construction


Question: Construct a binary tree from the given traversals. Inorder: B,I,D,A,C,G,E,H,F Postorder: I,D,B,G,C,H,F,E,A

Solution: From the postorder sequence, we know that 'A' is the root (last element). In the inorder sequence, 'A' appears in the middle, dividing the
sequence into left and right subtrees.

Left subtree inorder: B,I,D Left subtree postorder: I,D,B

Right subtree inorder: C,G,E,H,F Right subtree postorder: G,C,H,F,E

We can recursively construct the tree:

1. 'A' is the root


2. For the left subtree, 'B' is the root
Left of 'B': None
Right of 'B': 'I', 'D' where 'D' is the root
3. For the right subtree, 'E' is the root
Left of 'E': 'C', 'G' where 'C' is the root with 'G' as right child
Right of 'E': 'H', 'F' where 'H' is the root with 'F' as right child

Preorder traversal: A,B,D,I,E,C,G,H,F

4. Sorting Algorithms
Question: Write an algorithm for Quick Sort and apply it to sort the array [15, 22, 30, 10, 15, 64, 1, 3, 9, 2].

Solution:

Quick Sort Algorithm:


quickSort(arr, low, high):
if low < high:
pivotIndex = partition(arr, low, high)
quickSort(arr, low, pivotIndex - 1)
quickSort(arr, pivotIndex + 1, high)

partition(arr, low, high):


pivot = arr[high]
i = low - 1
for j from low to high-1:
if arr[j] <= pivot:
i = i + 1
swap arr[i] and arr[j]
swap arr[i+1] and arr[high]
return i+1

Applying to [15, 22, 30, 10, 15, 64, 1, 3, 9, 2]:

First partition (pivot = 2): [1, 2, 30, 10, 15, 64, 15, 3, 9, 22]

Recursive calls:

Left: [1]
Right: [30, 10, 15, 64, 15, 3, 9, 22]

Second partition (pivot = 22): [3, 10, 15, 9, 15, 22, 64, 30]

Continue recursively:

Final sorted array: [1, 2, 3, 9, 10, 15, 15, 22, 30, 64]

5. Graph Algorithms
Question: Apply Dijkstra's algorithm to find the shortest path from vertex S to all other vertices in the given graph. [Graph representation with vertices S, A,
B, C, D, E and weighted edges]

Solution:

Dijkstra's Algorithm steps:

1. Initialize distance of source vertex to 0 and all other vertices to infinity


2. Mark all vertices as unvisited
3. For each unvisited vertex:
a. Select the unvisited vertex with minimum distance
b. Mark it as visited
c. Update distances to adjacent unvisited vertices if a shorter path found
4. Repeat until all vertices are visited

For a sample graph (assuming S is connected to A with weight 4, A to B with weight 2, etc.):

Initial: dist[S]=0, dist[A]=inf, dist[B]=inf, dist[C]=inf, dist[D]=inf, dist[E]=inf Visited=[S], Update: dist[A]=4, dist[C]=2 Visited=[S,C], Update: dist[D]=5, dist[B]=7
Visited=[S,C,D], Update: dist[E]=7 Visited=[S,C,D,A], Update: dist[B]=6 Visited=[S,C,D,A,B], Update: dist[E]=9 Visited=[S,C,D,A,B,E]

Final shortest paths from S:

To A: 4
To B: 6
To C: 2
To D: 5
To E: 7

6. Hashing
Question: A hash function H is defined as H(key) = key % 7, with linear probing. Show the hash table after inserting the keys 37, 38, 72, 48, 98, 11, 66 into
a table indexed from 0 to 6. Count the total number of collisions.

Solution:

For each key, we calculate H(key) and place it in that position. If occupied, we move to the next available position.

H(37) = 37 % 7 = 2, place 37 at index 2 H(38) = 38 % 7 = 3, place 38 at index 3 H(72) = 72 % 7 = 2, collision at index 2, place 72 at index 3 (occupied),
place at index 4 H(48) = 48 % 7 = 6, place 48 at index 6 H(98) = 98 % 7 = 0, place 98 at index 0 H(11) = 11 % 7 = 4, collision at index 4, place 11 at index 5
H(66) = 66 % 7 = 3, collision at index 3, place 66 at index 4 (occupied), place at index 5 (occupied), place at index 6 (occupied), place at index 0 (occupied),
place at index 1

Final hash table: [98, 66, 37, 38, 72, 11, 48]

Total collisions: 3 (for 72, 11, and 66)

7. B-Trees
Question: Construct a B-tree of order 5 by inserting the following keys: 10, 20, 30, 40, 50, 60, 70, 80, 90

Solution:

For a B-tree of order 5:

Each node can have at most 4 keys and 5 children


Each node (except root) must have at least 2 keys

Initial B-tree: [10] After 20: [10, 20] After 30: [10, 20, 30] After 40: [10, 20, 30, 40] After 50: Tree splits into:

Root: [30]
Left node: [10, 20]
Right node: [40, 50]

After 60:

Root: [30]
Left node: [10, 20]
Right node: [40, 50, 60]

After 70:

Root: [30]
Left node: [10, 20]
Right node: [40, 50, 60, 70]

After 80: Right node splits:

Root: [30, 60]


Left node: [10, 20]
Middle node: [40, 50]
Right node: [70, 80]

After 90:

Root: [30, 60]


Left node: [10, 20]
Middle node: [40, 50]
Right node: [70, 80, 90]

8. AVL Trees
Question: Insert the following sequence of elements into an initially empty AVL tree: 71, 41, 91, 56, 60, 30, 40, 80, 50, 55

Solution:

AVL tree insertions with balancing after each insert:

1. Insert 71: [71]


2. Insert 41: [41, 71]
3. Insert 91: [41, 71, 91]
4. Insert 56: [41, 56, 71, 91] (rotation needed)
5. Insert 60: [41, 56, 60, 71, 91] (rotation needed)
6. Insert 30: [30, 41, 56, 60, 71, 91]
7. Insert 40: [30, 40, 41, 56, 60, 71, 91] (rotation needed)
8. Insert 80: [30, 40, 41, 56, 60, 71, 80, 91]
9. Insert 50: [30, 40, 41, 50, 56, 60, 71, 80, 91] (rotation needed)
10. Insert 55: [30, 40, 41, 50, 55, 56, 60, 71, 80, 91] (rotation needed)

Final AVL tree would have 56 as the root, with balanced subtrees.

9. Stack Applications
Question: Evaluate the following postfix expression using a stack: 2 3 9 * + 2 3 ^ - 6 2 / +

Solution:

Algorithm to evaluate postfix:

1. Scan expression from left to right


2. If operand, push onto stack
3. If operator, pop operands, apply operator, push result back

Evaluation steps:

Push 2: Stack = [2]


Push 3: Stack = [2, 3]
Push 9: Stack = [2, 3, 9]
Multiply: 3 * 9 = 27, Stack = [2, 27]
Add: 2 + 27 = 29, Stack = [29]
Push 2: Stack = [29, 2]
Push 3: Stack = [29, 2, 3]
Exponent: 2^3 = 8, Stack = [29, 8]
Subtract: 29 - 8 = 21, Stack = [21]
Push 6: Stack = [21, 6]
Push 2: Stack = [21, 6, 2]
Divide: 6 / 2 = 3, Stack = [21, 3]
Add: 21 + 3 = 24, Stack = [24]

Result = 24

10. Minimum Spanning Tree


Question: Apply Prim's algorithm to find the minimum spanning tree for the following graph. [Graph with vertices and weighted edges]

Solution:
Prim's Algorithm steps:

1. Start with any vertex and mark it as visited


2. Find the minimum weight edge connecting a visited vertex to an unvisited vertex
3. Add this edge to the MST and mark the new vertex as visited
4. Repeat until all vertices are visited

Assuming a graph with vertices A, B, C, D, E and weighted edges:

Starting at vertex A:

Choose edge A-B (weight 2)


Choose edge B-C (weight 3)
Choose edge C-E (weight 1)
Choose edge C-D (weight 4)

Minimum spanning tree edges: A-B, B-C, C-E, C-D Total weight: 2 + 3 + 1 + 4 = 10

You might also like