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) //Base case
return 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.
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)
10. Graphs
A graph consists of: G=(V,E)
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
Worst case: O(n)
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
A Possible DFS:
/ \
B C
A → B → D → E → C
/ \
D E
BFS: A → B → C → D → E
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
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+3+....+ (n-1)
[
0+1+2+\cdots+(n-1)
] n(n+1)/2
[
=\frac{n(n-1)}2 (n^2 + n)/2
]
Therefore:
[ O(n^2)
\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
]
Therefore collision occurs.
Practical 9 — MST
Suppose:
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:
Example:
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.
CIL relevance: ⭐⭐⭐⭐☆ 8/10
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]
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
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.
A. Inorder
B. Preorder
C. Postorder
D. Level order
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
PYQ connection: Very close to the 2020 traversal question where preorder and inorder were
given and postorder had to be calculated.
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.
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]
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
Explanation: Recursion implicitly uses a call stack; iterative DFS explicitly uses a stack.
Q22. [Searching]
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]
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.
A. Chaining
B. Recursion
C. DFS
D. Merge
Answer: A. Chaining
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.
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²)
Answer: C. O(n log 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
Answer: B. Overlapping subproblems and optimal substructure
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.
A. Dijkstra
B. Prim
C. Floyd-Warshall
D. Kruskal
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]
Explanation: A directed graph must be acyclic for a valid topological ordering to exist.
Q45. [Complexity]
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
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.
Given:
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
PYQ similarity: Concept-based; array/address questions fit the computational style expected
in technical papers.
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.
A. Linked List
B. Static Array only
C. Binary Heap
D. Adjacency Matrix
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.
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
Q65. [BST]
Which property is correct for a Binary Search Tree?
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.
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).
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
Explanation: (64=2^6), so binary search requires logarithmic depth of approximately (\log_2
64=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.
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
Explanation: Standard Selection Sort can move equal-key elements past one another,
destroying their original relative order.
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
Explanation: Both produce remainder 5: (25\bmod10=5) and (35\bmod10=5).
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.
COMPLEXITY
Q89. [Complexity]
What is the complexity of:
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:
for(i=1; i<=n; i=i*2)
printf("%d", i);
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.
CIL relevance: ⭐⭐⭐⭐⭐ 10/10
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 problem is divided into smaller instances, solved recursively, and their
results are combined.
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
GRAPH ALGORITHMS
Q99. [MST]
Which of the following is not an MST algorithm?
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
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.
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
A. A, B, C, D, E, F
B. A, B, D, E, C, F
C. A, C, F, B, E, D
D. D, E, B, F, C, A
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.
Q110. [MST]
Which condition must an MST satisfy?
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
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.
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.
Shortest Paths
Q115. [Dijkstra]
Dijkstra's algorithm is primarily used to find:
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
Answer: C. Negative-weight 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
What is the shortest distance from A to 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
Answer: B. Heap 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
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.
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.
Q133. [Complexity]
Which grows fastest for sufficiently large n?
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.
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
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
A. BFS
B. DFS
C. Prim
D. Kruskal
Answer: A. BFS
Explanation: BFS explores vertices in increasing order of their distance from the source.
Q144. [Graph]
Which algorithm can be used to detect a cycle in an undirected graph using DFS?
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
A. 4
B. 5
C. 6
D. 7
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.
[
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 BST contains n nodes and has height h. The time complexity of searching for a key is:
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.
2 5 8 12 16 21 25 30
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.
CIL relevance: ⭐⭐⭐⭐⭐ 9/10
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)
For n = 16, approximately how many levels of division are required in Merge Sort before
subarrays contain one element?
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.
[
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
Explanation: Double hashing uses another hash function to determine the step size during
collision resolution.
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]
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);
}
A. O(1)
B. O(log n)
C. O(n)
D. O(n²)
Answer: B. O(log n)
Explanation: The argument is halved at every recursive call, producing approximately log₂n
calls.
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²)
Answer: C. Θ(n log 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
Answer: B
Answer: B
Answer: B
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.
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.