GATE CS Programming & Data Structures Previous Year Questions

60 solved GATE CS questions on Programming & Data Structures, drawn from 4 exam years and grouped by year. Every question shows the official answer and a step-by-step solution.

Want to practise this topic and ask follow-up questions? Explore Success Tracker.

Revision companion

Programming & Data Structures: trace execution, then generalize

Simulate the code on small inputs before reasoning about general behavior. These selected foundations connect C-language constructs, pointers, recursion, and fundamental data structures. Each structure has insertion, deletion, and search costs that depend on its invariants.

Our study notes and original examples support the PYQs below; they are not official exam questions or a replacement for the current syllabus.

Before you start

  • Variables, assignments, loops, and conditional statements in an imperative language.
  • Basic understanding of functions, return values, and array indexing starting from 0.

Concepts to revise before solving

Pointers and memory layout

A pointer holds an address. Dereferencing accesses the value at that address. Pointer arithmetic on an array of type T advances by sizeof(T) bytes per unit. A null pointer dereference is undefined behavior. Pass-by-pointer lets a function modify the caller's variable.

Check yourself: What does the pointer point to after the assignment, and is it still valid?

Recursion and the call stack

Each recursive call creates a new stack frame with its own local variables. Identify the base case, the recursive reduction, and what each call returns. The total number of frames determines stack depth. Tail recursion reuses frames only when the compiler optimizes it.

Check yourself: Does every recursive path eventually reach the base case?

Linked lists, stacks, and queues

A singly linked list supports O(1) insertion at the head but O(n) search. A stack is last-in-first-out; a queue is first-in-first-out. Both can be implemented with arrays or linked nodes. Array-based versions may need resizing; linked versions use extra pointer storage.

Check yourself: Are you updating both the pointer and the node it references?

Trees and binary search trees

In a binary search tree, left descendants have smaller keys and right descendants have larger keys. Search, insert, and delete are O(h) where h is the height. A balanced BST maintains h = O(log n); an unbalanced one can degrade to O(n). Inorder traversal of a BST yields sorted order.

Check yourself: Does the BST property hold at every node after your operation?

Hashing

A hash table maps keys to slots via a hash function. Collisions are unavoidable when the key space exceeds the table size. Chaining uses linked lists per slot; open addressing probes for the next available slot. Average-case O(1) lookup depends on a good hash function and a reasonable load factor.

Check yourself: What happens when two different keys hash to the same slot?

Mistakes to avoid

Incrementing a pointer and assuming it moves by one byte regardless of type.
Pointer arithmetic advances by sizeof(pointed-to type); an int pointer moves by sizeof(int) bytes.
Forgetting to update the head pointer when deleting the first node of a linked list.
After freeing the old head, assign head to the next node; otherwise the list loses its entry point.
Assuming a BST is always balanced.
Inserting sorted data into a plain BST creates a degenerate chain. Balanced variants (AVL, red-black) enforce height constraints.

Original teaching example · not a PYQ

Work through the reasoning

Original mini-example: trace the output of this C-like function call foo(3), where foo(n) prints n, then if n > 1 calls foo(n−1), then prints n again.

  1. foo(3): prints 3, then calls foo(2).
  2. foo(2): prints 2, then calls foo(1).
  3. foo(1): prints 1, base condition n > 1 is false, prints 1 again.
  4. Returning: foo(2) prints 2 again. foo(3) prints 3 again.

Output: 3 2 1 1 2 3.

Try it before reading the answer

Insert keys 5, 3, 7, 2, 4 in order into an initially empty binary search tree (no rebalancing). What is the inorder traversal?

Show answer and reasoning

2 3 4 5 7.

5 becomes the root. 3 goes left of 5, 7 goes right. 2 goes left of 3, 4 goes right of 3. Inorder (left-root-right) visits nodes in sorted order: 2, 3, 4, 5, 7.

Go deeper with free learning resources

Supplemental reading, not an official GATE reading list or an endorsement of these notes.

Apply this to the previous-year questions

Previous-year questions by year

This page shows 60 recent questions from the released archive, newest first. For older questions and complete papers, browse all GATE CS papers. Questions can carry more than one subject tag; counts are not marks weightage.

GATE CS 202619 questions

  1. Set 1 Q23Let nn be an odd number greater than 100. Consider a binary minheap with nn elements stored in an array PP whose index starts from 1. Which of the following…MSQ · +1 marks · Medium
  2. Set 1 Q24Consider a hash table P[0,1,…,10]P[0, 1, \dots, 10] that is initially empty. The hash table is maintained using open addressing with linear probing. The hash function…MSQ · +1 marks · Medium
  3. Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]MSQ · +1 marks · Medium
  4. Set 1 Q33The height of a binary tree is the number of edges in the longest path from the root to a leaf in the tree. The maximum possible height of a full binary tree…NAT · +1 marks · Medium
  5. Set 1 Q34Consider the following program in C: [code] The output of the program is _________. (answer in integer) Note: Assume that the program compiles and runs…NAT · +1 marks · Medium
  6. Set 1 Q39Consider the following code snippet in C language that computes the number of nodes in a non-empty singly linked list pointed to by the pointer variable…MCQ · +2 marks · Easy
  7. Set 1 Q40Let PP be the set of all integers from 1 to 15. Consider any order of insertion of the elements of PP into a binary search tree that creates a complete…MCQ · +2 marks · Hard
  8. Set 1 Q55An undirected, unweighted, simple graph G(V, E) is said to be 2-colorable if there exists a function c:V→{0,1}c: V \to \{0, 1\} such that for every…MSQ · +2 marks · Medium
  9. Set 1 Q61Consider the recursive functions represented by the following code segment: [code] The smallest positive integer nn for which foo(n) returns 5 is ______.…NAT · +2 marks · Hard
  10. Set 1 Q62The following sequence corresponds to the preorder traversal of a binary search tree TT: 50, 25, 13, 40, 30, 47, 75, 60, 70, 80, 77 The position of the…NAT · +2 marks · Medium
  11. Set 1 Q63Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always…NAT · +2 marks · Medium
  12. Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of visiting nodes during a traversal. | | T | | S |…MCQ · +1 marks · Easy
  13. Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] P3 [code] Which one of the following statements is true?MCQ · +1 marks · Easy
  14. Set 2 Q30The keys 5, 28, 19, 15, 26, 33, 12, 17, 10 are inserted into a hash table using the hash function h(k)=kmod  9h(k) = k \mod 9. The collisions are resolved by chaining.…NAT · +1 marks · Easy
  15. Set 2 Q38Consider an array AA of integers of size nn. The indices of AA run from 1 to nn. An algorithm is to be designed to check whether AA satisfies the…MCQ · +2 marks · Medium
  16. Set 2 Q49Consider a binary search tree (BST) with nn leaf nodes (n>0n > 0). Given any node VV, the key present in the node is denoted as Val(V)Val(V). All the keys present…MSQ · +2 marks · Hard
  17. Set 2 Q50Consider a stack SS and a queue QQ. Both of them are initially empty and have the capacity to store ten elements each. The elements 1, 2, 3, 4, and 5 arrive…MSQ · +2 marks · Medium
  18. Set 2 Q60Consider the following ANSI-C program. [code] The output of this program is ____________. (answer in integer) Note: Assume that the program compiles and…NAT · +2 marks · Easy
  19. Set 2 Q61Consider the following ANSI-C function. [code] The maximum possible value that can be returned from this function is ____________. (answer in integer) *Note:…NAT · +2 marks · Medium

GATE CS 202518 questions

  1. Set 1 Q21Consider the following B+B^+ tree with 5 nodes, in which a node can store at most 3 key values. The value 23 is now inserted in the B+B^+ tree. Which of the…MSQ · +1 marks · Medium
  2. Set 1 Q26Which of the following statement(s) is/are TRUE for any binary search tree (BST) having nn distinct integers?MSQ · +1 marks · Easy
  3. Set 1 Q34[code] The output of the given C program is __________. (Answer in integer)NAT · +1 marks · Easy
  4. Set 1 Q35The height of any rooted tree is defined as the maximum number of edges in the path from the root node to any leaf node. Suppose a Min-Heap TT stores 32 keys.…NAT · +1 marks · Easy
  5. Set 1 Q61[code] The value printed by the given C program is _______ . (Answer in integer)NAT · +2 marks · Medium
  6. Set 1 Q62Let LIST be a datatype for an implementation of linked list defined as follows: [code] Suppose a program has created two linked lists, L1 and L2, whose…NAT · +2 marks · Medium
  7. Set 1 Q63Consider the following C program: [code] The value printed by the given C program is _______ . (Answer in integer)NAT · +2 marks · Medium
  8. Set 1 Q65In a double hashing scheme, h1(k)=k(mod11)h_1(k) = k \pmod{11} and h2(k)=1+(k(mod7))h_2(k) = 1 + (k \pmod 7) are the auxiliary hash functions. The size mm of the hash table is 11. The…NAT · +2 marks · Medium
  9. Set 2 Q13Consider a binary tree TT in which every node has either zero or two children. Let n>0n > 0 be the number of nodes in TT. Which ONE of the following is the…MCQ · +1 marks · Easy
  10. Set 2 Q19Consider the following C program: [code] Which ONE of the following will be the output of the program?MCQ · +1 marks · Medium
  11. Set 2 Q29Which of the following statements regarding Breadth First Search (BFS) and Depth First Search (DFS) on an undirected simple graph GG is/are TRUE?MSQ · +1 marks · Medium
  12. Set 2 Q33[code] The output of the given C code segment is ________. (Answer in integer)NAT · +1 marks · Easy
  13. Set 2 Q35Suppose the values 10,−4,15,30,20,5,60,1910, -4, 15, 30, 20, 5, 60, 19 are inserted in that order into an initially empty binary search tree. Let TT be the resulting binary…NAT · +1 marks · Easy
  14. Set 2 Q38A meld operation on two instances of a data structure combines them into one single instance of the same data structure. Consider the following data…MCQ · +2 marks · Medium
  15. Set 2 Q45Consider a stack data structure into which we can PUSH and POP records. Assume that each record pushed in the stack has a positive integer key and that all…MSQ · +2 marks · Medium
  16. Set 2 Q59Consider the following algorithm someAlgo that takes an undirected graph GG as input. someAlgo(G) 1. Let vv be any vertex in GG. Run BFS on GG starting…NAT · +2 marks · Medium
  17. Set 2 Q62Consider the following C program: [code] The output of the above program is ___________.NAT · +2 marks · Medium
  18. Set 2 Q63Consider the following C program: [code] The output of the given C program is ________. (Answer in integer)NAT · +2 marks · Easy

GATE CS 202417 questions

  1. Set 1 Q17Given an integer array of size NN, we want to check if the array is sorted (in either ascending or descending order). An algorithm solves this problem by…MCQ · +1 marks · Easy
  2. Set 1 Q18Consider the following C program: [code] Which one of the following statements is CORRECT?MCQ · +1 marks · Easy
  3. Set 1 Q19Consider the following C program: [code] Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the…MCQ · +1 marks · Medium
  4. Set 1 Q21In a B+B^+ tree, the requirement of at least half-full (50%) node occupancy is relaxed for which one of the following cases?MCQ · +1 marks · Easy
  5. Set 1 Q33Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below. | Operator | Precedence | Associativity…NAT · +1 marks · Medium
  6. Set 1 Q41An array [82,101,90,11,111,75,33,131,44,93][82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified. Which one of the following options represents the first three elements in the heapified…MCQ · +2 marks · Medium
  7. Set 1 Q43Consider a binary min-heap containing 105 distinct elements. Let kk be the index (in the underlying array) of the maximum element stored in the heap. The…MCQ · +2 marks · Medium
  8. Set 1 Q45Let GG be a directed graph and TT a depth first search (DFS) spanning tree in GG that is rooted at a vertex vv. Suppose TT is also a breadth first search…MSQ · +2 marks · Hard
  9. Set 1 Q48Consider the following C function definition. [code] Which of the following statements is/are TRUE about the above function?MSQ · +2 marks · Medium
  10. Set 1 Q60The number of edges present in the forest generated by the DFS traversal of an undirected graph GG with 100 vertices is 40. The number of connected components…NAT · +2 marks · Easy
  11. Set 2 Q13Consider the following C program. Assume parameters to a function are evaluated from right to left. [code] Which one of the following options is the CORRECT…MCQ · +1 marks · Medium
  12. Set 2 Q17Let AA be the adjacency matrix of a simple undirected graph GG. Suppose AA is its own inverse. Which one of the following statements is always TRUE?MCQ · +1 marks · Medium
  13. Set 2 Q33Consider the following C function definition. [code] Which of the following statements is/are TRUE?MSQ · +1 marks · Medium
  14. Set 2 Q36What is the output of the following C program? [code]MCQ · +2 marks · Medium
  15. Set 2 Q39You are given a set VV of distinct integers. A binary search tree TT is created by inserting all elements of VV one by one, starting with an empty tree. The…MCQ · +2 marks · Medium
  16. Set 2 Q42Consider an array XX that contains nn positive integers. A subarray of XX is defined to be a sequence of array locations with consecutive indices. The C…MCQ · +2 marks · Hard
  17. Set 2 Q48Let S1 and S2 be two stacks. S1 has capacity of 4 elements. S2 has capacity of 2 elements. S1 already has 4 elements: 100, 200, 300, and 400, whereas S2 is…MSQ · +2 marks · Hard

GATE CS 20236 questions

  1. Set 1 Q12Which one of the following sequences when stored in an array at locations A[1],…,A[10]A[1], \dots, A[10] forms a max-heap?MCQ · +1 marks · Easy
  2. Set 1 Q13Let SLLdel be a function that deletes a node in a singly-linked list given a pointer to the node and a pointer to the head of the list. Similarly, let…MCQ · +1 marks · Easy
  3. Set 1 Q20An 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.…MCQ · +1 marks · Easy
  4. Set 1 Q30Let AA be the adjacency matrix of the graph with vertices {1, 2, 3, 4, 5}. [figure] Let λ1,λ2,λ3,λ4\lambda_1, \lambda_2, \lambda_3, \lambda_4, and λ5\lambda_5 be the…NAT · +1 marks · Medium
  5. Set 1 Q35The integer value printed by the ANSI-C program given below is ________. [code]NAT · +1 marks · Easy
  6. Set 1 Q36Consider the following program: [code] Which one of the following options represents the activation tree corresponding to the main function?MCQ · +2 marks · Easy

Other GATE CS topics

Continue learning with Success Tracker

Keep working on Programming & Data Structures

Reading a solution is a useful start. In Success Tracker, you can attempt questions yourself, review mistakes and return to the topics that need another pass.

AI-powered practice· Unlimited practice on eligible plans
PYQs with solutions
Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
Practice that adapts
Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
AI doubt support
Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.

Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.

This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.