Practice tests · GATE
GATE algorithms: 5 practice questions with solutions
On this page
Algorithms is a dedicated section in the GATE CS paper and part of "Programming, Data Structures and Algorithms" in DA. The questions ask you to do three things: find the time complexity of a recurrence or a loop, run a standard algorithm by hand on a small input, and pick the right design technique. The five original questions below practise all three.
What GATE tests in algorithms
The standard content is asymptotic analysis, searching and sorting, divide and conquer, greedy methods, dynamic programming and graph algorithms (traversals, minimum spanning trees, shortest paths). The CS and DA syllabi were revised for 2027, so check your paper's list on the official syllabus page. Expect "what is the complexity of this recurrence", "what does Dijkstra's algorithm output on this graph", "what is the optimal value for this knapsack or LCS instance" and "which problem does a greedy algorithm solve". NAT questions need exact numbers, so hand-simulation must be accurate. All five below are MCQ.
Practice questions
Question 1
The running time T(n) of an algorithm satisfies T(n) = 3T(n/2) + n², with T(1) = 1. Then T(n) is:
- A. Θ(n^(log₂ 3))
- B. Θ(n² log n)
- C. Θ(n³)
- D. Θ(n²)
Question 2
An undirected graph has vertices A, B, C, D, E and edges with weights: A–B 4, A–C 2, B–C 1, B–D 5, C–D 8, C–E 10, D–E 3. The length of the shortest path from A to E is:
- A. 10
- B. 11
- C. 12
- D. 13
Question 3
In a 0/1 knapsack problem the capacity is 10 and the items, given as (weight, value), are (3, 25), (4, 30), (5, 40) and (6, 45). The maximum total value that can be carried is:
- A. 65
- B. 70
- C. 75
- D. 80
Question 4
The minimum number of comparisons needed in the worst case to find both the largest and the smallest element of an unsorted array of 8 distinct elements is:
- A. 10
- B. 11
- C. 12
- D. 14
Question 5
What is the time complexity of the following code in terms of n? for (i = 1; i <= n; i = i * 2) for (j = 0; j < i; j++) count++;
- A. Θ(n log n)
- B. Θ(n²)
- C. Θ(log² n)
- D. Θ(n)
Answers and explanations
- D. Apply the master theorem with a = 3, b = 2. Then n^(log₂ 3) is about n^1.585, which grows slower than f(n) = n². Since f(n) is polynomially larger and the regularity condition holds (3(n/2)² = 3n²/4 ≤ cn² with c = 3/4), T(n) = Θ(f(n)) = Θ(n²).
- B. Run Dijkstra from A. dist(C) = 2. From C, dist(B) = 2 + 1 = 3. From B, dist(D) = 3 + 5 = 8 (the direct C–D edge would give 2 + 8 = 10). From D, dist(E) = 8 + 3 = 11, which beats the direct C–E route of 2 + 10 = 12. The path A, C, B, D, E has length 2 + 1 + 5 + 3 = 11.
- C. Items 1 and 3 together (3 + 5 = 8, value 65) are what a greedy choice by value-to-weight ratio picks first, but nothing else fits. The best subset is items 2 and 4: weight 4 + 6 = 10, value 30 + 45 = 75. Dynamic programming over capacity confirms 75, and it shows why greedy fails for 0/1 knapsack.
- A. Compare the elements in pairs: 4 comparisons for 8 elements. Compare the 4 winners among themselves to find the maximum (3 comparisons) and the 4 losers to find the minimum (3 comparisons). The total is 4 + 3 + 3 = 10, which equals ⌈3n/2⌉ − 2 for n = 8. The naive method needs 2(n − 1) = 14.
- D. The inner loop runs i times, and i takes the values 1, 2, 4, …, up to n. The total is 1 + 2 + 4 + … + 2^k with 2^k about n, which is about 2n. This is Θ(n), not Θ(n log n): the loop counter doubles, so the work forms a geometric series dominated by its last term.
How to prepare algorithms
- Learn the master theorem cases and one fallback. The theorem covers T(n) = aT(n/b) + f(n); for anything else (T(n) = T(n − 1) + n, or unequal splits), expand the recursion or draw the tree.
- Know the common loop patterns. A doubling or halving counter gives log n; nested loops with a dependent inner bound need a sum; a geometric sum is dominated by its largest term (Question 5).
- Hand-simulate each classical algorithm once on a 5 to 7 node graph: BFS, DFS, Dijkstra, Prim, Kruskal, Bellman-Ford and Floyd-Warshall. Then practise on past-paper graphs until your tables are always right.
- For dynamic programming, set up the table first. Know LCS, edit distance, 0/1 knapsack, matrix chain multiplication and longest increasing subsequence with their recurrences.
- Memorise the bounds and the exceptions: quicksort O(n log n) average and O(n²) worst, the Ω(n log n) lower bound for comparison sorts, fractional knapsack is greedy but 0/1 is not, and Dijkstra needs non-negative weights.
Algorithms and data structures feed each other, so alternate between them. The GATE CSE preparation guide shows where this section sits in the CS plan, and theory of computation is a good next step.
Take the full topic test on PrepMatic
PrepMatic's topic tests run about 15 questions in 30 minutes, so you can practise hand-simulation under realistic time pressure. The free assessment shows your weakest subjects first.
Turn the GATE syllabus into a plan you can finish
Take a free GATE assessment on PrepMatic. It shows where you stand in your branch, turns the syllabus into a plan around the hours you actually have, and builds mock tests focused on your weak subjects.
Start free assessment →Free forever plan · No credit card · 2-minute setupRelated
- GATE data structures practiceFive GATE-style data structures MCQs on postfix evaluation, BST deletion, heap insertion, C pointer arithmetic and hashing, with traced solutions for CS and DA.
- GATE theory of computation practiceFive GATE-style TOC MCQs on DFA state counts, regular languages, context-free grammars, undecidability and closure properties, with proofs and explanations.
- GATE DBMS practiceFive GATE-style database MCQs on candidate keys, normal forms, SQL NULL handling, conflict serializability and foreign-key joins, with worked answers and tips.
- GATE CSE preparationPlan GATE CS 2027: the revised syllabus section by section, the order to study the nine core subjects, standard books and a 26-week plan from first pass to mocks.
- GATE Data Science and AIThe GATE DA 2027 paper: seven syllabus sections for 85 marks, who should pick DA over CS, Python and maths priorities, books, and a six-month order of study.
More in GATE
- GATE analog circuits practiceFive GATE-style analog circuit MCQs on op-amp summing, BJT bias, MOSFET transconductance, negative feedback gain and Zener regulation, with worked solutions.
- GATE analytical aptitude practiceFive GATE-style reasoning MCQs on deduction, number series, seating arrangement, clocks and 3D cube counting, with step-by-step answers and methods.
- GATE calculus practiceFive GATE-level calculus MCQs on limits, maxima and minima, the mean value theorem, definite integrals and critical points, with full worked solutions.
- GATE communications practiceFive GATE EC communications MCQs on AM power, Shannon capacity, PCM bit rate, QAM bandwidth and Hamming code error correction, with complete worked solutions.
- GATE compiler design practiceFive GATE-style compiler design MCQs on FOLLOW sets, LR(0) states, expression DAGs, basic blocks and lexical tokens, each with a full step-by-step explanation.
- GATE computer networks practiceFive GATE-style computer networks MCQs on subnetting, CSMA/CD frame size, Go-Back-N, TCP congestion control and IP fragmentation, with step-by-step solutions.
- GATE computer organization practiceFive GATE-style computer organization MCQs on cache tag bits, pipeline CPI, expanding opcodes, DMA cycle stealing and memory access time, with worked answers.
- GATE control systems practiceFive GATE-style control systems MCQs on damping ratio, Routh stability, steady-state error, root locus breakaway points and gain margin for EE and EC.