Data Structures
Data Structures
Complete Preparation
Your exact syllabus
Section 4 — Data Structures
1. Programming in C
2. Recursion
3. Arrays
4. Stacks
5. Queues
6. Linked Lists
7. Trees
8. Binary Search Trees
9. Binary Heaps
10. Graphs
Section 5 — Algorithms
11. Searching
12. Sorting
13. Hashing
14. Asymptotic worst-case time and space complexity
15. Greedy
16. Dynamic Programming
17. Divide and Conquer
18. Graph Traversals
19. Minimum Spanning Trees
20. Shortest Paths
Must know
int x = 10;
int *p = &x;
Conceptually:
x
┌──────┐
│ 10 │
└──────┘
↑
│ address
┌──────┐
│ p │
└──────┘
The 2020 paper included a C output question, showing that basic C programming can appear
directly.
2. Recursion
A function calling itself is recursion.
fact(n) = n × fact(n-1)
Example
int fact(int n)
{
if(n == 0)
return 1;
return n * fact(n-1);
}
For fact(4):
fact(4)
↓
4 × fact(3)
↓
3 × fact(2)
↓
2 × fact(1)
↓
1 × fact(0)
↓
1
Result:
[
4\times3\times2\times1=24
]
Critical concept
1. Base case
2. Recursive case
Recursion uses:
Stack
The 2020 CIL paper directly asked which data structure the system uses to implement
recursion, with Stack as the answer.
3. Arrays
An array stores elements in contiguous memory.
Advantages
Random access
A[i] → O(1)
Simple implementation
Disadvantages
Complexity
Operation Complexity
Access O(1)
Search O(n)
Insert at beginning O(n)
Delete at beginning O(n)
Important formula
N=UB-LB+1
4. Stack
Stack follows:
TOP
↓
┌───┐
│ 30│
├───┤
│ 20│
├───┤
│ 10│
└───┘
Operations:
push
pop
peek/top
Complexity
Applications
Recursion
Function calls
DFS
Parentheses matching
Expression conversion
Undo operation
Front Rear
↓ ↓
┌────┬────┬────┬────┐
│ 10 │ 20 │ 30 │ 40 │
└────┴────┴────┴────┘
↑ ↑
delete insert
Operations:
Enqueue
Dequeue
Front/Peek
Applications
BFS
CPU scheduling
Printer queue
Buffers
6. Linked List
A linked list consists of nodes.
Data + Pointer
Types
7. Trees
A tree is a hierarchical data structure.
A
/ \
B C
/ \ \
D E F
Important terms:
Root
Parent
Child
Leaf
Degree
Level
Height
Subtree
Binary tree
A
/ \
B C
/ \
D E
Tree traversals
Preorder
Inorder
Postorder
Level order
Level by level
The 2020 paper gives inorder + preorder and asks the candidate to derive the postorder
traversal.
Example:
50
/ \
30 70
/ \ / \
20 40 60 80
20 30 40 50 60 70 80
Searching
The 2020 paper directly asks the time required to search an element in a BST and gives O(log
n) as the intended answer.
CIL relevance: 10/10
9. Binary Heap
A heap is a complete binary tree.
Max heap
Parent ≥ children.
90
/ \
70 80
/ \ /
40 50 60
Min heap
Parent ≤ children.
10
/ \
20 30
/ \ /
40 50 60
Important
Right(i)=2i+2
Parent(i)=\lfloor(i-1)/2\rfloor
Complexity
Operation Complexity
Get min/max O(1)
Insert O(log n)
Delete root O(log n)
Build heap O(n)
where:
V = vertices
E = edges
Example:
A ─── B
│ │
│ │
C ─── D
Types
Directed
Undirected
Weighted
Unweighted
Connected
Disconnected
Cyclic
Acyclic
Representations
Adjacency Matrix
Adjacency List
Space: O(V+E)
PART B — ALGORITHMS
11. Searching
Linear Search
Check elements one by one.
10 20 30 40 50
↑
target
Binary Search
Requires a sorted array.
10 20 30 40 50 60 70
↑
middle
12. Sorting
You must know at least:
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
Radix Sort
Stable sorting
Collision
Collision!
Collision-resolution methods
Big-O Big-Omega
Big-Theta
For CIL, worst-case Big-O is especially important because your syllabus explicitly says:
Common order
O(1)
↓
O(log n)
↓
O(n)
↓
O(n log n)
↓
O(n²)
↓
O(n³)
↓
O(2ⁿ)
↓
O(n!)
Example
for(i=0; i<n; i++)
printf("%d", i);
Complexity: O(n)
Nested loop:
for(i=0;i<n;i++)
for(j=0;j<n;j++)
Complexity: O(n^2)
Space complexity
For:
fact(n)
recursive depth = n.
Divide
↓
Solve
↓
Combine
Example:
Merge Sort
[8 3 2 9]
/ \
[8 3] [2 9]
/ \ / \
8 3 2 9
\ / \ /
[3 8] [2 9]
\ /
[2 3 8 9]
Recurrence: T(n)=2T(n/2)+O(n)
Therefore: T(n)=O(n\log n)
Examples
Merge Sort
Binary Search
Quick Sort
Strassen-type algorithms
Current state
↓
Best immediate choice
↓
Next state
↓
Best immediate choice
Examples:
Kruskal
Prim
Dijkstra under appropriate conditions
Huffman coding
Fractional Knapsack
Important trap
For example:
1. Overlapping subproblems
2. Optimal substructure
Two approaches:
Memoization
Top-down.
Recursive
↓
Store answers
↓
Reuse
Tabulation
Bottom-up.
Small problems
↓
Build larger problems
Example: Fibonacci
Classic DP problems
0/1 Knapsack
Matrix Chain Multiplication
LCS
Floyd-Warshall
Coin Change
Uses: DFS
Queue Uses:
A Stack / recursion
/ \
B C
/ \ Possible DFS:
D E
A → B → D → E → C
The 2020 paper directly tests data structures associated with recursion/BFS/DFS, making this
a recurring application-style pattern.
Kruskal Prim
Sort edges Starts from a vertex and grows one tree.
↓
Pick smallest
Start vertex
↓
↓
Reject if cycle
minimum edge leaving tree
↓
↓
Continue
add vertex
↓
Uses the Disjoint Set / Union-Find repeat
concept.
Works with:
Basic idea:
Bellman-Ford Floyd-Warshall
Can handle: All-pairs shortest path.
Practical 1 — Recursion
Find:
[
T(n)=T(n-1)+1
]
Therefore:
[
T(n)=O(n)
]
10 20 30 40 50 60 70
Search 60.
middle = 40
60 > 40
↓
50
↓
60
[
O(\log n)
]
Practical 3 — BST
Insert:
Result:
50
/ \
30 70
/ \ / \
20 40 60 80
Inorder:
20 30 40 50 60 70 80
A
/ \
B C
/ \
D E
Preorder:
A B D E C
Inorder:
D B E A C
Postorder:
D E B C A
Practical 5 — Heap
Insert:
10, 20, 5, 30
30
/ \
20 5
/
10
Practical 6 — Complexity
for(i=0;i<n;i++)
for(j=0;j<i;j++)
printf("*");
Number of operations:
[
0+1+2+\cdots+(n-1)
]
[
=\frac{n(n-1)}2
]
Therefore:
[
\boxed{O(n^2)}
]
Practical 7 — Merge Sort
Recurrence:
[
T(n)=2T(n/2)+n
]
Master theorem:
[
a=2,\quad b=2,\quad f(n)=n
]
[
n^{\log_ba}=n
]
Therefore:
[
\boxed{T(n)=O(n\log n)}
]
Practical 8 — Hashing
Table size = 10.
Keys:
23, 33
Using:
[
h(k)=k\bmod10
]
Both:
[
23\bmod10=3
]
[
33\bmod10=3
]
A---1---B
| |
4 2
| |
C---3---D
Edges:
AB = 1
BD = 2
CD = 3
AC = 4
Kruskal chooses:
AB → BD → CD
Total:
[
1+2+3=6
]
Practical 10 — Dijkstra
For each vertex maintain:
distance
visited/unvisited
Repeatedly:
Answer: Stack.
Level 2 — Application
Example:
Answer: Queue.
Example:
The actual 2020 paper contains exactly this type of Level-3 tree question.
Q1. [C Programming]
A. *
B. &
C. ->
D. %
Answer: B. &
Explanation: The & operator gives the memory address of a variable, while * dereferences a
pointer.
Q2. [Recursion]
A. Queue
B. Stack
C. Heap
D. Graph
Answer: B. Stack
Explanation: Every recursive call creates an activation record that is stored on the call stack.
Q3. [Recursion]
[
T(n)=T(n-1)+1
]
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: The recurrence performs approximately n recursive levels, each doing constant
work.
Q4. [Array]
A. O(n)
B. O(log n)
C. O(1)
D. O(n log n)
Answer: C. O(1)
Explanation: Array elements have direct address calculation, so any index can normally be
accessed in constant time.
Q5. [Stack]
A. FIFO
B. LIFO
C. Random access
D. Priority-based
Answer: B. LIFO
PYQ connection: CIL 2020 directly asked FILO and expected Stack.
Q6. [Queue]
A. Stack
B. Queue
C. Heap
D. Array only
Answer: B. Queue
Which data structure is particularly suitable for representing a polynomial whose terms may
be inserted dynamically?
A. Stack
B. Queue
C. Linked List
D. Heap
Explanation: Each polynomial term can be stored in a node containing coefficient, exponent
and a link to the next term.
PYQ connection: CIL 2020 Q27 directly asked polynomial addition and linked lists.
What is the typical worst-case time complexity of searching an unsorted singly linked list?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: C. O(n)
Q9. [Tree]
A. Preorder
B. Postorder
C. Inorder
D. Level order
Answer: C. Inorder
Explanation: BST maintains smaller keys on the left and larger keys on the right; therefore
inorder produces ascending order.
Q10. [Tree]
Answer: B. Preorder
Explanation: Preorder always processes the root before its left and right subtrees.
Q11. [Tree]
A binary tree can have at most how many children per node?
A. 1
B. 2
C. 3
D. Unlimited
Answer: B. 2
A
/ \
B C
/ \
D E
A. A B D E C
B. D E B C A
C. D B E A C
D. A C B E D
Answer: B. D E B C A
Q13. [BST]
A. Right subtree
B. Left subtree
C. Parent node
D. Any random position
Explanation: The BST ordering property is Left < Root < Right.
Q14. [BST]
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: If the BST becomes skewed like a linked list, searching may require visiting all
n nodes.
Q15. [BST]
A. O(n²)
B. O(n)
C. O(log n)
D. O(2ⁿ)
Answer: C. O(log n)
Explanation: A balanced BST reduces the search space approximately by half at each level.
CIL relevance: ⭐⭐⭐⭐⭐ 10/10
Q16. [Heap]
Q17. [Heap]
A. O(1) always
B. O(log n)
C. O(n²)
D. O(2ⁿ)
Answer: B. O(log n)
Explanation: After insertion at the bottom, the element may move upward through at most
the height of the heap.
Q18. [Graph]
A. G = (V,E)
B. G = (A,B)
C. G = (N,L) only
D. G = (V,T)
Answer: A. G = (V,E)
Q19. [Graph]
A. O(V)
B. O(E)
C. O(V²)
D. O(V+E)
Answer: C. O(V²)
A. Stack
B. Queue
C. Heap
D. Hash table
Answer: B. Queue
A. Queue only
B. Stack or recursion
C. Heap only
D. Hash table only
Explanation: Recursion implicitly uses a call stack; iterative DFS explicitly uses a stack.
A. Random
B. Sorted
C. Hashed
D. Stored in a tree
Answer: B. Sorted
Explanation: Binary search decides whether to move left or right based on comparison with
the middle element.
Q23. [Searching]
A. O(n)
B. O(n²)
C. O(log n)
D. O(1)
Answer: C. O(log n)
Q24. [Sorting]
A. Merge Sort
B. Bubble Sort
C. Selection Sort
D. Naive Insertion Sort
Explanation: Merge Sort divides the input recursively and merges in linear time at every
level.
Q25. [Sorting]
Explanation: Stability concerns equal-key records, not the algorithm's time or memory
complexity.
PYQ connection: This is almost exactly the concept tested in CIL 2020 Q57.
Q26. [Hashing]
A. A key is deleted
B. Two keys map to the same hash location
C. Table becomes empty
D. Search succeeds
Explanation: Different keys can produce the same hash value, requiring collision resolution.
Q27. [Hashing]
A. Chaining
B. Recursion
C. DFS
D. Merge
Answer: A. Chaining
Explanation: Chaining stores multiple colliding elements in a linked structure associated
with the same hash bucket.
Q28. [Complexity]
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: The loop executes n times and each iteration performs constant work.
Q29. [Complexity]
What is the complexity of two independent nested loops, each running n times?
A. O(n)
B. O(log n)
C. O(n²)
D. O(2n)
Answer: C. O(n²)
Explanation: The inner loop executes n times for each of n outer iterations.
Q30. [Complexity]
A. n
B. n log n
C. n²
D. 2ⁿ
Answer: D. 2ⁿ
A. Merge Sort
B. BFS
C. Linear Search
D. Hashing
Explanation: Merge Sort divides the array, recursively solves both parts and combines the
sorted results.
[
T(n)=2T(n/2)+O(n)
]
A. O(n)
B. O(log n)
C. O(n log n)
D. O(n²)
Explanation: There are O(log n) levels, with O(n) total merging work at each level.
Q33. [Greedy]
Which technique chooses the locally best available option at each step?
A. Dynamic Programming
B. Greedy
C. Divide and Conquer
D. Backtracking
Answer: B. Greedy
Explanation: Greedy algorithms make locally optimal choices with the hope of obtaining a
globally optimal solution.
Q34. [Greedy]
A. Fractional Knapsack
B. 0/1 Knapsack
C. LCS
D. Matrix Chain Multiplication
A. Only recursion
B. Overlapping subproblems and optimal substructure
C. No repeated computation
D. Only sorted input
Explanation: DP stores solutions to repeated subproblems and uses them to construct larger
solutions.
Q37. [MST]
A. V edges
B. V+1 edges
C. V−1 edges
D. 2V edges
Explanation: A spanning tree is acyclic and connects all V vertices using exactly V−1 edges.
Q38. [MST]
A. Prim
B. Kruskal
C. Dijkstra
D. BFS
Answer: B. Kruskal
Explanation: Kruskal sorts the edges and repeatedly selects the smallest edge that does not
create a cycle.
Q39. [MST]
Which data structure is commonly used to detect cycles efficiently in Kruskal's algorithm?
A. Stack
B. Queue
C. Disjoint Set Union
D. Binary Search
Explanation: Union-Find efficiently determines whether two vertices already belong to the
same connected component.
Dijkstra's algorithm is designed for shortest paths when edge weights are:
A. Always negative
B. Non-negative
C. Always zero
D. Arbitrary including negative cycles
Answer: B. Non-negative
Explanation: Dijkstra's greedy selection is not valid when negative-weight edges can
invalidate previously finalized distances.
A. BFS
B. Binary Search
C. Bellman-Ford
D. Prim
Answer: C. Bellman-Ford
Explanation: Bellman-Ford repeatedly relaxes edges and can detect further improvement
after V−1 rounds.
Answer: C. Floyd-Warshall
Explanation: Floyd-Warshall systematically allows intermediate vertices and has O(V³) time
complexity.
Q43. [Graph]
A. Complete graph
B. Acyclic graph
C. Weighted graph
D. Multigraph
Q44. [Graph]
A. Any graph
B. Directed acyclic graph
C. Complete undirected graph
D. Cyclic directed graph
Explanation: A directed graph must be acyclic for a valid topological ordering to exist.
Q45. [Complexity]
Answer: C. O(n)
Explanation: Each recursive call creates a stack frame, and n simultaneously active calls
require O(n) stack space.
Which sorting algorithm has O(n²) worst-case complexity but O(n log n) average complexity
under common implementations?
A. Merge Sort
B. Quick Sort
C. Heap Sort
D. Counting Sort
Explanation: Poor pivot choices can produce highly unbalanced partitions, giving O(n²).
Q47. [Heap]
What is the complexity of building a binary heap from n arbitrary elements using the standard
bottom-up heap construction?
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: Bottom-up heap construction performs less work on nodes farther from the
leaves, resulting in linear total time.
Inorder: D B E A C
Preorder: A B D E C
A. D E B C A
B. D B E C A
C. A B D E C
D. C E D B A
Answer: A. D E B C A
Explanation: Preorder identifies A as root; inorder divides the left subtree into D-B-E and
right subtree into C.
PYQ similarity: Very high — this is deliberately modeled on the actual CIL 2020 traversal
question.
A. BFS — Stack
B. DFS — Queue
C. Kruskal — MST
D. Dijkstra — Sorting
A balanced BST contains n elements. What is the asymptotic worst-case time complexity of
searching for an element?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: B. O(log n)
Explanation: In a balanced BST, the height is O(log n), so a search follows at most
logarithmically many nodes.
PYQ connection: This directly matches the concept tested in CIL 2020 Q63.
That is why memorizing definitions alone will not be enough. The 2020 paper already
demonstrates this through the traversal problem, BST complexity, stable sorting, linked-list
application and recursion questions.
One important C-language caution: the 2020 paper contains a printf("%d %d %d", i++,
i, ++i) question. That expression has undefined behavior in standard C, so for your
preparation I would treat it as a historical exam question/pattern, not as a reliable rule for
predicting its output.
This gives us the theory + practical foundation + 50-question CIL-style baseline for the
entire DSA/Algorithms syllabus you supplied.
A. 1006
B. 1020
C. 1024
D. 1040
Answer: C. 1024
Q52. [Array]
What is the worst-case time complexity of inserting an element at the beginning of an array
containing n elements?
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: Existing elements generally need to be shifted one position to make space.
A. Queue
B. Stack
C. Heap
D. Graph
Answer: B. Stack
Explanation: Opening brackets are pushed and matched with closing brackets in reverse
order, which requires LIFO behavior.
A. Stack
B. Queue
C. Graph
D. Hash table
Answer: A. Stack
Explanation: Operands are pushed onto the stack, and operators pop the required operands
and push the result.
A. O(n)
B. O(log n)
C. O(1)
D. O(n²)
Answer: C. O(1)
Explanation: Push normally modifies the top position and stores the new element, requiring
constant time.
Q56. [Queue]
Which operation removes an element from a standard queue?
A. Push
B. Pop
C. Enqueue
D. Dequeue
Answer: D. Dequeue
Explanation: Enqueue inserts at the rear, while dequeue removes from the front.
A. It eliminates FIFO
B. It allows reuse of previously freed positions
C. It always sorts elements
D. It uses a tree
Explanation: Circular arrangement allows the rear to wrap around and use available space at
the beginning.
Explanation: Linked lists avoid shifting large numbers of elements when insertion/deletion
positions are already known.
TREES + BST
Q61. [Binary Tree]
A binary tree has n nodes. What is the maximum number of children any individual node can
have?
A. 1
B. 2
C. n
D. n−1
Answer: B. 2
[
Left \rightarrow Root \rightarrow Right
]
A. Preorder
B. Postorder
C. Inorder
D. Level order
Answer: C. Inorder
Explanation: Inorder processes the left subtree, root, and then right subtree.
CIL relevance: ⭐⭐⭐⭐⭐ 9/10
10
/ \
5 15
/ \
2 7
A. 2, 5, 7, 10, 15
B. 10, 5, 2, 7, 15
C. 2, 7, 5, 15, 10
D. 10, 15, 5, 7, 2
Answer: B. 10, 5, 2, 7, 15
PYQ similarity: Very high. The supplied CIL paper uses traversal information as a
problem-solving question rather than merely asking definitions.
10
/ \
5 15
/ \
2 7
A. 10, 5, 2, 7, 15
B. 2, 7, 5, 15, 10
C. 2, 5, 7, 10, 15
D. 15, 7, 2, 5, 10
Answer: B. 2, 7, 5, 15, 10
Answer: C. Left subtree values are smaller and right subtree values are greater
[
50,\ 30,\ 70,\ 20,\ 40
]
A.
50
/ \
30 70
/ \
20 40
B.
50
/ \
70 30
C.
30
/ \
20 50
\
70
D.
70
/
50
Answer: A
Explanation: Each key is inserted according to the BST rule: smaller values go left and
larger values go right.
Q67. [BST]
What does inorder traversal of a valid BST produce?
A. Random ordering
B. Descending order always
C. Sorted order
D. Level order
Explanation: Inorder visits all smaller values before the root and all larger values after it.
A. O(1)
B. O(log n)
C. O(n)
D. O(log log n)
Answer: C. O(n)
Explanation: A skewed BST can have height n−1, making it behave like a linked list.
Explanation: Ordinary BSTs do not automatically maintain balance; insertion order can
produce a highly skewed tree.
Q70. [Tree]
For a tree containing n nodes, how many edges does it contain?
A. n
B. n+1
C. n−1
D. 2n
Answer: C. n−1
Explanation: Every connected tree with n vertices has exactly n−1 edges.
HEAPS
Q71. [Heap]
A binary heap must be:
Explanation: Completeness ensures the heap can efficiently be represented using an array.
A. Minimum element
B. Maximum element
C. Median element
D. Random element
Explanation: The max-heap property requires every parent to be greater than or equal to its
children.
A. Maximum
B. Minimum
C. Median
D. Last inserted
Answer: B. Minimum
Explanation: Every parent is less than or equal to its children in a min heap.
A. 2i
B. 2i+1
C. 2i+2
D. i/2
Answer: B. 2i+1
Explanation: In a 0-based binary heap, left and right children are at 2i+1 and 2i+2.
A. 2i
B. 2i+1
C. 2i+2
D. i−1
Answer: C. 2i+2
Explanation: The standard 0-based heap mapping gives right child = 2i+2.
A. O(1)
B. O(log n)
C. O(n²)
D. O(2ⁿ)
Answer: B. O(log n)
Explanation: Removing the root requires restoring the heap property, which may take O(log
n).
Q77. [Heap]
Which algorithm can be implemented using a heap?
A. Heap Sort
B. Binary Search
C. BFS only
D. DFS only
Explanation: Heap Sort repeatedly extracts the maximum or minimum from a heap.
SEARCHING + SORTING
Q78. [Searching]
Linear search on an unsorted array has worst-case complexity:
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: C. O(n)
Explanation: The target may be at the last position or absent, requiring examination of all
elements.
A. 4
B. 6
C. 8
D. 32
Answer: B. 6
Q80. [Sorting]
Which sorting algorithm repeatedly selects the smallest remaining element and places it at the
next position?
A. Merge Sort
B. Selection Sort
C. Quick Sort
D. Heap Sort
Explanation: Selection Sort selects the minimum from the unsorted portion during each
pass.
CIL relevance: ⭐⭐⭐⭐☆ 8/10
Q81. [Sorting]
Which sorting algorithm is generally efficient when the array is already nearly sorted?
A. Insertion Sort
B. Selection Sort
C. Heap Sort
D. Naive Quick Sort
Explanation: Insertion Sort can approach O(n) when only a small number of shifts are
necessary.
Q82. [Sorting]
Which algorithm guarantees O(n log n) worst-case time among the following?
A. Quick Sort
B. Merge Sort
C. Bubble Sort
D. Insertion Sort
Explanation: Merge Sort always divides into logarithmically many levels and performs O(n)
merging per level.
Q83. [Sorting]
Which of the following is generally not stable in its standard in-place form?
A. Insertion Sort
B. Bubble Sort
C. Selection Sort
D. Merge Sort
PYQ similarity: Stability is directly represented in the supplied CIL 2020 paper.
A. O(1)
B. O(log n)
C. O(n log n)
D. O(n²)
Answer: D. O(n²)
Explanation: If each partition produces sizes 0 and n−1, recursion becomes linear in depth.
Explanation: Merge Sort recursively divides the input and then combines sorted halves.
HASHING
Q86. [Hashing]
Using:
[
h(k)=k\bmod10
]
A. 4
B. 7
C. 10
D. 47
Answer: B. 7
Explanation: (47\bmod10=7).
A. 21 and 32
B. 25 and 35
C. 14 and 25
D. 17 and 28
Answer: B. 25 and 35
Q88. [Hashing]
Which method stores colliding elements in a list associated with a hash bucket?
A. Linear probing
B. Chaining
C. Binary search
D. DFS
Answer: B. Chaining
Explanation: Separate chaining maintains a collection, commonly a linked list, for elements
having the same hash index.
A. O(n)
B. O(log n)
C. O(n²)
D. O(2ⁿ)
Answer: C. O(n²)
Explanation: The inner loop executes n times for each of n outer iterations.
Q90. [Complexity]
What is the complexity of:
A. O(n)
B. O(log n)
C. O(n²)
D. O(2ⁿ)
Answer: B. O(log n)
Q91. [Complexity]
Which is asymptotically smaller?
A. O(n²)
B. O(n log n)
C. O(n³)
D. O(2ⁿ)
Explanation: For sufficiently large n, (n\log n) grows slower than (n^2), (n^3), and (2^n).
Q92. [Recurrence]
Consider:
[
T(n)=2T(n/2)+n
]
A. O(n)
B. O(log n)
C. O(n log n)
D. O(n²)
Explanation: This is the standard Merge Sort recurrence and has (\log n) levels with O(n)
work per level.
Q93. [Recurrence]
Consider:
[
T(n)=T(n/2)+1
]
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: B. O(log n)
Explanation: The input size is halved at every recursive step.
Explanation: The problem is divided into smaller instances, solved recursively, and their
results are combined.
A. Greedy
B. Dynamic Programming
C. Divide and Conquer
D. Backtracking
Explanation: Binary Search repeatedly divides the search interval into two parts and
continues with the relevant half.
GREEDY + DP
Q96. [Greedy]
Which problem is not generally solved optimally by simply choosing the item with the
highest value/weight ratio?
A. Fractional Knapsack
B. 0/1 Knapsack
C. Both
D. Neither
Explanation: The indivisible nature of 0/1 Knapsack can make a simple greedy choice
suboptimal; DP is commonly used.
A. Memoization
B. Hash collision
C. Traversal
D. Partitioning
Answer: A. Memoization
A. Prim
B. Kruskal
C. Dijkstra
D. Both Prim and Kruskal
Answer: C. Dijkstra
Explanation: Prim and Kruskal construct minimum spanning trees; Dijkstra solves a
shortest-path problem.
A──1──B
│ │
4 2
│ │
C──3──D
A. AC, CD, BD
B. AB, BD, CD
C. AB, AC, BD
D. AC, AB, CD
Explanation: Kruskal considers weights 1, 2, 3, 4; AB, BD and CD connect all four vertices
without forming a cycle.
Q51–60
↓
Basic + implementation/application
↓
Q61–77
↓
Trees + BST + Heap
↓
Q78–88
↓
Searching + Sorting + Hashing
↓
Q89–93
↓
Complexity + Recurrences
↓
Q94–100
↓
Algorithm design + MST
And Q63, Q64, Q67, Q68, Q83, Q92 and Q100 are particularly important because they
move beyond pure definition-based questions into the type of reasoning that the supplied CIL
papers demonstrate. The supplied 2020 paper specifically contains traversal reconstruction,
stable sorting, and BST-complexity patterns.
A. Stack
B. Queue
C. Heap
D. Hash table
Answer: B. Queue
Explanation: BFS explores vertices level by level, so a FIFO queue naturally maintains the
order of exploration.
CIL relevance: ⭐⭐⭐⭐⭐ 10/10
A. Queue
B. Stack
C. Priority Queue
D. Hash table
Answer: B. Stack
Explanation: DFS goes as deep as possible before backtracking; this is implemented using a
stack or recursion.
Explanation: BFS explores vertices by increasing distance from the source, so the first
discovered path uses the minimum number of edges.
A
/ \
B C
/ \ \
D E F
Answer: A. A, B, C, D, E, F
Explanation: BFS processes all vertices at one level before moving to the next level.
A. A, B, D, E, C, F
B. A, B, C, D, E, F
C. D, B, E, A, C, F
D. A, C, F, B, D, E
Answer: A. A, B, D, E, C, F
Explanation: DFS completely explores the B subtree before returning to A and exploring C.
A. Adjacency matrix
B. Adjacency list
C. Complete matrix
D. 2D heap
A. O(V)
B. O(E)
C. O(V log V)
D. O(V²)
Answer: D. O(V²)
Explanation: The matrix contains one entry for every possible ordered pair of vertices.
A. O(V²)
B. O(V+E)
C. O(E²)
D. O(log V)
Answer: B. O(V+E)
Explanation: One structure is maintained for each vertex and entries are maintained for the
graph's edges.
A. V edges
B. V+1 edges
C. V−1 edges
D. V² edges
Explanation: Every spanning tree of a connected graph contains exactly one fewer edge than
its number of vertices.
Answer: B. It must connect all vertices with minimum total edge weight
Explanation: An MST connects every vertex without cycles while minimizing the sum of
selected edge weights.
Q111. [Kruskal]
Kruskal's algorithm primarily selects edges according to:
A. Number of vertices
B. Increasing edge weight
C. Decreasing vertex degree
D. BFS order
Explanation: Kruskal sorts edges by weight and repeatedly chooses the smallest edge that
does not create a cycle.
Q112. [Prim]
Prim's algorithm grows an MST by:
Explanation: Prim repeatedly chooses the minimum-weight edge connecting the current tree
to an unvisited vertex.
CIL relevance: ⭐⭐⭐⭐⭐ 10/10
Edge Weight
A-B 1
B-C 2
A-C 3
C-D 4
B-D 5
A. 5
B. 6
C. 7
D. 10
Answer: B. 6
Explanation: Kruskal chooses AB(1), BC(2), skips AC(3) because it creates a cycle, then
chooses CD(4): total = 1+2+4 = 7.
A. Always impossible
B. Unique
C. Always cyclic
D. Always disconnected
Answer: B. Unique
Explanation: Distinct edge weights eliminate ties in the MST selection process, giving a
unique MST.
Explanation: Dijkstra computes shortest paths from one source when edge weights satisfy its
non-negative-weight requirement.
A. Positive-weight edges
B. Zero-weight edges
C. Negative-weight edges
D. Undirected edges
Explanation: Dijkstra's greedy choice can become incorrect in the presence of negative edge
weights.
A. Dijkstra only
B. Bellman-Ford
C. BFS only
D. Prim
Answer: B. Bellman-Ford
Explanation: Bellman-Ford can handle negative edge weights and can also detect reachable
negative-weight cycles.
A. Floyd-Warshall
B. Binary Search
C. Prim
D. DFS
Answer: A. Floyd-Warshall
A --2--> B
A --5--> C
B --1--> C
A. 1
B. 2
C. 3
D. 5
Answer: C. 3
Explanation: Directly A→C costs 5, but A→B→C costs 2+1=3, which is shorter.
Answer: C
Topological Sorting
Q121. [DAG]
Topological sorting is defined for:
Explanation: A topological ordering exists only for a directed graph with no directed cycles.
A. Merge Sort
B. Quick Sort
C. Heap Sort
D. Counting Sort
Explanation: Poor pivot choices can create highly unbalanced partitions, producing O(n²)
worst-case behavior.
Q124. [Sorting]
Which sorting algorithm guarantees O(n log n) worst-case time and uses O(1) auxiliary array
space in its standard heap-based implementation?
A. Merge Sort
B. Heap Sort
C. Quick Sort
D. Bubble Sort
Explanation: Heap Sort maintains a heap and repeatedly extracts the extreme element; its
worst-case time is O(n log n).
(A, 5)
(B, 3)
(C, 5)
After a stable sort by the second field, which ordering must preserve the relative order of A
and C?
A. C, A, B
B. B, A, C
C. A, C, B
D. B, C, A
Answer: B and C?
Let's examine carefully: Sorting by the second field requires 3 before 5, and stability requires
A before C.
Therefore:
Answer: B. B, A, C
Explanation: B has key 3, while A and C both have key 5; stability preserves A before C.
Q126. [Sorting]
Which sorting algorithm is based on repeatedly merging sorted subarrays?
A. Selection Sort
B. Merge Sort
C. Bubble Sort
D. Heap Sort
Explanation: Merge Sort recursively creates sorted subarrays and combines them through
merging.
A. Insertion Sort
B. Selection Sort
C. Heap Sort
D. Merge Sort
Explanation: With an already sorted array, insertion sort performs very little shifting and can
run in linear time.
CIL relevance: ⭐⭐⭐⭐⭐ 9/10
Explanation: Open addressing resolves collisions by searching for another empty slot within
the table itself.
Explanation: Consecutive occupied positions form clusters, increasing the number of probes
required.
Q130. [Hashing]
If the load factor of a hash table becomes very high, generally:
Explanation: As more positions become occupied, collisions and probe lengths generally
increase.
Complexity — CIL-Level
Q131. [Complexity]
What is the complexity?
A. O(n)
B. O(log n)
C. O(n²)
D. O(n³)
Answer: C. O(n²)
Q132. [Complexity]
What is the complexity?
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n²)
Explanation: The outer loop executes O(log n) times and the inner loop executes O(n) times.
A. log n
B. n
C. n log n
D. 2ⁿ
Answer: D. 2ⁿ
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: A. O(1)
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: C. O(n)
Explanation: Each active recursive call occupies a stack frame, producing O(n) stack usage.
CIL relevance: ⭐⭐⭐⭐⭐ 10/10
Greedy Algorithms
Q136. [Greedy]
A greedy algorithm makes decisions by:
Explanation: Greedy algorithms build a solution through locally optimal choices, hoping
they lead to a globally optimal solution.
Explanation: Since fractions can be taken, maximizing value per unit weight produces an
optimal greedy solution.
Q138. [Greedy]
Which problem is classically solved optimally using a greedy strategy?
A. Fractional Knapsack
B. 0/1 Knapsack using simple value/weight ratio
C. General longest common subsequence
D. Matrix-chain multiplication
Answer: A. Fractional Knapsack
Explanation: Fractional Knapsack has the greedy-choice property because items can be
divided.
Dynamic Programming
Q139. [DP]
Dynamic Programming is particularly useful when a problem has:
A. It uses arrays
B. It repeatedly computes the same subproblems
C. It uses sorting
D. It uses hashing
Explanation: For example, computing F(n) repeatedly recomputes values such as F(n−2)
and F(n−3).
Q141. [DP]
Memoization is generally:
A. Bottom-up
B. Top-down
C. Non-recursive only
D. A graph traversal
Answer: B. Top-down
Explanation: Memoization begins with the original problem and recursively solves
subproblems while caching their results.
Q142. [DP]
Tabulation is generally:
A. Top-down recursion
B. Bottom-up computation
C. Randomized search
D. Hash collision resolution
Explanation: Tabulation fills a table starting from smaller base cases and progressively
computes larger subproblems.
A. BFS
B. DFS
C. Prim
D. Kruskal
Answer: A. BFS
Explanation: BFS explores vertices in increasing order of their distance from the source.
A. Maintain visited vertices and detect an already visited vertex that is not the parent
B. Always use Dijkstra
C. Sort all vertices
D. Use binary search
Answer: A
Explanation: In an undirected DFS, encountering a visited neighbor other than the current
node's parent indicates a cycle.
Q145. [DAG]
A directed graph has edges:
A → B
A → C
B → D
C → D
A. D, B, C, A
B. A, B, C, D
C. B, A, D, C
D. D, C, B, A
Answer: B. A, B, C, D
Explanation: Every prerequisite appears before the vertex that depends on it.
A --4--> B
A --1--> C
C --2--> B
B --3--> D
C --7--> D
Answer: C. 6
Explanation: A→C→B→D has cost (1+2+3=6), which is smaller than A→C→D = 8 and
A→B→D = 7.
A. The edge may belong to the MST but not the shortest source-destination path
B. It must belong to every shortest path
C. MST and shortest path are always identical
D. It cannot belong to an MST
Answer: A
Explanation: MST minimizes total tree weight, while shortest path minimizes the distance
between particular vertices.
A. Binary Heap
B. Stack only
C. Singly linked list only
D. Binary search algorithm
Explanation: A heap provides efficient insertion and extraction of the highest- or lowest-
priority element.
A. Dijkstra
B. DFS
C. Binary Search
D. Bubble Sort
Answer: A. Dijkstra
Explanation: A min-priority queue allows Dijkstra to efficiently select the currently closest
unsettled vertex.
A. 1 and 2 only
B. 1, 2 and 3 only
C. 2, 3 and 4 only
D. All four
Explanation: BFS uses a queue, DFS uses a stack/recursion, and Dijkstra assumes non-
negative edge weights; Kruskal is an MST algorithm, not a shortest-path algorithm.
Floyd-Warshall
(all pairs)
I am keeping the questions aligned with the supplied syllabus: C programming, recursion,
arrays, stacks, queues, linked lists, trees/BST/heaps/graphs, searching, sorting, hashing,
asymptotic complexity, greedy, DP, divide-and-conquer, graph traversal, MST and shortest
paths.
For this final section, I am deliberately increasing the proportion of calculation, tracing,
complexity analysis, algorithm selection, and statement-based questions rather than
simple definitions.
Q151–160 — Advanced Trees, BST &
Heaps
Q151. [BST — Insertion]
[
40,\ 20,\ 60,\ 10,\ 30,\ 50,\ 70
]
Explanation: Inorder traversal of a BST always produces the keys in sorted ascending order.
PYQ-pattern relevance: Very high — this combines BST construction and traversal rather
than testing either concept independently.
In a BST, when deleting a node having two children, a common replacement is:
Explanation: Replacing the node with its inorder successor/predecessor preserves the BST
ordering property.
A. O(1)
B. O(h)
C. O(n²)
D. O(2h)
Answer: B. O(h)
Explanation: At most one node is examined at each level, so search takes O(h); for a
balanced BST this is O(log n), while a skewed BST can be O(n).
[
10,\ 20,\ 30,\ 40,\ 50
]
A. Complete
B. Perfect
C. Right-skewed
D. Left-skewed
Answer: C. Right-skewed
Explanation: Every new value is larger than the previous value, so every node becomes the
right child of the previous node.
Which operation on a binary heap normally requires moving an element upward through its
ancestors?
A. Heapify-down
B. Insertion
C. Extract-min only
D. Traversal
Answer: B. Insertion
Explanation: A newly inserted element is initially placed at the next available leaf position
and may need to move upward to restore the heap property.
What is the worst-case complexity of inserting one element into a binary heap containing n
elements?
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Answer: B. O(log n)
Explanation: The inserted element can move from a leaf to the root, crossing at most the
heap height, which is O(log n).
A. O(n²)
B. O(n log n)
C. O(n)
D. O(log n)
Answer: C. O(n)
Explanation: Although individual heapify operations can take O(log n), most nodes are near
the leaves, giving a total O(n) construction time.
Explanation: A heap guarantees parent-child ordering, not complete ordering among all
elements.
A hospital emergency system always wants to process the highest-priority case first. Which
structure is most appropriate?
A. Stack
B. Simple queue
C. Priority queue
D. Circular linked list
Explanation: A priority queue removes elements according to priority rather than merely
arrival order; heaps are commonly used to implement it.
A completely skewed binary tree containing n nodes has height Θ(n), whereas a balanced
binary tree has height approximately:
A. Θ(1)
B. Θ(log n)
C. Θ(n²)
D. Θ(2ⁿ)
Answer: B. Θ(log n)
Explanation: Each level of a balanced binary tree contains exponentially more possible
nodes, resulting in logarithmic height.
A. 2
B. 12
C. 16
D. 21
Answer: C. 16
Explanation: The middle index is examined first; with eight elements, using the upper
middle gives 16.
Explanation: Binary search decides which half can be discarded based on ordering.
A. O(n)
B. O(log n)
C. O(n log n)
D. O(1) always
Answer: B. O(log n)
A. 2
B. 4
C. 8
D. 16
Answer: B. 4
Which factor has the greatest influence on Quick Sort's partition balance?
A. Choice of pivot
B. Number of variables in the program
C. Array data type only
D. Hash function
Explanation: A good pivot creates balanced partitions; repeatedly choosing a poor pivot can
produce O(n²) behavior.
Which algorithm is generally preferable when guaranteed O(n log n) worst-case comparison
sorting is required and auxiliary array space should be small?
A. Heap Sort
B. Quick Sort
C. Bubble Sort
D. Selection Sort
Explanation: Heap Sort provides O(n log n) worst-case time with O(1) auxiliary array space
in its standard implementation.
Explanation: Counting Sort can be efficient when the range of integer keys is reasonably
small.
[
h(k)=k\bmod10
]
A. 12 and 21
B. 15 and 25
C. 17 and 28
D. 11 and 22
Answer: B. 15 and 25
Under suitable assumptions and a controlled load factor, the average search complexity in a
hash table can be approximately:
A. O(1)
B. O(log n)
C. O(n) always
D. O(n²)
Answer: A. O(1)
Explanation: A good hash function distributes keys effectively, giving expected constant-
time lookup.
A. O(n)
B. O(log n)
C. O(n log n)
D. O(n²)
Explanation: The outer loop executes n times and the inner loop executes O(log n) times.
Q172. [Complexity]
What is the complexity?
i = n;
while(i > 1)
{
i = i / 2;
}
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: B. O(log n)
Q173. [Complexity]
[
1+2+3+\cdots+n
]
A. Θ(1)
B. Θ(log n)
C. Θ(n)
D. Θ(n²)
Answer: D. Θ(n²)
Q174. [Recursion]
Consider:
f(n)
{
if(n <= 1)
return;
f(n/2);
}
Answer: B. O(log n)
Explanation: The argument is halved at every recursive call, producing approximately log₂n
calls.
Q175. [Recursion]
Consider:
f(n)
{
if(n <= 1)
return;
f(n-1);
f(n-1);
}
A. O(log n)
B. O(n)
C. O(n²)
D. O(2ⁿ)
Answer: D. O(2ⁿ)
Explanation: Each call creates two calls of size n−1, producing an exponential recursion
tree.
Q176. [Recurrence]
Solve:
[
T(n)=T(n-1)+1
]
A. Θ(1)
B. Θ(log n)
C. Θ(n)
D. Θ(n²)
Answer: C. Θ(n)
Explanation: There are approximately n recursive levels, each performing constant work.
Explanation: Both recursively reduce the problem into smaller portions and solve the
relevant subproblem(s).
Consider:
[
T(n)=2T(n/2)+O(1)
]
A. O(log n)
B. O(n)
C. O(n log n)
D. O(n²)
Answer: B. O(n)
Explanation: There are (2^k) subproblems after k levels, and the recursion reaches size 1
after log₂n levels, giving Θ(n) total work.
Consider:
[
T(n)=2T(n/2)+n
]
A. Θ(log n)
B. Θ(n)
C. Θ(n log n)
D. Θ(n²)
Explanation: Each recursion level contributes Θ(n), and there are Θ(log n) levels.
Consider:
[
T(n)=4T(n/2)+n
]
A. Θ(n)
B. Θ(n log n)
C. Θ(n²)
D. Θ(2ⁿ)
Answer: C. Θ(n²)
Explanation: Here (n^{\log_2 4}=n²), which dominates the linear non-recursive work.
A. Greedy-choice property
B. Randomness
C. Hashing
D. Recursion must always be used
Explanation: The problem must permit a locally optimal choice that can be extended to an
optimal global solution.
Items have:
A. A
B. B
C. C
D. All equal
Answer: A. A
Explanation: Ratios are 6, 5 and 4 respectively; therefore A has the highest value per unit
weight.
Using the previous items, if the capacity is 20, which greedy strategy is appropriate?
A. Select C first
B. Select A first and then use the remaining capacity from B
C. Select B only regardless of ratio
D. Select the lowest-value item first
Answer: B
Why does the fractional-knapsack greedy strategy not generally work for 0/1 Knapsack?
Answer: A
Explanation: Each item must be either completely selected or completely rejected, so the
locally best ratio may prevent the optimal combination.
For strings:
X = ABC
Y = AC
A. 1
B. 2
C. 3
D. 0
Answer: B. 2
Explanation: AC is a common subsequence of both strings and has maximum length 2.
Answer: B
Answer: B
Answer: B
Explanation: DP systematically considers relevant subproblem states, while greedy commits
to local choices.
A problem allows items to be divided into fractions and asks for maximum value under a
weight constraint. Which technique should immediately come to mind?
Answer: A
Explanation: Fractional Knapsack has the greedy-choice property and is optimally solved by
value/weight ratio.
A -- B -- D
| |
C -- E
A. 1
B. 2
C. 3
D. 4
Answer: B. 2
Which algorithm is generally preferable for finding the shortest path in terms of number of
edges in an unweighted graph?
A. DFS
B. BFS
C. Prim
D. Kruskal
Answer: B. BFS
Explanation: BFS explores vertices in increasing distance from the source, guaranteeing the
minimum number of edges in an unweighted graph.
A. O(V+E)
B. O(V²E)
C. O(log V)
D. O(E²)
Answer: A. O(V+E)
Explanation: DFS visits each vertex and examines each adjacency-list edge a constant
number of times.
A. O(V+E)
B. O(V²) always
C. O(log V)
D. O(E²)
Answer: A. O(V+E)
Explanation: Each vertex enters the queue at most once and every edge is examined through
the adjacency lists.
Kruskal's algorithm is most naturally associated with which supporting operation for efficient
cycle detection?
Explanation: Disjoint Set Union efficiently determines whether adding an edge would
connect vertices already belonging to the same component.
A. Prim grows one tree from a starting vertex, while Kruskal considers edges globally in
increasing weight order
B. Both are shortest-path algorithms
C. Kruskal requires directed graphs only
D. Prim cannot work on weighted graphs
Answer: A
Explanation: Prim expands a connected tree, whereas Kruskal builds a forest by repeatedly
choosing the lightest non-cycle-forming edge.
A directed graph contains negative edge weights but no reachable negative cycle. Which
algorithm is appropriate for single-source shortest paths?
A. Dijkstra
B. Bellman-Ford
C. Prim
D. BFS only
Answer: B. Bellman-Ford
Explanation: Bellman-Ford supports negative edge weights and can detect negative cycles
reachable from the source.
There are V vertices, and shortest paths are required between every pair of vertices. Which
algorithm is a standard choice?
A. Floyd-Warshall
B. Binary Search
C. Prim
D. Insertion Sort
Answer: A. Floyd-Warshall
A. DFS
B. BFS
C. Kruskal
D. Dijkstra with arbitrary negative weights
Answer: B. BFS
Explanation: BFS handles cycles using visited information and guarantees minimum edge-
count distances in an unweighted graph.
S3: Dijkstra's algorithm is appropriate for graphs containing arbitrary negative edge weights.
A. S1 and S2 only
B. S1, S2 and S4 only
C. S2, S3 and S4 only
D. All four
Explanation: S1, S2 and S4 are correct; S3 is false because ordinary Dijkstra requires non-
negative edge weights.
PYQ-pattern relevance: 🔥 Very High — 10/10. This combines multiple syllabus concepts
and tests whether you can distinguish closely related algorithms instead of merely
remembering definitions.
GRAPH PROBLEM
│
┌──────────────┼───────────────┐
│ │ │
Traversal MST Shortest Path
│ │ │
┌───┴───┐ ┌───┴────┐ ┌────┴─────────┐
│ │ │ │ │ │ │
BFS DFS Prim Kruskal Dijkstra Bellman Floyd
│ │ │
1 source 1 source all pairs
non-neg. negative
This completes the full 200-question DSA + Algorithms sequence. The supplied CIL
syllabus itself confirms that these are the intended Programming/Data Structures and
Algorithms areas, so the final set has been kept within that scope rather than adding unrelated
DSA topics.
This is a copy of a shared ChatGPT chat. It won't be added to memory.
Report conversation
Yes — this confusion is very common, because Prims, Kruskal, Dijkstra, BFS, DFS, etc.
are all graph algorithms, but they don't all belong to the same algorithmic paradigm.
1. What is MST?
MST = Minimum Spanning Tree.
A spanning tree:
A Minimum Spanning Tree is the spanning tree whose total edge weight is minimum.
Example
Imagine 4 cities:
A ----2---- B
| |
5 3
| |
C ----1---- D
We want to connect all cities with minimum total cost, without creating a cycle.
Prim’s Algorithm
and
Kruskal’s Algorithm
2. The classification you should memorize
This is the important part:
Select the minimum-weight edge that connects the already-selected vertices to an unselected
vertex.
So:
Think:
It:
Therefore:
Easy distinction
Prim:
Kruskal:
It is a greedy algorithm.
At every step, it permanently selects the unvisited vertex having the smallest currently known
distance.
Therefore:
⚠️Important:
Dijkstra ≠ MST.
Break a problem into overlapping subproblems, solve them, and store/reuse their results.
Bellman-Ford
Finds single-source shortest paths and can handle negative edge weights.
Floyd-Warshall
Dij(k)=min(Dij(k−1),Dik(k−1)+Dkj(k−1))
You don't need to panic about the formula yet — just recognize:
Floyd-Warshall = DP
First ask:
Traversal?
→ BFS / DFS
→ Prim / Kruskal
Shortest path?
→ Dijkstra / Bellman-Ford / Floyd-Warshall
Then ask:
Approach Algorithms
Greedy Prim, Kruskal, Dijkstra, Fractional Knapsack, Activity Selection,
Huffman
Dynamic Bellman-Ford, Floyd-Warshall, 0/1 Knapsack, LCS, MCM
Programming
Divide & Conquer Merge Sort, Quick Sort, Binary Search
Backtracking N-Queens, Graph Coloring, Hamiltonian Cycle
Graph Traversal BFS, DFS
ALGORITHMS
|
+---------------+----------------+
| |
GRAPH OTHER PROBLEMS
|
+----+----+
| |
Traversal Optimization/Paths
| |
BFS +----------------------+
DFS | |
MST Shortest Path
| |
+---+---+ +-----+--------+
| | | | |
Prim Kruskal Dijkstra Bellman Floyd
| | | | |
Greedy Greedy Greedy DP DP
⭐ One-line rules
while
That's the classification you should have in your head before solving CIL-level algorithm
Report conversation
“Minimum Spanning Tree has minimum weight, so it must give the shortest path.”
It does not. MST minimizes the total weight of the entire tree, not the distance between
two particular vertices.
2
A -------- B
| |
1| |2
| |
C -------- D
1
A -------- D
10
Prim/Kruskal → MST
They want to connect ALL vertices with minimum total edge weight.
A−C=1,C−D=1,A−B=2
Total:
1+1+2=4
In the MST:
A
|
1
C
|
1
D
Distance:
1+1=2
Consider this:
1
A ------- B
| |
2 | | 1
| |
C ------- D
2
A -------- D
3
Total:
1+1+2=4
A → D in MST:
A→B→D
Cost:
1+1=2
Again, shortest.
Dijkstra/Bellman-Ford asks:
1
A ------- B
| |
10 1
| |
C ------- D
10
A -------- D
5
MST
Total:
1+1+5=7
Total MST:
1+1+5+10=17
A→B→D
with cost:
1+1=2
The MST's objective was not to find A→D. It was to connect A, B, C, D together as
cheaply as possible.
Patna → Delhi
So use:
That's:
MST
Use:
Prim
Kruskal
🔥 The easiest way to remember
Think of building roads.
Prim/Kruskal:
Dijkstra/Bellman-Ford:
Therefore:
MST=Shortest Path
And consequently:
Prim/Kruskal=Dijkstra/Bellman-Ford
Even though both involve weighted graphs and minimizing weights, they minimize
different things.
→ Prim/Kruskal
If it says:
→ Dijkstra/Bellman-Ford
If it says:
→ Floyd-Warshall
Yes — you are essentially clear, and your core distinction is correct. 👍 Just make two small
corrections.
1. Shortest-path algorithms
You said:
Bellman-Ford and Dijkstra are used to find shortest paths, with Bellman-Ford handling
negative edges.
Correct. ✅
And yes, in a shortest-path problem, you don't have to use all vertices. The shortest route
from A to D might use only:
A→B→D
They connect all vertices while minimizing the total weight of the selected edges.
Exactly. ✅
V−1 edges
You meant Floyd-Warshall, and Prim should not be in the shortest-path group.
WEIGHTED GRAPH
│
├── Minimum Spanning Tree
│ ├── Prim
│ └── Kruskal
│ → Connect ALL vertices
│ → Minimum TOTAL weight
│
└── Shortest Path
├── Dijkstra
│ → Single source
│ → No negative edges
│
├── Bellman-Ford
│ → Single source
│ → Negative edges allowed
│
└── Floyd-Warshall
→ ALL PAIRS shortest paths
→ Negative edges allowed
MST:
Shortest Path:
Report conversation
In simple words:
If two elements have the same value/key, their order after sorting remains the same as
before sorting.
1. Simple Example
Suppose we have students with their marks:
Name Marks
----------------
Juhi 80
Rahul 70
Aman 80
Neha 90
Juhi→Aman
Name Marks
----------------
Rahul 70
Juhi 80
Aman 80
Neha 90
Notice:
Juhi→Aman
Name Marks
----------------
Rahul 70
Aman 80
Juhi 80
Neha 90
70<80<80<90
But originally:
Juhi → Aman
and now:
Aman → Juhi
Important point
The only problem is that it changed the order of elements having equal keys.
Suppose:
A stable sorting algorithm can preserve the previous ordering among students having equal
marks.
4. Real-life example
Imagine a list of employees:
Employee Salary
---------------------
A 50,000
B 40,000
C 50,000
D 30,000
A→C
D ₹30,000
B ₹40,000
A ₹50,000
C ₹50,000
A remains before C.
That's stability.
[4A, 2, 4B, 1]
The A and B indicate that the two 4s are different records but have the same key.
Selection sort finds the minimum 1 and swaps it with the first element:
Originally:
4A→4B
After sorting:
4B→4A
Therefore:
Stable:
[B(70), A(80), C(80), D(90)]
↑ ↑
A C
A remains before C.
Unstable:
[B(70), C(80), A(80), D(90)]
↑ ↑
C A
faster sorting ❌
O(nlogn) sorting ❌
sorted data remains unchanged ❌
duplicate elements are removed ❌
It specifically means:
Common answers:
If you see:
A stable sorting algorithm preserves the relative order of records having equal keys.
Report conversation
When a collision occurs, instead of creating a linked list outside the table, we search for
another empty slot in the same table.
Simple example
Index: 0 1 2 3 4 5 6
─────────────────────────────
Table: _ _ _ _ _ _ _
Hash function:
h(k)=kmod7
Insert:
10→10mod7=3
So:
Index: 0 1 2 3 4 5 6
─────────────────────────────
Table: _ _ _ 10 _ _ _
17mod7=3
With open addressing, we find another empty position according to a probing method.
So 17 goes to index 4:
Index: 0 1 2 3 4 5 6
─────────────────────────────
Table: _ _ _ 10 17 _ _
🧠 Remember
Whereas separate chaining stores collided elements in a linked list/bucket associated with
the table slot.
Exam keyword:
👉 Open addressing → all elements remain inside the hash table.
Yes. When a collision occurs in hashing, there are several collision-resolution techniques.
Main techniques
🧠 Exam shortcut
These are the main collision-resolution techniques you should know for CIL/PSU-level
questions.
Absolutely. Let's use the same hash function and the same keys for every technique so the
difference becomes very clear.
We'll use:
h(k)=kmod7
0 1 2 3 4 5 6
Notice:
So all four keys collide at index 3. This makes the examples very useful.
1. Separate Chaining
Here, we don't search for another empty table position.
Insert 10
Index 0 → NULL
Index 1 → NULL
Index 2 → NULL
Index 3 → 10 → NULL
Index 4 → NULL
Index 5 → NULL
Index 6 → NULL
Insert 17
17 also hashes to 3.
Index 3 → 10 → 17 → NULL
Insert 24
Index 3 → 10 → 17 → 24 → NULL
Insert 31
Index 3 → 10 → 17 → 24 → 31 → NULL
0 → NULL
1 → NULL
2 → NULL
3 → 10 → 17 → 24 → 31 → NULL
4 → NULL
5 → NULL
6 → NULL
🧠 Key idea
2. Linear Probing
Now we use open addressing.
Formula:
hi(k)=(h(k)+i)mod7
Insert 10
10mod7=3
Index 3 is empty:
0 1 2 3 4 5 6
- - - 10 - - -
Insert 17
17mod7=3
Index 3 occupied.
Check next:
3+1=4
Index 4 is empty.
0 1 2 3 4 5 6
- - - 10 17 - -
Insert 24
Again:
24mod7=3
Put 24 at 5:
0 1 2 3 4 5 6
- - - 10 17 24 -
Insert 31
Again start at 3:
3 → occupied
4 → occupied
5 → occupied
6 → empty
So:
0 1 2 3 4 5 6
- - - 10 17 24 31
🧠 Key idea
Pattern:
3→4→5→6
Main disadvantage
3. Quadratic Probing
Instead of checking the next position one by one, we jump using squares.
Formula:
hi(k)=(h(k)+i2)mod7
So from index 3:
After modulo 7:
3, 4, 0, 5,…
Insert 10
10mod7=3
Index 3 empty:
0 1 2 3 4 5 6
- - - 10 - - -
Insert 17
17 → index 3 → occupied.
Try:
3+12=4
Index 4 is empty:
0 1 2 3 4 5 6
- - - 10 17 - -
Insert 24
24 → index 3 → occupied.
Try:
3+12=4
4 is occupied.
Try:
3+22=7 7mod7=0
Index 0 is empty.
So:
0 1 2 3 4 5 6
24 - - 10 17 - -
Insert 31
31 → index 3 → occupied.
Try:
3+12=4
occupied.
Try:
3+22=0
occupied.
Try:
3+32=12 12mod7=5
Index 5 is empty.
Final:
0 1 2 3 4 5 6
24 - - 10 17 31 -
🧠 Key idea
h(k)=kmod7
and keys:
10,17,24,31
Separate Chaining
3 → 10 → 17 → 24 → 31
Linear Probing
Index: 0 1 2 3 4 5 6
- - - 10 17 24 31
Quadratic Probing
Index: 0 1 2 3 4 5 6
24 - - 10 17 31 -
Linear Probing
h(k)+i
Pattern:
0,1,2,3,…
Quadratic Probing
h(k)+i2
Pattern:
0,1,4,9,…
Double Hashing
h1(k)+i×h2(k)
The most important numerical skill: when a question gives you a hash function, table size,
and keys, calculate the initial index and then follow the appropriate probing sequence.
1. Double Hashing
Double hashing is an open-addressing technique where we use two hash functions.
h(k,i)=(h1(k)+i×h2(k))modm
where:
Example
Let:
Insert:
10, 17, 24
Insert 10
First hash:
h1(10)=10mod7=3
So 10 goes to index 3.
Index: 0 1 2 3 4 5 6
- - - 10 - - -
Insert 17
First hash:
h1(17)=17mod7=3
Collision at 3.
h2(17)=5−(17mod5) =5−2=3
For i=0:
(3+0×3)mod7=3
Occupied.
For i=1:
(3+1×3)mod7=6
Index 6 is empty.
Therefore:
Index: 0 1 2 3 4 5 6
- - - 10 - - 17
Insert 24
First:
h1(24)=24mod7=3
Collision.
Second hash:
h2(24)=5−(24mod5) =5−4=1
Positions:
i=0:3
occupied.
i=1:(3+1)mod7=4
Index 4 is empty.
So:
Index: 0 1 2 3 4 5 6
- - - 10 24 - 17
Linear:
3 → 4 → 5 → 6
Quadratic:
3 → 4 → 0 → 5 ...
Double hashing:
That's why double hashing usually gives better distribution and reduces clustering.
h(k)=kmod7
And we see:
h(k)=3
Can we say:
k=?
So:
For example:
Key Hash
---------------------------
10 ───────────────→ 3
17 ───────────────→ 3
24 ───────────────→ 3
31 ───────────────→ 3
Once you only have 3, you don't know which original key produced it.
Index 3 → 10 → 17 → 24 → 31
h(17)=3
You're doing:
Hash value = 3
kmod7=3
Therefore:
k=7n+3
for integer n.
Examples:
3,10,17,24,31,…
Key→Hash value
Reverse hashing:
And: