3 Mark Questions (DSA)
3 Mark Questions (DSA)
1. Explain an AVL tree, and what properties distinguish it from a regular binary search
tree (BST)?
An AVL tree is a self-balancing binary search tree where the difference in heights of the left
and right subtrees (called the balance factor) of any node is at most 1.
Key properties distinguishing it from BST:
● Rotations: AVL tree uses rotations (LL, RR, LR, RL) to maintain balance after insertions
or deletions.
● Time Complexity: O(log n) for insertion, deletion, and search due to strict balancing.
2. Explain the balancing of AVL trees after insertion or deletion of nodes, and why is this
balancing necessary?
After insertion or deletion, the balance factor of a node may become less than -1 or more than
1. To restore the AVL property, rotations are applied:
3. Explain the time complexity of common operations (insertion, deletion, and search) in
an AVL tree, and how does it compare to a regular binary search tree (BST)?
● AVL Tree:
○ Insertion: O(log n)
● Regular BST:
4. Explain the concept of a binary search tree (BST) and its key properties.
A Binary Search Tree is a binary tree in which each node has the following properties:
● The left subtree contains nodes with values less than the node’s value.
● The right subtree contains nodes with values greater than the node’s value.
5. Construct a binary search tree from the following list of numbers: [10, 5, 15, 3, 7, 12,
18].
Step-by-step Insertion:
● Insert 10 → root
● Insert 7 → right of 5
10
/ \
5 15
/\ / \
3 7 12 18
10
/ \
5 15
/\ / \
3 7 12 18
● Root = 10
● Types:
8. Describe path in a graph. Distinguish the difference between a simple path and a cycle
in a graph. Provide an example of each.
○ Example: A → B → C
○ Example: A → B → C → A
○
● Disconnected Graph: Not all pairs of vertices have paths between them.
Example:
A—B D—E
○
11. Distinguish between a B+ tree and a B-tree, and write advantages of both in terms of
searching and range queries.
Leaf Nodes Contains both data and keys Only leaf nodes store data
Advantages:
● B+ Tree: Excellent for range-based searches and sequential access due to linked leaf
nodes.
1. Search the appropriate leaf node for the new value.
5. If the root splits, a new root is created — increasing the tree height.
13. Write the difference between a directed graph (digraph) and an undirected graph.
Provide an example of each.
● Directed: A → B
● Undirected: A — B
Analysis:
Calculation:
● factorial(5)
= 5 × factorial(4)
= 5 × 4 × factorial(3)
= 5 × 4 × 3 × factorial(2)
= 5 × 4 × 3 × 2 × factorial(1)
= 5 × 4 × 3 × 2 × 1 × factorial(0)
= 5 × 4 × 3 × 2 × 1 × 1 = 120
Answer: 120
Calculation:
● F[2] = 1
● F[3] = 2
● F[4] = 3
● F[5] = 5
● F[6] = 8
● F[7] = 13
● F[8] = 21
● F[9] = 34
Answer: 34
● Index (K) = 5
Calculation:
Address = 2000 + 4 × 5 = 2020
Answer: 2020
Infix: A * B + C / D
Postfix conversion steps:
1. A * B → AB*
2. C / D → CD/
Answer: AB*CD/+
Index: 0 1 2 3 4 5 6 7
Value: 5 4 3 23 11 4 6 7
20. Perform 3 Pop operations first, then 4 Push operations: 22, 12, 5, 16
Initial Stack: Top = 7
After 3 Pops: Remove 7, 6, 4 → Top moves to index 4
Stack becomes:
[5, 4, 3, 23, 11] (Top = 4)
Push 22 → Top = 5
Push 12 → Top = 6
Push 5 → Top = 7
Push 16 → Top = 8
21. Deduce the number of elements inside a Queue when Front = 2 and Rear = 6.
Given:
Queue[8] = [20, 30, 25, 12, 15, 24, 32, 16]
Front = 2, Rear = 6
Formula:
Number of elements = Rear - Front + 1 = 6 - 2 + 1 = 5
Answer: 5 elements
10.POP → 20
● Time Complexity: The total amount of time taken by an algorithm to run, measured as a
function of the input size (n).
Answer:
24. Interpret the pseudo code to find the running time complexity:
● When i is even: i = i + 2
● When i is odd: i = i - 2
This can create an infinite loop for certain values of n, especially when i keeps oscillating.
Type Example
●
Self-Balancing trees adjust themselves after insertion/deletion.
Importance:
● Self-balancing ensures that the height of the tree remains logarithmic (O(log n)).
● This guarantees efficient performance for search, insertion, and deletion operations.
● Prevents degeneration of the tree into a linked list, which would degrade performance
to O(n).
● Helps maintain uniform distribution of nodes, improving traversal speed.
27. Classify nonlinear data structures into balanced and unbalanced categories.
Balanced Unbalanced
●
Balanced structures keep their height minimized automatically.
28. Explain the significance of balance in search trees and how it affects their efficiency.
● Unbalanced trees (e.g., skewed BSTs) may degrade to O(n) time complexity.
● Efficiency is directly proportional to the balance: the better balanced, the faster
operations.
29. Discuss suitability of B-Tree for scenarios like file systems and databases.
● Each node can store multiple keys, reducing the tree height.
● In a threaded binary tree, null pointers are replaced with pointers to inorder
predecessor or successor.
Example:
If a node’s right child is NULL, it may point to the inorder successor.
10
/ \
5 15
12
Advantages:
● Better Performance: Provide faster access, insertion, and deletion in specific scenarios.
● Examples:
32. Predict and construct AVL tree for the following data:
Data: 21, 26, 30, 9, 4, 14, 28, 18, 15, 10, 2, 3, 7
Step-by-step construction with rebalancing is complex for this format, but here’s the idea:
14
/ \
9 26
/ \ / \
4 10 21 28
/\ \ \ \
2 3 12 30 -
\
7
Note: Actual AVL tree may vary slightly depending on the rotation strategy but will always
maintain balance at each step.
33. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted into an initially empty BST.
Predict the inorder traversal sequence.
Step-by-step Insertion:
BST structure will look like:
7
/ \
5 8
/\ \
1 6 9
/\
0 3
/\
2 4
Answer: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
34. Express the application of preorder, postorder, level-order, or inorder tree traversals
to check whether the given binary tree is full or not.
● Application of traversal:
35. Consider the following data and identify which one is Preorder, Inorder, and
Postorder sequences.
● S1: N, M, P, O, Q
● S2: N, P, Q, O, M
● S3: M, N, O, P, Q
Let’s examine:
Answer:
● S1: Preorder
● S2: Postorder
● S3: Inorder
● A strongly connected graph is a directed graph where there is a path from every
vertex to every other vertex.
●
○ From A, we can reach B and C.
2. Circular Queue: Last position connects to the first to utilize space.
3. Priority Queue: Elements are dequeued based on priority, not order.
4. Double-Ended Queue (Deque): Insertion and deletion possible at both ends.
Example in pseudo-code:
if (Top == Max - 1)
print "Stack Overflow"
else
Top = Top + 1
Stack[Top] = item
40. Explain the concept of a pop algorithm in a stack.
Example in pseudo-code:
if (Top == -1)
print "Stack Underflow"
else
item = Stack[Top]
Top = Top - 1
Steps:
● Stack = []
Answer: 20
Infix: A + B * (C - D)
Steps:
● C - D → CD-
● B * (C - D) → B CD- *
● A + ... → A B CD- * +
Answer: A B C D - * +
1. Check if the queue is full (Rear == Max - 1 for linear queue).
○ Increment Rear
Pseudo-code:
if (Rear == Max - 1)
print "Overflow"
else {
if (Front == -1)
Front = 0;
Rear = Rear + 1;
Queue[Rear] = item;
○ Increment Front
Pseudo-code:
print "Underflow"
else {
item = Queue[Front];
Front = Front + 1;
45. Explain the conditions to check whether a circular queue is empty or full.
● Empty Condition:
Front == -1
Answer:
● Queue is full when the next position of Rear is Front (after wrapping around).
46. Explain what a circular queue is and how it differs from a regular queue.
● A circular queue is a linear data structure in which the last position is connected
back to the first, forming a circle.
● It uses modulo arithmetic to wrap around the index when inserting or removing elements.
47. Explain the key advantages of using a linked list to implement a queue over an array.
Dynamic size (no overflow unless memory full) Fixed size (can overflow)
Key Advantages:
A sparse matrix is a matrix with more zero elements than non-zero elements.
Example:
0005
0800
0000
3000
Storage: Instead of storing all elements, store only non-zero elements with their positions to
save memory.
Triplet representation:
0 3 5
1 1 8
3 0 3
● Applications:
○ Graph algorithms
○ Image compression
○ Scientific computing
51. Evaluate the following expression from Infix to Postfix and Prefix: A + B * (C - D)
Given Infix: A + B * (C - D)
Postfix Conversion:
1. C - D → CD-
2. B * (C - D) → B CD- *
✅ Postfix: A B C D - * +
Prefix Conversion:
1. C - D → - C D
2. B * ... → * B - C D
3. A + ... → + A * B - C D
✅ Prefix: + A * B - C D
52. Explain insertion, deletion, traversal operations in data structure.
53. Explain the need for a double-ended queue (deque) and its usage scenarios.
Use Cases:
● Task scheduling
55. Explain insertion, deletion, traversal operation in data structure (same as Q52 –
possibly repeated)
Different data structures may implement these in specific ways (linked list, arrays, trees, etc.)