Consider that the quick sort algorithm is used to sort an array of n distinct randomly ordered elements. In every call, the pivot is chosen as the first element of the current subarray.
Let T(n) denote the expected time to sort the array. Assume that the time to partition is linear in the size of the current subarray.
Which of the following recurrence relations correctly represents T(n) in this scenario?
- (A)T(n) = T(1) + T(n - 1) + O(n)
- (B)T(n) = T(n/4) + T(3n/4) + O(n)
- (C)T(n) = 2T(n/2) + O(n)
- (D)T(n) = (1/n) Σ_{k=0}^{n-1} [T(k) + T(n - k - 1)] + O(n)
With randomly ordered input, the first element is equally likely to be any rank. If it is the (k+1)-th smallest, the partitions have sizes k and n - k - 1, each with probability 1/n for k = 0..n-1. Averaging gives T(n) = (1/n) Σ [T(k) + T(n - k - 1)] + O(n), which solves to O(n log n). (A) is the worst case, (B) and (C) are particular fixed splits. Answer: (D).
Concept: Expected-case recurrence averages over all pivot ranks. The trap is choosing the balanced-split recurrence, which describes one ideal case, not the expectation.
Let A be a sorted array containing 1000 distinct integers. You perform a recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y.
The maximum number of comparisons that may have to be performed if y is not an element of A is _______ . (Answer in integer)
Each step makes one (three-way) comparison and recurses on at most the larger half. Worst-case sizes: 1000 → 500 → 250 → 125 → 62 → 31 → 15 → 7 → 3 → 1, then the range becomes empty. That is 10 comparisons, i.e. ⌊log₂ 1000⌋ + 1 = 9 + 1 = 10. Answer: 10.
Concept: Unsuccessful binary search takes ⌊log₂ n⌋ + 1 probes in the worst case. The trap is answering ⌈log₂ 1000⌉ = 10 for the wrong reason or 9 by forgetting the last probe.
Consider a directed graph G = (V, E), where V is the finite set of vertices and E is the set of directed edges between the vertices. G may contain cycles but there is no self-loop. Further, G may not be strongly connected.
Let Gᴿ be the graph obtained by reversing the directions of all the edges in G without changing the set of vertices.
Assume that Breadth First Search (BFS) or Depth First Search (DFS) from any given vertex v of a graph visits only the reachable vertices from v in that graph.
Which of the following statements must always be true, regardless of the structure of G?
- (A)If u is a reachable vertex in the BFS of Gᴿ from v, then u is also a reachable vertex in the DFS of G from v.
- (B)In Gᴿ, the BFS traversal from v will visit exactly the same set of vertices as the DFS from v in G.
- (C)The order of vertices visited in the BFS of Gᴿ from v is the reverse of the order of vertices visited in the DFS of G from v.
- (D)If u is a reachable vertex in the DFS of G from v, then v is also a reachable vertex in the BFS of Gᴿ from u.
BFS and DFS both visit exactly the vertices reachable from the start. If u is reachable from v in G, there is a path v → ... → u; reversing every edge gives a path u → ... → v in Gᴿ, so v is reachable from u in Gᴿ (D true). (A)/(B) fail for G with the single edge v → u: in Gᴿ nothing is reachable from v, but u is reachable in G. (C) fails because traversal orders are unrelated. Answer: (D).
Concept: Reachability reverses under edge reversal. The trap is assuming reversed graphs have the same reachable sets from the same source.
Consider the problem of sorting the given array in ascending order:
P = [1, 2, 3, 5, 4]
Consider two sorting algorithms Bubble Sort (BS) and Insertion Sort (IS).
Let N1 be the total number of comparisons done by BS on the elements of P and N2 be the total number of comparisons done by IS on the elements of P.
Which of the following options is/are correct?
- (A)N1 = 10, N2 = 4
- (B)N1 > N2
- (C)IS on P will perform only one swap
- (D)Both BS and IS on P will make at least one unnecessary comparison (i.e., comparing elements that are already in correct order)
Insertion sort: insert 2 (1 comparison), 3 (1), 5 (1), 4 (compare with 5 and shift, then compare with 3 and stop: 2). N2 = 5, with one swap/shift (C true), so (A) false. Bubble sort: the plain version makes 4 + 3 + 2 + 1 = 10 comparisons; even with an early-exit flag it makes 4 + 3 = 7. Either way N1 > N2 (B true). Both compare already-ordered pairs such as (1, 2) (D true). Answer: (B), (C), (D).
Concept: Counting comparisons on a nearly sorted array. The trap is counting only 4 comparisons for insertion sort, missing the final comparison that stops the insertion of 4.
Consider the given Python program. The output of the program is __________ . (Answer in integer)
def fun(L, i=0):
if i >= len(L)-1:
return 0
if L[i] > L[i+1]:
L[i+1], L[i] = L[i], L[i+1]
return 1+fun(L, i+1)
else:
return fun(L, i+1)
data = [5, 3, 4, 1, 2]
count = 0
for _ in range(len(data)):
count += fun(data)
print(count)fun is one bubble-sort pass that swaps adjacent out-of-order pairs and returns the number of swaps; the list is modified in place. The loop runs 5 passes, enough to sort a 5-element list, so count equals the total number of adjacent swaps, which equals the number of inversions. Inversions in [5, 3, 4, 1, 2]: 5 with 3, 4, 1, 2 (4); 3 with 1, 2 (2); 4 with 1, 2 (2); total 8. Pass by pass: 4, 2, 2, 0, 0. Answer: 8.
Concept: Bubble sort swaps equal the inversion count. The trap is counting comparisons instead of swaps, or thinking the list is copied between calls.
Consider the following recurrence relations. For all n > 1,
T₁(n) = 4T₁(n/2) + T₂(n)
T₂(n) = 5T₂(n/4) + Θ(log₂ n)
Assume that for all n ≤ 1, T₁(n) = 1 and T₂(n) = 1. Which one of the following options is correct?
- (A)T₁(n) = Θ(n²)
- (B)T₁(n) = Θ(n² log₂ n)
- (C)T₁(n) = Θ(n^(log₄ 5))
- (D)T₁(n) = Θ(n^(log₄ 5) log₂ n)
T₂: a = 5, b = 4, n^(log₄ 5) ≈ n^1.16, which dominates log n polynomially, so T₂(n) = Θ(n^(log₄ 5)) by Master theorem case 1. T₁: a = 4, b = 2, n^(log₂ 4) = n². The driving term n^1.16 is polynomially smaller than n², so case 1 again: T₁(n) = Θ(n²). Answer: (A) T₁(n) = Θ(n²).
Concept: Nested Master theorem: solve the inner recurrence first, then use it as f(n). Options (C)/(D) are the trap of reporting T₂ instead of T₁, since log₄ 5 < 2.
Consider a hash table P[0, 1, ..., 10] that is initially empty. The hash table is maintained using open addressing with linear probing. The hash function used is h(x) = (x + 7) mod 11. Consider the following sequence of insertions performed on P: 1, 13, 22, 15, 11, 24. Which of the following positions in the hash table is/are empty after these insertions are performed?
- (A)0
- (B)10
- (C)2
- (D)1
1 -> 8. 13 -> 20 mod 11 = 9. 22 -> 29 mod 11 = 7. 15 -> 22 mod 11 = 0. 11 -> 18 mod 11 = 7, occupied; probe 8, 9 occupied; 10 free. 24 -> 31 mod 11 = 9, occupied; 10, 0 occupied; 1 free. Occupied: 0, 1, 7, 8, 9, 10. Of the options only position 2 is empty. Answer: (C) 2.
Concept: Linear probing wraps around from the last slot to slot 0, which is how 24 ends up at 1. Forgetting the wrap leaves 1 wrongly empty.
Let G(V, E) be an undirected, edge-weighted graph with integer weights. The weight of a path is the sum of the weights of the edges in that path. The length of a path is the number of edges in that path. Let s ∈ V be a vertex in G. For every u ∈ V and for every k ≥ 0, let d_k(u) denote the weight of a shortest path (in terms of weight) from s to u of length at most k. If there is no path from s to u of length at most k, then d_k(u) = ∞. Consider the statements:
S1: For every k ≥ 0 and u ∈ V, d_(k+1)(u) ≤ d_k(u).
S2: For every (u, v) ∈ E, if (u, v) is part of a shortest path (in terms of weight) from s to v, then for every k ≥ 0, d_k(u) ≤ d_k(v).
Which one of the following options is correct?
- (A)Only S1 is true
- (B)Only S2 is true
- (C)Both S1 and S2 are true
- (D)Neither S1 nor S2 is true
S1: every path of length at most k is also of length at most k + 1, so the minimum over the larger set cannot be larger: true. S2: weights are integers and may be negative. Let s-u have weight 5 and u-v weight -3 with no other edges. The shortest s to v path is s-u-v using (u, v), with weight 2. For k = 2, d₂(u) = 5 > d₂(v) = 2. Also for k = 1, d₁(v) = ∞ while d₁(u) = 5, but the k = 2 case already breaks S2. So S2 is false. Answer: (A) Only S1 is true.
Concept: Hop-bounded distances (as in Bellman-Ford rounds) are monotone non-increasing in k. 'Predecessor is closer' fails once negative edges are allowed, which 'integer weights' quietly permits.
Let G(V, E) be a simple, undirected, edge-weighted graph with unique edge weights. Which of the following statements about the minimum spanning trees (MST) of G is/are true?
- (A)In every cycle C of G, the edge with the largest weight in C is not in any MST
- (B)In every cycle C of G, the edge with the smallest weight in C is in every MST
- (C)For every vertex v ∈ V, the edge with the largest weight incident on v is not in any MST
- (D)For every vertex v ∈ V, the edge with the smallest weight incident on v is in every MST
(A) Cycle property: with distinct weights the heaviest edge on any cycle is in no MST. True. (B) False: vertices a, b, c, d with ab = 5, bc = 6, ca = 7, ad = 1, db = 2. In cycle a-b-c, ab is the lightest, but ab is the heaviest on cycle a-b-d, so it is in no MST. (C) False: if v has degree 1, its only edge is both the heaviest and lightest at v and must be in every MST. (D) Cut property on the cut ({v}, V - {v}): the lightest crossing edge is in every MST. True. Answer: (A), (D).
Concept: Cycle property excludes the heaviest edge of a cycle; cut property includes the lightest edge across a cut. The lightest edge of a cycle has no such guarantee.
Consider the following pseudocode for depth-first search (DFS) algorithm which takes a directed graph G(V, E) as input, where d[v] and f[v] are the discovery time and finishing time, respectively, of the vertex v ∈ V. Suppose that the input directed graph G(V, E) is a directed acyclic graph (DAG). For an edge (u, v) ∈ E, which of the following options will NEVER be correct?
DFS(G):
unmark all v ∈ V
t ← 0
for each v ∈ V
if v is unmarked
t ← Explore(G, v, t)
end if
end for
Explore(G, v, t):
mark v
t ← t + 1
d[v] ← t
for each (v, w) ∈ E
if w is unmarked
t ← Explore(G, w, t)
end if
end for
t ← t + 1
f[v] ← t
return t- (A)d[u] < d[v] < f[v] < f[u]
- (B)d[v] < d[u] < f[u] < f[v]
- (C)d[v] < f[v] < d[u] < f[u]
- (D)d[u] < d[v] < f[u] < f[v]
By the parenthesis theorem, two DFS intervals are either nested or disjoint, so the overlapping pattern (D) can never occur in any graph. (A): v nested in u, a tree or forward edge, fine in a DAG. (C): v finished before u was discovered, a cross edge, fine. (B): u nested inside v means u is a descendant of v, so (u, v) is a back edge, which implies a cycle, impossible in a DAG. Answer: (B), (D).
Concept: Parenthesis theorem plus edge classification: a directed graph is acyclic iff DFS finds no back edge. That is also why reverse finishing order is a topological sort.
Consider the following functions, where n is a positive integer.
n^(1/3), log(n), log(n!), 2^(log(n))
Which one of the following options lists the functions in increasing order of asymptotic growth rate?
Note: Assume the base of log to be 2.
- (A)log(n), n^(1/3), 2^(log(n)), log(n!)
- (B)n^(1/3), log(n), log(n!), 2^(log(n))
- (C)log(n), n^(1/3), log(n!), 2^(log(n))
- (D)2^(log(n)), n^(1/3), log(n), log(n!)
2^(log₂ n) = n. log(n!) = Θ(n log n) by Stirling. Any polynomial beats a logarithm, so log n < n^(1/3) < n. Hence log n, n^(1/3), 2^(log n), log(n!). Answer: (A).
Concept: Simplify before comparing: 2^(log₂ n) is just n, and log(n!) is Θ(n log n). The trap is treating 2^(log n) as exponential and putting it last.
Which of the following can be recurrence relation(s) corresponding to an algorithm with time complexity Θ(n)?
- (A)T(n) = T(n - 1) + 1, T(1) = 1
- (B)T(n) = 2T(n/2) + 1, T(1) = 1
- (C)T(n) = 2T(n/2) + n, T(1) = 1
- (D)T(n) = T(n - 1) + n, T(1) = 1
(A) unrolls to T(n) = n: Θ(n). (B) by the master theorem, a = 2, b = 2, n^(log_b a) = n dominates f(n) = 1, so Θ(n). (C) f(n) = n equals n^(log_b a), so Θ(n log n). (D) sums 1 + 2 + ... + n = Θ(n²). Answer: (A) and (B).
Concept: Solving recurrences by unrolling and the master theorem. The trap is (B): two half-size calls with constant work still total Θ(n), not Θ(log n).
The keys 5, 28, 19, 15, 26, 33, 12, 17, 10 are inserted into a hash table using the hash function h(k) = k mod 9. The collisions are resolved by chaining. After all the keys are inserted, the length of the longest chain is __________. (answer in integer)
k mod 9: 5→5, 28→1, 19→1, 15→6, 26→8, 33→6, 12→3, 17→8, 10→1. Slot 1 holds 28, 19, 10 (3 keys); slots 6 and 8 hold 2 each; others at most 1. Longest chain = 3. Answer: 3.
Concept: Separate chaining: just compute each bucket and count. The usual slip is an arithmetic error in a mod computation.
Consider an array A = [10, 7, 8, 19, 41, 35, 25, 31]. Suppose the merge sort algorithm is executed on array A to sort it in increasing order. The merge sort algorithm will carry out a total of 7 merge operations.
A merge operation on sorted left array L and sorted right array R is said to be void if the output of the merge operation is the elements of array L followed by the elements of array R.
The number of void merge operations among these 7 merge operations is __________. (answer in integer)
Level 1: [10]+[7] not void; [8]+[19] void; [41]+[35] not void; [25]+[31] void. Level 2: [7,10]+[8,19] not void (8 < 10); [35,41]+[25,31] not void. Level 3: [7,8,10,19]+[25,31,35,41] void (19 < 25). A merge is void exactly when max(L) < min(R). Total = 3. Answer: 3.
Concept: Trace top-down merge sort level by level; a merge is void when the largest element of L is below the smallest of R. The trap is checking the original unsorted halves instead of the sorted subarrays.
Let G be a weighted directed acyclic graph with m edges and n vertices. Given G and a source vertex s in G, which one of the following options gives the worst case time complexity of the fastest algorithm to find the lengths of shortest paths from s to all vertices that are reachable from s in G?
- (A)Θ(m + n)
- (B)Θ(m + n log(n))
- (C)Θ(nm)
- (D)Θ(n³)
Topologically sort the DAG in Θ(m + n), then relax the outgoing edges of each vertex in topological order. Every edge is relaxed once, and when a vertex is processed its distance is final. Total Θ(m + n), and every edge may need to be examined, so this is optimal. It also handles negative weights. Answer: (A).
Concept: DAG shortest paths need no priority queue. The trap is reaching for Dijkstra (B) or Bellman-Ford (C) without exploiting acyclicity.
Consider an array A of integers of size n. The indices of A run from 1 to n. An algorithm is to be designed to check whether A satisfies the condition given below.
∀ i, j ∈ {1, ..., n - 1} such that i > j, (A[i + 1] - A[i]) > (A[j + 1] - A[j])
Which one of the following gives the worst case time complexity of the fastest algorithm that can be designed for the problem?
- (A)Θ(n)
- (B)Θ(log(n))
- (C)Θ(n log(n))
- (D)Θ(n²)
Let dᵢ = A[i+1] - A[i] for i = 1..n-1. The condition says dᵢ > dⱼ whenever i > j, i.e. the difference sequence is strictly increasing. By transitivity it suffices to check dᵢ₊₁ > dᵢ for consecutive i: one pass, Θ(n). A lower bound of Ω(n) holds because any unread element could break the condition. Answer: (A) Θ(n).
Concept: A condition over all pairs often reduces to adjacent pairs via transitivity. The trap is checking all pairs in Θ(n²) or thinking binary search applies.
Consider a table T, where the elements T[i][j], 0 ≤ i, j ≤ n, represent the cost of the optimal solutions of different subproblems of a problem that is being solved using a dynamic programming algorithm. The recursive formulation to compute the table entries is as follows:
T[0][k] = T[k][0] = 1 for k = 0, 1, 2, ..., n
T[i][j] = 2T[i-1][j] + 3T[i][j-1] for 1 ≤ i, j ≤ n
Consider the following two algorithms to compute entries of T. Assume that for both the algorithms, for all 0 ≤ i, j ≤ n, T[i][j] has been initialized to 1.
Algorithm B1:
For i = 1, 2, ..., n
For j = 1, 2, ..., n
T[i][j] = 2T[i-1][j] + 3T[i][j-1]
Algorithm B2:
For s = 2, 3, ..., 2n
For i = 1, 2, ..., n
For j = 1, 2, ..., n
If (i + j == s)
T[i][j] = 2T[i-1][j] + 3T[i][j-1]
Algorithm Bk, k ∈ {1,2} is said to be correct if and only if it calculates the correct values of T[i][j], for all 0 ≤ i, j ≤ n, (as per the recursive formulation) at the end of the execution of the algorithm Bk.
Which one of the following statements is true?
- (A)Both algorithms B1 and B2 are correct
- (B)Algorithm B1 is correct, but algorithm B2 is incorrect
- (C)Algorithm B2 is correct, but algorithm B1 is incorrect
- (D)Both algorithms B1 and B2 are incorrect
T[i][j] depends on T[i-1][j] (row above) and T[i][j-1] (left). Boundary entries are already 1. B1 fills row by row, left to right, so both dependencies are final when used: correct. B2 fills anti-diagonals i + j = s in increasing s; both dependencies lie on diagonal s - 1 (or on the boundary), already computed: correct. B2 is just slower (Θ(n³)). Answer: (A).
Concept: A DP evaluation order is valid if every entry's dependencies are computed before it. Both row-major and anti-diagonal orders satisfy this. The trap is judging B2 wrong because of its wasted iterations.
Consider a hash table of size 10 with indices {0, 1, ..., 9}, with the hash function
h(x) = 3x (mod 10),
where linear probing is used to handle collisions. The hash table is initially empty and then the following sequence of keys is inserted into the hash table: 1, 4, 5, 6, 14, 15. The indices where the keys 14 and 15 are stored are, respectively
- (A)2 and 5
- (B)2 and 6
- (C)4 and 5
- (D)4 and 6
h(1) = 3 -> slot 3.
h(4) = 12 mod 10 = 2 -> slot 2.
h(5) = 15 mod 10 = 5 -> slot 5.
h(6) = 18 mod 10 = 8 -> slot 8.
h(14) = 42 mod 10 = 2: slot 2 full, 3 full, 4 free -> slot 4.
h(15) = 45 mod 10 = 5: slot 5 full, 6 free -> slot 6.
Answer: (D) 4 and 6.
Concept: Linear probing moves to the next free slot on a collision. The trap is reporting the home slot (2 and 5) instead of where the key actually lands after probing.
For which of the following inputs does binary search take time O(log n) in the worst case?
- (A)An array of n integers in any order
- (B)A linked list of n integers in any order
- (C)An array of n integers in increasing order
- (D)A linked list of n integers in increasing order
Binary search needs sorted data (otherwise discarding half the range is unjustified) and O(1) access to the middle element. An unsorted array or list fails the first requirement. A sorted linked list fails the second: reaching the middle takes O(n) steps, so the total is O(n). Only a sorted array gives O(log n). Answer: (C).
Concept: Binary search requires both order and random access. The trap is the sorted linked list: it has order but no random access, so it degrades to linear time.
Suppose that insertion sort is applied to the array [1, 3, 5, 7, 9, 11, x, 15, 13] and it takes exactly two swaps to sort the array. Select all possible values of x.
- (A)10
- (B)12
- (C)14
- (D)16
Insertion sort makes one adjacent swap per inversion. The pair (15, 13) is always one inversion, so x must create exactly one more.
x = 10: 11 > 10 before it - 1 inversion; 10 < 15, 13. Total 2. Yes.
x = 12: nothing before it is larger, and 12 < 15, 13. Total 1. No.
x = 14: 14 > 13 after it - 1 inversion. Total 2. Yes.
x = 16: 16 > 15 and 16 > 13 - 2 inversions. Total 3. No.
Answer: (A) 10, (C) 14.
Concept: Swaps in insertion sort equal the number of inversions. The trap is counting only inversions involving x and forgetting the fixed (15, 13) pair.
Let G be a simple, unweighted, and undirected graph. A subset of the vertices and edges of G is known: the vertices a, b, c, d, e, f, g, h with edges a-b, b-c, c-d, e-f, f-g, g-h, a-f, f-c, c-h.
It is given that a - b - c - d is a shortest path between a and d; e - f - g - h is a shortest path between e and h; a - f - c - h is a shortest path between a and h. Which of the following is/are NOT the edges of G?
- (A)(b, d)
- (B)(b, g)
- (C)(b, h)
- (D)(e, g)
Each given shortest path has length 3, so no edge may create a shorter route between its endpoints.
(b, d): a - b - d would have length 2 < 3. Not an edge.
(b, h): a - b - h would have length 2 < 3. Not an edge.
(e, g): e - g - h would have length 2 < 3. Not an edge.
(b, g): it creates a - b - g - h (length 3, not shorter), and gives no shorter a-d or e-h path, so it may exist.
Answer: (A), (C), (D).
Concept: An edge between two vertices of a shortest path that skips ahead would shorten it, so such edges cannot exist. The trap is ruling out (b, g), which only creates a path of equal length.
Consider a directed graph G = (V, E), where V = {0, 1, 2, ..., 100} and E = {(i, j) : 0 < j - i ≤ 2, for all i, j ∈ V}. Suppose the adjacency list of each vertex is in decreasing order of vertex number, and depth-first search (DFS) is performed at vertex 0. The number of vertices that will be discovered after vertex 50 is ______ (Answer in integer)
Each vertex i has out-edges to i + 2 and i + 1, visited in that order.
DFS from 0 always takes the +2 edge first: 0, 2, 4, ..., 100 are discovered in that order. 100 has no out-edges.
Backtracking: 98's next neighbour 99 is new, discovered (its neighbour 100 is done). Then 96 discovers 97, and so on down to 0 discovering 1.
Discovery order: 0, 2, ..., 100, then 99, 97, ..., 1.
After 50: the evens 52, ..., 100 (25 vertices) and all 50 odd vertices.
Total = 25 + 50 = 75.
Concept: Adjacency order decides the DFS path: the larger neighbour first means a dive along even vertices, and odd vertices are found only during backtracking. The trap is assuming 51 is found right after 50.
Let G be any undirected graph with positive edge weights, and T be a minimum spanning tree of G. For any two vertices, u and v, let d1(u, v) and d2(u, v) be the shortest distances between u and v in G and T, respectively. Which ONE of the options is CORRECT for all possible G, T, u and v?
- (A)d1(u, v) = d2(u, v)
- (B)d1(u, v) ≤ d2(u, v)
- (C)d1(u, v) ≥ d2(u, v)
- (D)d1(u, v) ≠ d2(u, v)
T is a subgraph of G, so the unique u-v path in T is also a path in G. The shortest u-v distance in G is at most the length of that path: d1 ≤ d2. Equality need not hold: in a triangle with weights 2, 2, 3, the MST drops the 3-edge, and its endpoints are 3 apart in G but 4 apart in T. Equality can also hold (u, v adjacent by an MST edge), so (D) fails. Answer: (B).
Concept: An MST minimises total weight, not pairwise distances; it is not a shortest-path tree. Removing edges can only lengthen shortest paths.
Consider the following recurrence relation:
T(n) = 2T(n - 1) + n·2ⁿ for n > 0, T(0) = 1.
Which ONE of the following options is CORRECT?
- (A)T(n) = Θ(n²2ⁿ)
- (B)T(n) = Θ(n2ⁿ)
- (C)T(n) = Θ((log n)² 2ⁿ)
- (D)T(n) = Θ(4ⁿ)
Divide by 2ⁿ: let U(n) = T(n)/2ⁿ. Then U(n) = U(n - 1) + n with U(0) = 1. So U(n) = 1 + (1 + 2 + ... + n) = 1 + n(n + 1)/2 = Θ(n²). Hence T(n) = 2ⁿ U(n) = Θ(n²2ⁿ). Answer: (A).
Concept: Normalising by the homogeneous solution 2ⁿ turns it into a sum. The trap is Θ(n2ⁿ), ignoring that the n·2ⁿ term accumulates over n levels.
The pseudocode of a function fun() is given below. Let A[0, ... ,29] be an array storing 30 distinct integers in descending order. The number of swap operations that will be performed, if the function fun() is called with A[0, ... ,29] as argument, is __________. (Answer in integer)
fun(int A[0,...,n-1]){
for i=0 to n-2
for j=0 to n-i-2
if (A[j]>A[j+1])
then swap A[j] and A[j+1]
}fun() is bubble sort; each swap removes exactly one inversion. A descending array of n distinct values has every pair inverted: n(n - 1)/2 inversions. For n = 30: 30 x 29 / 2 = 435 swaps. Answer: 435.
Concept: Bubble sort swaps = number of inversions. Reverse-sorted input is the worst case, giving the maximum C(n, 2).
Let G(V, E) be an undirected and unweighted graph with 100 vertices. Let d(u, v) denote the number of edges in a shortest path between vertices u and v in V. Let the maximum value of d(u, v), u, v ∈ V such that u ≠ v, be 30. Let T be any breadth-first-search tree of G. Which ONE of the given options is CORRECT for every such graph G?
- (A)The height of T is exactly 15.
- (B)The height of T is exactly 30.
- (C)The height of T is at least 15.
- (D)The height of T is at least 30.
A BFS tree from root r has height equal to the eccentricity e(r), the largest d(r, v). Take u, v with d(u, v) = 30. By the triangle inequality 30 ≤ d(u, r) + d(r, v) ≤ 2e(r), so e(r) ≥ 15. Height 15 occurs from a central vertex (e.g. middle of a path) and 30 from an endpoint, so neither "exactly" option holds for every G, and "at least 30" fails for a central root. Answer: (C).
Concept: BFS tree height = eccentricity of the root, which lies between radius and diameter, and radius ≥ diameter / 2. Trap: assuming BFS height always equals the diameter.
The maximum value of x such that the edge between the nodes B and C is included in every minimum spanning tree of the given graph is _________. (answer in integer)
Graph (from the figure): vertices A, B, C, D with edges A-B = 7, A-D = 6, D-C = 8, B-C = x, B-D = 1, A-C = 3.
Edge BC is in every MST iff it is strictly lighter than the heaviest edge on every other B-C path (cycle property). Other B-C paths and their maximum edge: B-A-C max(7, 3) = 7; B-D-C max(1, 8) = 8; B-D-A-C max(1, 6, 3) = 6; B-A-D-C max(7, 6, 8) = 8. The smallest of these is 6, so we need x < 6. At x = 6, the MST {BD, AC, AD} avoids BC. Largest integer: x = 5. Answer: 5.
Concept: An edge is in every MST iff every alternative path between its endpoints contains an edge strictly heavier than it. Trap: answering 6, where a tie lets another MST exclude BC.
In a double hashing scheme, h1(k) = k mod 11 and h2(k) = 1 + (k mod 7) are the auxiliary hash functions. The size m of the hash table is 11. The hash function for the i-th probe in the open address table is [h1(k) + i·h2(k)] mod m. The following keys are inserted in the given order: 63, 50, 25, 79, 67, 24.
The slot at which key 24 gets stored is ___________. (Answer in integer)
63 mod 11 = 8 → slot 8. 50 mod 11 = 6 → slot 6. 25 mod 11 = 3 → slot 3. 79 mod 11 = 2 → slot 2. 67 mod 11 = 1 → slot 1. 24: h1 = 2 (occupied), h2 = 1 + (24 mod 7) = 1 + 3 = 4. i = 1: (2 + 4) mod 11 = 6 (occupied). i = 2: (2 + 8) mod 11 = 10, free. Key 24 goes to slot 10. Answer: 10.
Concept: Double hashing steps by h2(k) on each collision. Traps: using linear probing (slot 4) or forgetting h2 adds 1 to k mod 7.
Consider an unordered list of N distinct integers. What is the minimum number of element comparisons required to find an integer in the list that is NOT the largest in the list?
- (A)1
- (B)N - 1
- (C)N
- (D)2N - 1
Pick any two elements and compare them once. The smaller one cannot be the largest element of the list, since the other is bigger. Zero comparisons cannot work because no element is known to be non-maximal. Answer: (A) 1.
Concept: Read the question: any non-largest element is enough, not the largest itself. The trap is answering N - 1, the cost of finding the maximum.
Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph G is/are TRUE?
- (A)A DFS tree of G is a Shortest Path tree of G.
- (B)Every non-tree edge of G with respect to a DFS tree is a forward/back edge.
- (C)If (u, v) is a non-tree edge of G with respect to a BFS tree, then the distances from the source vertex s to u and v in the BFS tree are within ±1 of each other.
- (D)Both BFS and DFS can be used to find the connected components of G.
(A) False: in a triangle s-a-b, DFS may go s-a-b, giving b depth 2 though its distance is 1. (B) True: in an undirected DFS there are no cross edges; every non-tree edge joins an ancestor and a descendant. (C) True: for any edge (u, v), BFS levels satisfy |d(u) - d(v)| ≤ 1, otherwise the farther vertex would have been discovered earlier. (D) True: repeatedly starting either traversal from an unvisited vertex labels components. Answer: (B), (C), (D).
Concept: Key edge properties: undirected DFS has only tree and back edges; BFS non-tree edges join the same or adjacent levels. Only BFS gives shortest paths in unweighted graphs.
Let G be an edge-weighted undirected graph with positive edge weights. Suppose a positive constant α is added to the weight of every edge. Which ONE of the following statements is TRUE about the minimum spanning trees (MSTs) and shortest paths (SPs) in G before and after the edge weight update?
- (A)Every MST remains an MST, and every SP remains an SP.
- (B)MSTs need not remain MSTs, and every SP remains an SP.
- (C)Every MST remains an MST, and SPs need not remain SPs.
- (D)MSTs need not remain MSTs, and SPs need not remain SPs.
Every spanning tree has exactly n - 1 edges, so each tree's weight rises by (n - 1)α and the ranking of trees is unchanged: every MST stays an MST. A path with k edges rises by kα, so paths with more edges are penalized more. Example: s-t direct weight 3, s-a-t with weights 1 and 1. Before, s-a-t (2) is shortest; after adding 2, direct = 5 and s-a-t = 6. SPs need not remain SPs. Answer: (C).
Concept: MSTs depend only on the relative order of edge weights; shortest paths depend on sums over paths of varying length. Multiplying all weights by a positive constant would preserve both.
An array A of length n with distinct elements is said to be bitonic if there is an index 1 ≤ i ≤ n such that A[1..i] is sorted in the non-decreasing order and A[i + 1..n] is sorted in the non-increasing order. Which ONE of the following represents the best possible asymptotic bound for the worst-case number of comparisons by an algorithm that searches for an element in a bitonic array A?
- (A)Θ(n)
- (B)Θ(1)
- (C)Θ(log² n)
- (D)Θ(log n)
Find the peak by binary search: compare A[mid] with A[mid+1]; if increasing, the peak is to the right, else at or to the left. This takes O(log n). Then binary search the increasing part and the decreasing part separately, O(log n) each. Total O(log n). A lower bound of Ω(log n) holds because a sorted array is a special bitonic array. Answer: (D) Θ(log n).
Concept: Decompose into peak-finding plus two ordinary binary searches. The trap is (C), assuming nested binary searches.
Consider the following algorithm someAlgo that takes an undirected graph G as input. The output of someAlgo(T) for the tree T described below is ___________. (Answer in integer)
The tree T (as drawn in the paper) has 14 vertices, labelled here A to N, and 13 edges: A-C, C-D, C-E, B-E, E-F, F-G, E-H, H-I, H-J, H-K, K-L, K-M, M-N.
someAlgo(G)
1. Let v be any vertex in G. Run BFS on G starting at v.
Let u be a vertex in G at maximum distance from v as given by the BFS.
2. Run BFS on G again with u as the starting vertex.
Let z be the vertex at maximum distance from u as given by the BFS.
3. Output the distance between u and z in G.Two BFS passes on a tree return its diameter: the first BFS ends at an endpoint of a longest path, the second measures that path. Longest paths here: N-M-K-H-E-F-G and N-M-K-H-E-C-A, each with 6 edges. Check: from E the farthest vertices are N (4 edges) and A, G, D (2 edges), so no path is longer than 4 + 2 = 6. Answer: 6.
Concept: Double BFS computes the diameter of a tree exactly (not of general graphs). Count edges along the longest path, not vertices.
Consider performing uniform hashing on an open address hash table with load factor α = n/m < 1, where n elements are stored in the table with m slots. The expected number of probes in an unsuccessful search is at most 1/(1 - α).
Inserting an element in this hash table requires at most ______ probes, on average.
- (A)ln(1/(1 - α))
- (B)1/(1 - α)
- (C)1 + α/2
- (D)1/(1 + α)
To insert a key with open addressing we probe until an empty slot is found, which is exactly the probe sequence of an unsuccessful search for that key. Hence the expected number of probes for insertion is at most 1/(1 - α). Answer: (B) 1/(1 - α).
Concept: Insertion in open addressing equals an unsuccessful search. The trap is the expression (1/α) ln(1/(1 - α)), which bounds a successful search, not an insertion.
Let F(n) denote the maximum number of comparisons made while searching for an entry in a sorted array of size n using binary search.
Which ONE of the following options is TRUE?
- (A)F(n) = F(⌊n/2⌋) + 1
- (B)F(n) = F(⌊n/2⌋) + F(⌈n/2⌉)
- (C)F(n) = F(⌊n/2⌋)
- (D)F(n) = F(n - 1) + 1
Binary search compares the target with the middle element (1 comparison) and then continues in one half; excluding the middle, the larger half has ⌊n/2⌋ elements. So the worst case satisfies F(n) = F(⌊n/2⌋) + 1, giving F(n) = ⌊log₂ n⌋ + 1. (B) would search both halves, (C) has no cost per step, (D) describes linear search. Answer: (A).
Concept: Recurrence for binary search. The trap is (B), the shape of a divide-and-conquer that recurses into both halves, like merge sort.
Consider a state space where the start state is number 1. The successor function for the state numbered n returns two states numbered n+1 and n+2. Assume that the states in the unexpanded state list are expanded in the ascending order of numbers and the previously expanded states are not added to the unexpanded state list.
Which ONE of the following statements about breadth-first search (BFS) and depth-first search (DFS) is true, when reaching the goal state number 6?
- (A)BFS expands more states than DFS.
- (B)DFS expands more states than BFS.
- (C)Both BFS and DFS expand equal number of states.
- (D)Both BFS and DFS do not reach the goal state number 6.
BFS: expand 1 (adds 2, 3); expand 2 (adds 4; 3 already listed); expand 3 (adds 5); expand 4 (adds 6); expand 5 (adds 7); then 6 is reached. Expansion order 1, 2, 3, 4, 5. DFS with ascending order: expand 1 (2, 3), take the smallest, 2 (adds 4), then the smallest unexpanded 3 (adds 5), then 4 (adds 6), then 5, then 6. Because every new child is n+1 or n+2 and the smallest unexpanded number is always the next integer, both strategies expand states in the order 1, 2, 3, 4, 5 before the goal 6. They expand the same number of states. Answer: (C).
Concept: Here ascending-order tie-breaking makes BFS and DFS follow the identical sequence. The trap is assuming DFS dives deep along n+2 and so expands fewer states.
Consider the following sorting algorithms:
(i) Bubble sort
(ii) Insertion sort
(iii) Selection sort
Which ONE among the following choices of sorting algorithms sorts the numbers in the array [4, 3, 2, 1, 5] in increasing order after exactly two passes over the array?
- (A)(i) only
- (B)(iii) only
- (C)(i) and (iii) only
- (D)(ii) and (iii) only
Bubble sort: pass 1 gives [3, 2, 1, 4, 5], pass 2 gives [2, 1, 3, 4, 5]: not sorted. Insertion sort: pass 1 inserts 3 giving [3, 4, 2, 1, 5], pass 2 inserts 2 giving [2, 3, 4, 1, 5]: not sorted. Selection sort: pass 1 swaps the minimum 1 into position 1 giving [1, 3, 2, 4, 5], pass 2 swaps the minimum of the rest, 2, into position 2 giving [1, 2, 3, 4, 5]: sorted. Answer: (B) (iii) only.
Concept: Tracing what one pass of each elementary sort achieves. The trap is assuming bubble sort finishes quickly because 5 is already in place; it moves the small values left only one step per pass.
Consider the directed acyclic graph (DAG) with vertices P, Q, R, S, T, U, V and edges P → Q, R → Q, Q → S, Q → V, S → U, V → T.
Which of the following is/are valid vertex orderings that can be obtained from a topological sort of the DAG?
- (A)P Q R S T U V
- (B)P R Q V S U T
- (C)P Q R S V U T
- (D)P R Q S V T U
A valid order must put each edge's tail before its head: P and R before Q; Q before S and V; S before U; V before T. (A) Q comes before R: invalid (also T before V). (B) P, R, Q, then V, S, U, T: every constraint holds: valid. (C) Q before R: invalid. (D) P, R, Q, S, V, T, U: S before U, V before T: valid. Answer: (B), (D).
Concept: Topological order respects every edge direction. The trap is overlooking that Q has two predecessors, so both P and R must precede it.
Given an integer array of size N, we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by making a single pass through the array and comparing each element of the array only with its adjacent elements. The worst-case time complexity of this algorithm is
- (A)both Ο(N) and Ω(N)
- (B)Ο(N) but not Ω(N)
- (C)Ω(N) but not Ο(N)
- (D)neither Ο(N) nor Ω(N)
In the worst case (the array is sorted, or the violation is at the end) the single pass makes about N − 1 comparisons, each O(1). So worst-case time is Θ(N), which means it is both O(N) and Ω(N). Answer: (A).
Concept: Θ(N) implies both O(N) and Ω(N). The trap is associating Ω only with best-case analysis; here the bound is on the worst case.
Consider the following recurrence relation:
T(n) = √n · T(√n) + n for n ≥ 1,
T(n) = 1 for n = 1.
Which one of the following options is CORRECT?
- (A)T(n) = Θ(n log log n)
- (B)T(n) = Θ(n log n)
- (C)T(n) = Θ(n² log n)
- (D)T(n) = Θ(n² log log n)
Divide by n: T(n)/n = T(√n)/√n + 1. Let S(n) = T(n)/n, so S(n) = S(√n) + 1. Taking square roots repeatedly reaches a constant after log log n steps, so S(n) = Θ(log log n) and T(n) = Θ(n log log n). Answer: (A).
Concept: Normalise by n (or substitute n = 2^(2^k)) to turn the square-root recurrence into a simple count. The trap is treating it like T(n) = 2T(n/2) + n and answering n log n.
Let be a directed graph and a depth first search (DFS) spanning tree in that is rooted at a vertex . Suppose is also a breadth first search (BFS) tree in , rooted at . Which of the following statements is/are TRUE for every such graph and tree ?
- (A)There are no back-edges in with respect to the tree
- (B)There are no cross-edges in with respect to the tree
- (C)There are no forward-edges in with respect to the tree
- (D)The only edges in are the edges in
Forward edges: an edge u → w to a non-child descendant puts w at least two levels below u, but BFS from v would discover w at depth(u) + 1 via that edge, so T could not be a BFS tree. Hence no forward edges: (C) true. Back edges can exist (v → u and u → v). Cross edges can exist: with v → a, v → b, b → a, DFS visits a then b, and b → a is a cross edge, while BFS gives the same tree. So (A), (B), (D) fail. Answer: (C).
Concept: A BFS tree has no edge that skips a level downward. The trap is assuming the tree must contain every edge or that cross edges are impossible.
The number of edges present in the forest generated by the DFS traversal of an undirected graph with 100 vertices is 40. The number of connected components in is _________
DFS on an undirected graph gives a spanning forest with one tree per connected component. A forest with V vertices and c trees has V − c edges. So 100 − c = 40 and c = 60. Answer: 60.
Concept: Edges in a spanning forest = vertices − components. The trap is thinking DFS yields a single tree.
Let A be an array containing integer values. The distance of A is defined as the minimum number of elements in A that must be replaced with another integer so that the resulting array is sorted in non-decreasing order. The distance of the array [2, 5, 3, 1, 4, 2, 6] is ___________
The elements kept unchanged must already form a non-decreasing subsequence, and any such subsequence can be kept (integers can fill the gaps since values may repeat). So distance = n − length of the longest non-decreasing subsequence. Here 2, 3, 4, 6 (positions 1, 3, 5, 7) has length 4, and no length-5 non-decreasing subsequence exists. Distance = 7 − 4 = 3 (e.g. 2, 3, 3, 3, 4, 4, 6). Answer: 3.
Concept: Reduces to the longest non-decreasing subsequence. The trap is counting inversions or out-of-place elements instead.
Consider an array X that contains n positive integers. A subarray of X is defined to be a sequence of array locations with consecutive indices.
The C code snippet given below has been written to compute the length of the longest subarray of X that contains at most two distinct integers. The code has two missing expressions labelled (P) and (Q).
Which one of the following options gives the CORRECT missing expressions?
(Hint: At the end of the i-th iteration, the value of len1 is the length of the longest subarray ending with X[i] that contains all equal values, and len2 is the length of the longest subarray ending with X[i] that contains at most two distinct values.)
int first=0, second=0, len1=0, len2=0, maxlen=0;
for (int i=0; i < n; i++) {
if (X[i] == first) {
len2++; len1++;
} else if (X[i] == second) {
len2++;
len1 = (P);
second = first;
} else {
len2 = (Q);
len1 = 1; second = first;
}
if (len2 > maxlen) {
maxlen = len2;
}
first = X[i];
}- (A)(P) len1+1 (Q) len2+1
- (B)(P) 1 (Q) len1+1
- (C)(P) 1 (Q) len2+1
- (D)(P) len2+1 (Q) len1+1
first is the previous element and second the other value in the current two-value window. If X[i] equals second (not first), the window still has two values, so len2++; but the run of equal values ending at X[i] restarts, so len1 = 1 (P = 1). If X[i] is a new third value, the longest two-value subarray ending at X[i] is the run of the previous value (length len1) plus X[i]: len2 = len1 + 1 (Q). Answer: (B) (P) 1, (Q) len1+1.
Concept: Maintaining run-length invariants. When a new value appears, only the trailing run of the previous value can stay in the window - not the whole old window (len2+1 is the trap).
Let G be an undirected connected graph in which every edge has a positive integer weight. Suppose that every spanning tree in G has even weight. Which of the following statements is/are TRUE for every such graph G ?
- (A)All edges in G have even weight
- (B)All edges in G have even weight OR all edges in G have odd weight
- (C)In each cycle C in G, all edges in C have even weight
- (D)In each cycle C in G, either all edges in C have even weight OR all edges in C have odd weight
Take any cycle C and two edges e, f on it. Extend the path C − f to a spanning tree T (it contains e, not f). T − e + f is also a spanning tree, and both have even weight, so w(f) − w(e) is even: e and f have the same parity. Hence every cycle is all-even or all-odd: (D) true. Counterexamples: a triangle with weights 1, 1, 1 (every spanning tree weighs 2) breaks (A) and (C); a path (a tree) with weights 1, 1, 2 has its only spanning tree of weight 4, breaking (B). Answer: (D).
Concept: Spanning-tree exchange: swapping one cycle edge for another keeps a spanning tree, so cycle edges must share parity. Edges not on a common cycle (bridges) can differ.
The number of distinct minimum-weight spanning trees of the following graph is _________
Graph on vertices a, b, c, d, e, f, g with edges (weight in brackets): a-b (1), a-f (1), a-g (2), b-g (2), b-c (3), c-g (2), c-d (1), d-g (2), d-e (1), e-g (2), f-g (2), f-e (3).
Take all weight-1 edges (no cycle): a-b, a-f, c-d, d-e. Components: {a,b,f}, {c,d,e}, {g}. Two more edges are needed. The cheapest connections are weight 2, all through g: g to {a,b,f} via a-g, b-g or f-g (3 choices) and g to {c,d,e} via c-g, d-g or e-g (3 choices). The direct links between {a,b,f} and {c,d,e} (b-c, f-e) weigh 3, so they never appear. MST weight 4 + 4 = 8 and the count is 3 × 3 = 9. Answer: 9.
Concept: Counting MSTs with ties: contract the forced lighter edges, then multiply the number of equal-weight choices for each required connection.
An algorithm has to store several keys generated by an adversary in a hash table. The adversary is malicious who tries to maximize the number of collisions. Let k be the number of keys, m be the number of slots in the hash table, and k > m. Which one of the following is the best hashing strategy to counteract the adversary?
- (A)Division method, i.e., use the hash function h(k) = k mod m.
- (B)Multiplication method, i.e., use the hash function h(k) = m(kA - kA ) , where A is a carefully chosen constant.
- (C)Universal hashing method.
- (D)If k is a prime number, use Division method. Otherwise, use Multiplication method.
Any fixed hash function (division or multiplication method) is known to the adversary, who can then choose keys that all hash to the same slot, giving Θ(k) chains. Universal hashing picks the hash function at random from a universal family at run time, so for any fixed set of keys the expected number of collisions for a pair is at most 1/m, regardless of how the keys were chosen. Answer: (C) Universal hashing method.
Concept: Randomisation defeats a malicious input generator. The trap is believing a carefully chosen constant or a prime modulus helps; any deterministic function can be attacked.
Let f and g be functions of natural numbers given by f(n) = n and g(n) = n². Which of the following statements is/are TRUE?
- (A)f ∈ O(g)
- (B)f ∈ Ω(g)
- (C)f ∈ o(g)
- (D)f ∈ Θ(g)
n ≤ n² for all n ≥ 1, so f ∈ O(g). Moreover f(n)/g(n) = 1/n → 0, so f ∈ o(g) (strictly slower growth). Since the ratio tends to 0, there is no constant c > 0 with n ≥ c·n² for all large n, so f ∉ Ω(g) and therefore f ∉ Θ(g). Answer: (A) and (C).
Concept: Little-o means the ratio tends to 0, which implies big-O but rules out Ω and Θ. The trap is marking only O and forgetting that o also holds.
Consider functions Function 1 and Function 2 expressed in pseudocode as follows. Let f1(n) and f2(n) denote the number of times the statement "x = x + 1" is executed in Function 1 and Function 2, respectively. Which of the following statements is/are TRUE?
Function 1
while n > 1 do
for i = 1 to n do
x = x + 1;
end for
n = floor(n/2);
end while
Function 2
for i = 1 to 100 * n do
x = x + 1;
end for- (A)f1(n) ∈ Θ(f2(n))
- (B)f1(n) ∈ o(f2(n))
- (C)f1(n) ∈ ω(f2(n))
- (D)f1(n) ∈ O(n)
f1(n) = n + floor(n/2) + floor(n/4) + ... (while the value exceeds 1), which lies between n and 2n, so f1(n) = Θ(n). f2(n) = 100n = Θ(n). Hence f1 ∈ Θ(f2) and f1 ∈ O(n). Since the ratio f1/f2 stays between 1/100 and 1/50 (roughly), it neither tends to 0 nor to infinity, so o and ω are false. Answer: (A) and (D).
Concept: A geometric series n + n/2 + n/4 + ... sums to under 2n, so the halving loop is linear, not n log n. Constant factors like 100 do not affect Θ.
Let U = {1, 2, 3}. Let 2^U denote the powerset of U. Consider an undirected graph G whose vertex set is 2^U. For any A, B ∈ 2^U, (A, B) is an edge in G if and only if (i) A ≠ B, and (ii) either A ⊊ B or B ⊊ A. For any vertex A in G, the set of all possible orderings in which the vertices of G can be visited in a Breadth First Search (BFS) starting from A is denoted by B(A). If ∅ denotes the empty set, then the cardinality of B(∅) is ________.
G has 8 vertices. ∅ is a proper subset of every other subset, so ∅ is adjacent to all 7 other vertices. BFS from ∅ visits ∅ first and then discovers all 7 neighbours at level 1; they may be enqueued in any order, and once they are all discovered nothing else remains. So every ordering of the 7 non-empty subsets is a valid BFS order: 7! = 5040. Answer: 5040.
Concept: BFS order freedom comes from the order in which neighbours are enqueued. Because the start vertex is adjacent to everything, the count is simply 7!.
Which one of the following statements is TRUE for all positive functions f(n)?
- (A)f(n²) = θ(f(n)²), when f(n) is a polynomial
- (B)f(n²) = o(f(n)²)
- (C)f(n²) = O(f(n)²), when f(n) is an exponential function
- (D)f(n²) = Ω(f(n)²)
Polynomial f(n) = a·nᵏ + lower terms: f(n²) ~ a·n²ᵏ and f(n)² ~ a²·n²ᵏ, same order, so (A) holds. Exponential f(n) = 2ⁿ: f(n²) = 2^(n²) while f(n)² = 2^(2n), so f(n²) is not O(f(n)²) - (C) fails, and this also kills (B). For f(n) = log n: f(n²) = 2 log n while f(n)² = log²n, so f(n²) is not Ω(f(n)²) - (D) fails. Answer: (A).
Concept: Test each claim with a polynomial, an exponential and a slow function like log n. The trap is (D): squaring the input grows polynomials and exponentials faster, but not logarithms.
Suppose we are given n keys, m hash table slots, and two simple uniform hash functions h₁ and h₂. Further suppose our hashing scheme uses h₁ for the odd keys and h₂ for the even keys. What is the expected number of keys in a slot?
- (A)m/n
- (B)n/m
- (C)2n/m
- (D)n/(2m)
Each key, whether hashed by h₁ or h₂, lands in any particular slot with probability 1/m because both functions are simple uniform. By linearity of expectation, the expected number of keys in a slot is n × (1/m) = n/m. Splitting keys between two uniform functions changes nothing. Answer: (B) n/m.
Concept: Load factor α = n/m under simple uniform hashing. The trap is thinking two functions halve or double the load.
Consider a simple undirected weighted graph G, all of whose edge weights are distinct. Which of the following statements about the minimum spanning trees of G is/are TRUE?
- (A)The edge with the second smallest weight is always part of any minimum spanning tree of G.
- (B)One or both of the edges with the third smallest and the fourth smallest weights are part of any minimum spanning tree of G.
- (C)Suppose S ⊆ V be such that S ≠ ∅ and S ≠ V. Consider the edge with the minimum weight such that one of its vertices is in S and the other in V \ S. Such an edge will always be part of any minimum spanning tree of G.
- (D)G can have multiple minimum spanning trees.
Think of Kruskal. (A) The two lightest edges can never form a cycle, so both are picked: true. (B) The third edge is rejected only if edges 1, 2, 3 form a triangle. Then the fourth edge would be rejected only if it closed a cycle with edges 1 and 2, i.e. joined the same pair as edge 3, impossible in a simple graph. So at least one of them is picked: true. (C) Cut property: with distinct weights, the lightest edge crossing any cut is in every MST: true. (D) Distinct weights give a unique MST: false. Answer: (A), (B), (C).
Concept: Kruskal's order and the cut property. The trap is (D): multiple MSTs need tied weights.
Let G be a simple undirected graph. Let T_D be a depth first search tree of G. Let T_B be a breadth first search tree of G. Consider the following statements.
(I) No edge of G is a cross edge with respect to T_D. (A cross edge in G is between two nodes neither of which is an ancestor of the other in T_D.)
(II) For every edge (u,v) of G, if u is at depth i and v is at depth j in T_B, then |i - j| = 1.
Which of the statements above must necessarily be true?
- (A)I only
- (B)II only
- (C)Both I and II
- (D)Neither I nor II
(I) In an undirected DFS, when an edge (u, v) is examined from u and v is unvisited, v becomes a descendant of u; if v is already visited it must be an ancestor or descendant, since u's subtree is finished before DFS backs out. So every non-tree edge is a back edge and there are no cross edges: true. (II) In BFS, edge endpoints differ in depth by at most 1, but can be at the same depth: in a triangle x-y-z rooted at x, y and z are both at depth 1 and the edge y-z has |i - j| = 0. False. Answer: (A) I only.
Concept: Undirected DFS has only tree and back edges; BFS non-tree edges join the same or adjacent levels. The trap is reading BFS's "at most 1" as "exactly 1".
Assume that multiplying a matrix G₁ of dimension p × q with another matrix G₂ of dimension q × r requires pqr scalar multiplications. Computing the product of n matrices G₁G₂G₃...Gₙ can be done by parenthesizing in different ways. Define GᵢGᵢ₊₁ as an explicitly computed pair for a given paranthesization if they are directly multiplied. For example, in the matrix multiplication chain G₁G₂G₃G₄G₅G₆ using parenthesization (G₁(G₂G₃))(G₄(G₅G₆)), G₂G₃ and G₅G₆ are the only explicitly computed pairs.
Consider a matrix multiplication chain F₁F₂F₃F₄F₅, where matrices F₁, F₂, F₃, F₄ and F₅ are of dimensions 2×25, 25×3, 3×16, 16×1 and 1×1000, respectively. In the parenthesization of F₁F₂F₃F₄F₅ that minimizes the total number of scalar multiplications, the explicitly computed pairs is/are
- (A)F₁F₂ and F₃F₄ only
- (B)F₂F₃ only
- (C)F₃F₄ only
- (D)F₁F₂ and F₄F₅ only
Dimensions p = 2, 25, 3, 16, 1, 1000. Pairs: m12 = 150, m23 = 1200, m34 = 48, m45 = 16000. m13 = min(150 + 96, 1200 + 800) = 246. m24 = min(48 + 75, 1200 + 400) = 123, via F₂(F₃F₄). m35 = min(16000 + 48000, 48 + 3000) = 3048. m14 = min(123 + 50, 150 + 48 + 6, 246 + 32) = 173, via F₁(F₂(F₃F₄)). m15: split after F₄ gives 173 + 2·1·1000 = 2173; after F₂ gives 150 + 3048 + 6000 = 9198; others are larger. Optimum ((F₁(F₂(F₃F₄)))F₅) = 2173. The only directly multiplied adjacent pair is F₃F₄. Answer: (C).
Concept: Matrix chain multiplication DP. The 16×1 and 1×1000 shapes make it cheap to collapse everything to a 2×1 matrix first, then multiply by F₅ once. The trap is greedily multiplying the cheapest-looking pairs, such as F₁F₂.
Consider the following undirected graph G:
(Graph, described: five vertices T, A, B, C, R. Edges with weights: T-A = 4, A-B = 4, T-B = 1, T-C = 3, B-C = 4, T-R = x, C-R = 5.)
Choose a value for x that will maximize the number of minimum weight spanning trees (MWSTs) of G. The number of MWSTs of G for this value of x is ______.
Edges T-B (1) and T-C (3) are the two cheapest and form no cycle, so every MST contains them. Vertex A joins through T-A or A-B, both weight 4, giving 2 choices (B-C, also 4, would close the cycle T-B-C and is never needed). Vertex R joins only through T-R (x) or C-R (5). If x = 5 both are equally cheap, giving 2 choices; any other x gives 1. Maximum number of MSTs = 2 × 2 = 4 with x = 5. Answer: 4.
Concept: Multiple MSTs arise from ties among the cheapest edges crossing a cut. Choosing x equal to the competing edge (5) creates a tie for R. The trap is counting B-C as a third option for A; it never crosses the cut that separates A.
Consider the weights and values of items listed below. Note that there is only one unit of each item.
Item number Weight (in Kgs) Value (in Rupees)
1 10 60
2 7 28
3 4 20
4 2 24
The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by V_opt. A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by V_greedy.
The value of V_opt - V_greedy is ____________.
Ratios: item 4 = 12, item 1 = 6, item 3 = 5, item 2 = 4. Greedy takes item 4 (2 kg, 24); item 1 (10 kg) would make 12 kg, so it is skipped; item 3 (4 kg) fits, total 6 kg, 44; item 2 (7 kg) would make 13 kg, skipped. V_greedy = 44. Optimal: item 1 alone gives 60; no other item fits with it (10 + 2 = 12). Other subsets: 2 + 4 = 52, 2 + 3 = 48, 3 + 4 = 44. V_opt = 60. Difference = 60 - 44 = 16. Answer: 16.
Concept: Greedy by value/weight is optimal only for fractional knapsack; 0/1 knapsack needs DP or exhaustive search. The trap is letting greedy stop at the first item that does not fit; it continues down the list.
Consider the following directed graph with vertices a, b, c, d, e, f and edges a → b, b → c, c → f, a → d, d → e, e → f.
The number of different topological orderings of the vertices of the graph is ________.
a is the only source, so it comes first; f is the only sink, so it comes last. In between, b must precede c and d must precede e, with no constraint between the two chains. The number of interleavings of two chains of length 2 is C(4, 2) = 6. Answer: 6.
Concept: Counting topological orders as interleavings of independent chains. The trap is listing orders by hand and missing some, or treating b, c, d, e as freely permutable (24).
The worst case running times of Insertion sort, Merge sort and Quick sort, respectively, are:
- (A)Θ(n log n), Θ(n log n), and Θ(n²)
- (B)Θ(n²), Θ(n²), and Θ(n log n)
- (C)Θ(n²), Θ(n log n), and Θ(n log n)
- (D)Θ(n²), Θ(n log n), and Θ(n²)
Insertion sort: a reverse-sorted input forces i shifts for the i-th element, Θ(n²). Merge sort always splits in half and merges in linear time, Θ(n log n) in every case. Quick sort: with consistently bad pivots (e.g. sorted input with first-element pivot) partitions are of size n-1 and 0, Θ(n²). Answer: (D).
Concept: Worst-case complexities of the standard sorts. The trap is quoting quick sort's average Θ(n log n) as its worst case.
Let G be a weighted connected undirected graph with distinct positive edge weights. If every edge weight is increased by the same value, then which of the following statements is/are TRUE?
P: Minimum spanning tree of G does not change
Q: Shortest path between any pair of vertices does not change
- (A)P only
- (B)Q only
- (C)Neither P nor Q
- (D)Both P and Q
P: Every spanning tree has exactly V - 1 edges, so adding c to each edge adds (V - 1)c to every tree; the relative order of edge weights (which Kruskal uses) is unchanged. The MST stays the same. P is true.
Q: Paths have different numbers of edges, so a path with k edges increases by kc. Example: A-B weight 3 directly vs A-C-B with weights 1 + 1 = 2. Adding 2 to each edge gives 5 vs 6, so the shortest path changes. Q is false. Answer: (A) P only.
Concept: MSTs depend only on the order of weights; shortest paths depend on sums over paths of different lengths. The trap is assuming both are invariant.
Consider the weighted undirected graph with 4 vertices, where the weight of edge {i, j} is given by the entry Wᵢⱼ in the matrix W.
0 2 8 5
W = 2 0 5 8
8 5 0 x
5 8 x 0
The largest possible integer value of x, for which at least one shortest path between some pair of vertices will contain the edge with weight x is ________.
Edges: {1,2} = 2, {1,3} = 8, {1,4} = 5, {2,3} = 5, {2,4} = 8, {3,4} = x.
The edge {3,4} is most useful for the pair (3, 4). Without it, the shortest 3-4 route is 3-2-1-4 = 5 + 2 + 5 = 12 (3-1-4 and 3-2-4 cost 13). So the direct edge lies on a shortest path whenever x ≤ 12 (at x = 12 it ties, and 'at least one shortest path' still includes it). For other pairs the edge would need a much smaller x (e.g. for (1,3), 5 + x ≤ 7 needs x ≤ 2). Largest integer x = 12. Answer: 12.
Concept: An edge lies on some shortest path iff its weight does not exceed the best alternative between its endpoints. The trap is using strict inequality and answering 11.
Let G be a complete undirected graph on 4 vertices, having 6 edges with weights being 1, 2, 3, 4, 5, and 6. The maximum possible weight that a minimum weight spanning tree of G can have is ________.
Kruskal always takes edges 1 and 2 (two edges cannot form a cycle). To make the MST heavy, edge 3 should close a cycle: place edges 1, 2, 3 as a triangle on three vertices, so edge 3 is rejected. The fourth vertex is joined by edges 4, 5, 6, and Kruskal takes 4. MST weight = 1 + 2 + 4 = 7. If edges 1 and 2 do not form a path with 3 completing a triangle, edge 3 is accepted and the weight is only 1 + 2 + 3 = 6. Maximum = 7. Answer: 7.
Concept: Adversarial edge placement against Kruskal's algorithm. The trap is answering 6 + 5 + 4 or ignoring that edges 1 and 2 are always chosen.
G = (V, E) is an undirected simple graph in which each edge has a distinct weight, and e is a particular edge of G. Which of the following statements about the minimum spanning trees (MSTs) of G is/are TRUE?
I. If e is the lightest edge of some cycle in G, then every MST of G includes e
II. If e is the heaviest edge of some cycle in G, then every MST of G excludes e
- (A)I only
- (B)II only
- (C)both I and II
- (D)neither I nor II
II is the cycle property: if e is the unique heaviest edge on a cycle and some spanning tree contains e, removing e and adding another edge of that cycle reconnecting the tree gives a lighter spanning tree. So no MST contains e. II is true.
I is false: an edge can be the lightest on one cycle and the heaviest on another. Take vertices X, Y, Z, W with XY = 1, YZ = 2, XZ = 3, ZW = 4, WX = 5. XZ = 3 is the lightest edge of cycle X-Z-W, yet it is the heaviest edge of triangle X-Y-Z, so by the cycle property no MST contains it (the MST is XY, YZ, ZW). Answer: (B) II only.
Concept: Cycle property: the unique heaviest edge of any cycle is in no MST. The trap is believing the converse for the lightest edge; an edge can be lightest on one cycle and heaviest on another.
The Floyd-Warshall algorithm for all-pair shortest paths computation is based on
- (A)Greedy paradigm.
- (B)Divide-and-Conquer paradigm.
- (C)Dynamic Programming paradigm.
- (D)neither Greedy nor Divide-and-Conquer nor Dynamic Programming paradigm.
Floyd-Warshall computes d_k(i,j), the shortest path using only intermediate vertices from {1..k}, from the k - 1 table: d_k(i,j) = min(d_(k-1)(i,j), d_(k-1)(i,k) + d_(k-1)(k,j)). Overlapping subproblems solved bottom-up in a table is dynamic programming. Answer: (C) Dynamic Programming paradigm.
Concept: Floyd-Warshall and Bellman-Ford are DP; Dijkstra and Prim are greedy. The recurrence over growing sets of allowed intermediate vertices is the giveaway.
Let A₁, A₂, A₃, and A₄ be four matrices of dimensions 10 × 5, 5 × 20, 20 × 10, and 10 × 5, respectively. The minimum number of scalar multiplications required to find the product A₁A₂A₃A₄ using the basic matrix multiplication method is ______.
Dimensions p = 10, 5, 20, 10, 5. Pairs: A₁A₂ = 1000, A₂A₃ = 1000, A₃A₄ = 1000. Triples: A₁..A₃ = min(1000 + 10·20·10, 1000 + 10·5·10) = 1500; A₂..A₄ = min(1000 + 5·10·5, 1000 + 5·20·5) = 1250. Full: split after A₁: 1250 + 10·5·5 = 1500; after A₂: 1000 + 1000 + 10·20·5 = 3000; after A₃: 1500 + 10·10·5 = 2000. Minimum 1500, from A₁((A₂A₃)A₄). Answer: 1500.
Concept: Matrix chain multiplication DP: m[i][j] = min over k of m[i][k] + m[k+1][j] + pᵢ₋₁pₖpⱼ. Greedy left-to-right order is not optimal.
The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst case time complexity of this function is O(n^α), then the least possible value (accurate up to two decimal positions) of α is ______.
Flowchart (described in words): Start, then call A(n/2), then decision 1.
- Decision 1, one branch: call A(n/2), then Return.
- Decision 1, other branch: call A(n/2), call A(n/2), then decision 2.
- Decision 2, one branch: Return.
- Decision 2, other branch: call A(n/2), then decision 3.
- Decision 3, one branch: call A(n/2), then Return.
- Decision 3, other branch: Return.
Count recursive calls on the longest path: 1 (before decision 1) + 2 (decision 1 right branch) + 1 (after decision 2) + 1 (decision 3 call) = 5 calls of A(n/2). Worst case T(n) = 5T(n/2) + O(1). By the master theorem T(n) = Θ(n^(log₂ 5)), and log₂ 5 ≈ 2.32. Answer: 2.32 (accepted range 2.2 to 2.4).
Concept: Worst-case path through branching code determines the recurrence. With a = 5, b = 2 and constant extra work, the master theorem gives n^(log₂ 5). Counting all calls in the chart (6) instead of one path is the trap.
Let G be a graph with n vertices and m edges. What is the tightest upper bound on the running time of Depth First Search on G, when G is represented as an adjacency matrix?
- (A)Θ(n)
- (B)Θ(n + m)
- (C)Θ(n²)
- (D)Θ(m²)
DFS visits each vertex once. When a vertex is visited, finding its neighbours in an adjacency matrix means scanning its entire row of n entries, regardless of how many edges it actually has. n vertices × n entries = Θ(n²). Answer: (C) Θ(n²).
Concept: The cost of DFS/BFS depends on the representation: Θ(n + m) with adjacency lists, Θ(n²) with an adjacency matrix. The trap is quoting Θ(n + m) out of habit.
Consider the directed graph given below. It has vertices P, Q, R, S and the directed edges P→Q, P→R, R→Q, S→R and S→Q.
Which one of the following is TRUE?
- (A)The graph does not have any topological ordering.
- (B)Both PQRS and SRQP are topological orderings.
- (C)Both PSRQ and SPRQ are topological orderings.
- (D)PSRQ is the only topological ordering.
The graph has no cycle, so a topological order exists. P and S have no incoming edges. R has incoming edges from P and S only. Q has incoming edges from P, R and S.
So P and S must come first (in either order), then R, then Q. The valid orders are PSRQ and SPRQ.
PQRS fails (Q before R), SRQP fails (P last), and PSRQ is not unique. Answer: (C).
Concept: A topological order places every vertex after all its predecessors; when two sources are independent, both orders are valid. The trap is assuming the order is unique.
The minimum number of arithmetic operations required to evaluate the polynomial P(X) = X⁵ + 4X³ + 6X + 5 for a given value of X, using only one temporary variable is ______.
Rewrite in nested (Horner-like) form: P(X) = ((X² + 4)·X² + 6)·X + 5.
With one temporary t:
t = X * X (1)
t = t + 4 (2)
t = t * X (3)
t = t * X (4) now t = X⁴ + 4X²
t = t + 6 (5)
t = t * X (6)
t = t + 5 (7)
Seven operations. Fewer is not possible with one temporary, since X² cannot be kept while t is reused. Answer: 7.
Concept: Horner-style factoring to minimise operations. The trap is computing X² once and reusing it, which needs a second temporary (giving 6 with two variables).
Suppose depth first search is executed on the graph below starting at some unknown vertex. Assume that a recursive call to visit a vertex is made only after first checking that the vertex has not been visited earlier. Then the maximum possible recursion depth (including the initial call) is _________.
The graph: a left block that is a 3 × 3 grid of vertices L(r,c), r, c ∈ {1, 2, 3} with row 1 on top, each vertex adjacent to its horizontal and vertical neighbours; a right block that is an identical 3 × 3 grid R(r,c). A vertex X is adjacent to L(3,3) and R(3,1). A vertex Y is adjacent only to X. A vertex Z is adjacent only to R(2,3). (21 vertices in all.)
Recursion depth = number of vertices on the DFS stack = length of a simple path DFS can follow, and DFS can be steered along any simple path. So find the longest simple path.
Colour each 3 × 3 grid like a chessboard: corners and centre (5) versus edge-middles (4). A path through all 9 vertices must start and end on corners/centre.
Path: snake through all 9 of the left grid ending at corner L(3,3) (9), go to X (10), enter R(3,1), a corner, and snake through all 9 of the right grid ending at a corner (19).
To also reach Z the path would have to end the right grid at R(2,3), an edge-middle, which forces skipping a grid vertex, so still 19. Y is a dead end beside X. Answer: 19.
Concept: DFS recursion depth equals the longest simple path DFS can be steered along. The trap is counting all 21 vertices, ignoring the pendant vertices and the grid parity constraint.
You have an array of n elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is
- (A)O(n²)
- (B)O(n log n)
- (C)Θ(n log n)
- (D)O(n³)
Any fixed pivot rule (first, last or middle element) has an adversarial input where the chosen pivot is always the smallest or largest remaining element. Each partition then removes only one element, giving n + (n - 1) + ... = Θ(n²) comparisons. Choosing the middle position is not the same as choosing the median value. Answer: (A) O(n²).
Concept: Worst-case quicksort with a deterministic pivot position is quadratic. The trap is confusing 'central element' with the median, which would give O(n log n).
Suppose you want to move from 0 to 100 on the number line. In each step, you either move right by a unit distance or you take a shortcut. A shortcut is simply a pre-specified pair of integers i, j with i < j. Given a shortcut i, j if you are at position i on the number line, you may directly move to j. Suppose T(k) denotes the smallest number of steps needed to move from k to 100. Suppose further that there is at most 1 shortcut involving any number, and in particular from 9 there is a shortcut to 15. Let y and z be such that T(9) = 1 + min(T(y), T(z)). Then the value of the product yz is _____.
From position 9 there are exactly two possible moves: a unit step to 10, or the shortcut to 15 (9 is involved in only this one shortcut). Each costs one step, so T(9) = 1 + min(T(10), T(15)). Hence {y, z} = {10, 15} and yz = 150. Answer: 150.
Concept: Writing the DP recurrence from the available transitions. The trap is overthinking other shortcuts; 'at most one shortcut involving any number' rules them out at 9.
Consider a hash table with 100 slots. Collisions are resolved using chaining. Assuming simple uniform hashing, what is the probability that the first 3 slots are unfilled after the first 3 insertions?
- (A)(97 × 97 × 97)/100³
- (B)(99 × 98 × 97)/100³
- (C)(97 × 96 × 95)/100³
- (D)(97 × 96 × 95)/(3! × 100³)
With simple uniform hashing each key independently lands in any of the 100 slots with equal probability, and chaining lets several keys share a slot. For the first 3 slots to stay empty each insertion must land in one of the other 97 slots: probability 97/100 each, independently. Total = (97 × 97 × 97)/100³. Answer: (A).
Concept: Independence of insertions under uniform hashing with chaining. The trap is option C, which assumes later keys cannot reuse an occupied slot (as with open addressing).
Consider the C function given below. Assume that the array listA contains n (> 0) elements, sorted in ascending order.
Which one of the following statements about the function ProcessArray is CORRECT?
int ProcessArray(int *listA, int x, int n)
{
int i, j, k;
i = 0;
j = n-1;
do {
k = (i+j)/2;
if (x <= listA[k])
j = k-1;
if (listA[k] <= x)
i = k+1;
}while (i <= j);
if (listA[k] == x)
return(k);
else
return -1;
}- (A)It will run into an infinite loop when x is not in listA.
- (B)It is an implementation of binary search.
- (C)It will always find the maximum element in listA.
- (D)It will return -1 even when x is present in listA.
Each iteration takes the midpoint k. If x < listA[k] only j = k - 1 runs; if x > listA[k] only i = k + 1 runs; the range shrinks every time, so the loop terminates (A false).
If x == listA[k], both conditions hold: j = k - 1 and i = k + 1, so i > j and the loop exits with k pointing at x, and the function returns k. So it finds x when present (D false) and returns -1 otherwise.
This is binary search; it searches for x, not the maximum (C false). Answer: (B).
Concept: Tracing a binary search with two non-exclusive ifs. The trap is thinking the equality case causes an extra step that loses k; both updates fire together and end the loop at the right index.