Data Structures Practice Questions
Data Structures Practice Questions
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].
Given:
For A[5][10][15]:
Solution:
Converting A+(B*C-(D/E^F)*G)*H:
Result: ABCDEF^/G-H*+
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.
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:
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:
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]
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]
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:
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 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:
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:
Evaluation steps:
Result = 24
Solution:
Prim's Algorithm steps:
Starting at vertex A:
Minimum spanning tree edges: A-B, B-C, C-E, C-D Total weight: 2 + 3 + 1 + 4 = 10