GATE CS Programming & Data Structures PYQ with Solutions
Also searched as: GATE DS PYQ · GATE Data Structures PYQ · GATE C Programming PYQ
All 65 Programming & Data Structures questions from every GATE Computer Science (CS/CSE) paper from 2021 to 2026, both sets where there were two, with the official answer key and a step-by-step solution. Programming & Data Structures carries about 9.8 marks per paper on average. Questions are grouped chapter-wise, newest paper first.
| 2026 | 2025 | 2024 | 2023 | 2022 | 2021 |
|---|---|---|---|---|---|
| 12 | 11.5 | 9 | 9 | 10 | 7 |
Programming & Data Structures: Trees — GATE CS PYQ (13)
- GATE CS 2026 Set 1 · Q33Programming & Data Structuresnat1 markThe 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 with 23 nodes is _________. (answer in integer)Open GATE CS 2026 Set 1 Q33 · discuss this question →
Answer: 11
Solution
Answer: 11 In a full binary tree (every node has 0 or 2 children), , so internal nodes. The height is greatest when the tree is a "caterpillar": each internal node has one leaf child and one internal child. Each internal node then adds one level, so the height is 11.
- GATE CS 2026 Set 1 · Q40Programming & Data Structuresmcq2 marksLet be the set of all integers from 1 to 15. Consider any order of insertion of the elements of into a binary search tree that creates a complete binary tree. Which one of the following elements can NEVER be the third element that is inserted?
- A.4
- B.2
- C.10
- D.5✓
Open GATE CS 2026 Set 1 Q40 · discuss this question →Answer: D
Solution
Answer: (D) 5 Fifteen keys in a complete BST have a fixed shape: the root is 8, level 1 is {4, 12}, and level 2 is {2, 6, 10, 14}. A node can be inserted only after its parent. So 8 goes first, then 4 or 12 second, and the third is the other of 4/12 or a child of the second (2, 6, 10 or 14). 5 is on level 3 and needs 8, 4 and 6 before it. It can never be third.
- A.
- GATE CS 2026 Set 1 · Q62Programming & Data Structuresnat2 marksThe following sequence corresponds to the preorder traversal of a binary search tree : 50, 25, 13, 40, 30, 47, 75, 60, 70, 80, 77 The position of the element 60 in the postorder traversal of is ______. (answer in integer) Note: The position begins with 1.Open GATE CS 2026 Set 1 Q62 · discuss this question →
Answer: 7
Solution
Answer: 7 Inserting the preorder sequence into an empty BST rebuilds : Postorder (left, right, root): 13, 30, 47, 40, 25, 70, 60, 77, 80, 75, 50. 60 is at position 7.
- GATE CS 2026 Set 2 · Q12Programming & Data Structuresmcq1 markThe set represents various traversals over a binary tree. The set represents the order of visiting nodes during a traversal.
Which one of the following is the correct match from to ?T S I: Inorder L: left subtree, node, right subtree II: Preorder M: node, left subtree, right subtree III: Postorder N: left subtree, right subtree, node- A.I – L, II – M, III – N✓
- B.I – M, II – L, III – N
- C.I – N, II – M, III – L
- D.I – L, II – N, III – M
Open GATE CS 2026 Set 2 Q12 · discuss this question →Answer: A
Solution
Answer: (A) Inorder visits left-node-right ; Preorder visits node-left-right ; Postorder visits left-right-node . Hence I–L, II–M, III–N. - A.
- GATE CS 2026 Set 2 · Q49Programming & Data Structuresmsq2 marksConsider a binary search tree (BST) with leaf nodes (); all keys are distinct reals. For a node , is its inorder successor ( if none), and if then . For every leaf node with a non-NULL , a new key with is inserted into the BST. Let be the list of all such new keys. Which of the following statements is/are true?
- A.cannot have any duplicates✓
- B.will have at least one element
- C.After inserting all keys from , the height of the BST can increase at most by one✓
- D.Number of nodes in the BST will double after inserting all keys from
Open GATE CS 2026 Set 2 Q49 · discuss this question →Answer: A, C
Solution
Answer: (A), (C) - (A) Each lies in the disjoint open interval ; these intervals are pairwise disjoint, so all are distinct. True ✓ - (B) If the tree is a single node (a leaf with no successor), is empty. False ✗ - (C) Each is inserted just after an existing leaf/successor, adding at most one new level, so overall height grows by at most 1. True ✓ - (D) (total nodes), so the count does not double. False ✗ - A.
- GATE CS 2025 Set 1 · Q26Programming & Data Structuresmsq1 markWhich of the following statement(s) is/are TRUE for any binary search tree (BST) having distinct integers?
- A.The maximum length of a path from the root node to any other node is .✓
- B.An inorder traversal will always produce a sorted sequence of elements.✓
- C.Finding an element takes time in the worst case.
- D.Every BST is also a Min-Heap.
Open GATE CS 2025 Set 1 Q26 · discuss this question →Answer: A, B
Solution
Answer: (A) and (B) - (A) A completely skewed BST (each node has one child) forms a chain of nodes → longest root-to-node path has edges. True (this is the maximum). - (B) Inorder traversal of a BST always visits keys in increasing order. True. - (C) Search is ; in the worst case (skewed tree) , so worst case is , not . False. - (D) A BST orders left root right; a Min-Heap requires parent children with no left/right ordering. A BST is generally not a min-heap. False. - A.
- GATE CS 2025 Set 2 · Q13Programming & Data Structuresmcq1 markConsider a binary tree in which every node has either zero or two children. Let be the number of nodes in . Which ONE of the following is the number of nodes in that have exactly two children?
- A.
- B.✓
- C.
- D.
Open GATE CS 2025 Set 2 Q13 · discuss this question →Answer: B
Solution
Answer: (B) In a full binary tree, the number of leaves equals (internal nodes) . Let be the internal nodes (each with two children). Then - A.
- GATE CS 2025 Set 2 · Q35Programming & Data Structuresnat1 markSuppose the values 10, -4, 15, 30, 20, 5, 60, 19 are inserted in that order into an initially empty binary search tree . The number of edges in the path from the node containing 19 to the root node of is ___________. (Answer in integer)Open GATE CS 2025 Set 2 Q35 · discuss this question →
Answer: 4
Solution
Answer: 4 Insertion builds:Path from 19 up to root: = 4 edges.10 root; -4 left of 10; 15 right of 10; 30 right of 15; 20 left of 30; 5 right of -4; 60 right of 30; 19 left of 20. - GATE CS 2024 Set 2 · Q39Programming & Data Structuresmcq2 marksA BST is built by inserting all elements of a set in some unknown order. Which can be determined from alone?
- A.Inorder traversal of T✓
- B.Root node of T
- C.Preorder traversal of T
- D.Postorder traversal of T
Open GATE CS 2024 Set 2 Q39 · discuss this question →Answer: A
Solution
Answer: (A) The inorder traversal of any BST is the sorted order of its keys, which is determined by the set regardless of insertion order. The root, preorder, and postorder all depend on the (unknown) insertion sequence. - A.
- GATE CS 2023 · Q47Programming & Data Structuresmcq2 marks
foo(p)returnsp->val + foo(p->left) + foo(p->right)and prints that sum. For the binary tree shown (root 10; left subtree root 5 with children 3,8; right subtree root 11 with right child 13), what does foo print when called on the root?
- A.3 8 5 13 11 10
- B.3 5 8 10 11 13
- C.3 8 16 13 24 50✓
- D.3 16 8 50 24 13
Open GATE CS 2023 Q47 · discuss this question →Answer: C
Solution
Answer: (C) 3 8 16 13 24 50foocomputes subtree sums and prints each AFTER recursing (post-order), printing the subtree sum:
Output: 3 8 16 13 24 50.foo(3)=3 -> prints 3 foo(8)=8 -> prints 8 foo(5)=5+3+8=16 -> prints 16 foo(13)=13 -> prints 13 foo(11)=11+0+13=24 -> prints 24 foo(10)=10+16+24=50 -> prints 50 - A.
- GATE CS 2022 · Q28Programming & Data Structuresnat1 markSuppose a binary search tree with 1000 distinct elements is also a complete binary tree, stored using the array representation of binary heap trees. Assuming array indices start at 0, the 3rd largest element is stored at index __________.Open GATE CS 2022 Q28 · discuss this question →
Answer: 509
Solution
Answer: 509 A complete binary tree that is also a BST, stored in a 0-indexed array (heap layout), has 1000 nodes. The largest element is the right-most node of the tree; the 3rd largest is found by the in-order predecessor relationships. Working out its array position in the complete-tree layout gives index 509. - GATE CS 2021 Set 1 · Q20Programming & Data Structuresmcq1 markA binary search tree contains distinct elements. What is the time complexity of picking an element in that is smaller than the maximum element in ?
- A.
- B.
- C.
- D.✓
Open GATE CS 2021 Set 1 Q20 · discuss this question →Answer: D
Solution
The maximum is the right-most node, so the root (for ) is not the maximum. Returning the root (or any fixed non-maximum node) takes time. - A.
- GATE CS 2021 Set 2 · Q26Programming & Data Structuresnat1 markConsider a complete binary tree with 7 nodes. Let denote the set of first 3 elements obtained by performing Breadth-First Search (BFS) starting from the root. Let denote the set of first 3 elements obtained by performing Depth-First Search (DFS) starting from the root. The value of is ______.Open GATE CS 2021 Set 2 Q26 · discuss this question →
Answer: 1
Solution
Number the nodes (root), (level 1), (level 2). BFS first three: . DFS first three: . , so .
Programming & Data Structures: Functions — GATE CS PYQ (9)
- GATE CS 2026 Set 1 · Q61Programming & Data Structuresnat2 marksConsider the recursive functions represented by the following code segment:
The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer) Note: Ignore syntax errors (if any) in the function.int bar(int n){ if (n == 1) return 0; else return 1 + bar(n/2); } int foo(int n){ if (n == 1) return 1; else return 1 + foo(bar(n)); }Open GATE CS 2026 Set 1 Q61 · discuss this question →Answer: 65536
Solution
Answer: 65536bar(n), andfoocounts how many times you can apply before reaching 1. That is an iterated logarithm, plus 1. Build the smallest for each value, working backwards:Check: . That is 4 calls to bar, so foo returns . Note that bar(1) = 0 would make foo(0) recurse forever, but the chain above never calls foo(0).foo(n) | Need bar(n) to be | Smallest n -------+-------------------------------+--------------- 1 | – | 1 2 | 1 | 2 3 | ≥ 2 (smallest giving foo = 2) | 4 4 | ≥ 4 | 16 5 | ≥ 16 | 2^(16) = 65536 - GATE CS 2026 Set 2 · Q61Programming & Data Structuresnat2 marksConsider the following ANSI-C function.
The maximum possible value that can be returned from this function is __________. (answer in integer)cint func(int start, int end){ int length=end+1-start; if((length<1)||(start<0)||(end<0)){ return(0); } if(length%3==0){ return(func(start+1, end)); } else if(length%3==1){ return(1+func(start, end-1)); } else { return(func(start+2, end)); } }Open GATE CS 2026 Set 2 Q61 · discuss this question →Answer: 1
Solution
The return value depends only on . Let be the value: - : . - : . - : . - : . Computing: The value never exceeds . - GATE CS 2025 Set 1 · Q61Programming & Data Structuresnat2 marksConsider the C program:
The value printed by the given C program is _______. (Answer in integer)#include <stdio.h> int foo(int S[], int size){ if(size == 0) return 0; if(size == 1) return 1; if(S[0] != S[1]) return 1 + foo(S+1, size-1); return foo(S+1, size-1); } int main(){ int A[] = {0,1,2,2,2,0,0,1,1}; printf("%d", foo(A,9)); return 0; }Open GATE CS 2025 Set 1 Q61 · discuss this question →Answer: 5
Solution
Answer: 5fooscans adjacent pairs, adding 1 wheneverS[0] != S[1], and adds a final 1 at thesize==1base case. This counts the number of maximal runs of equal consecutive elements (i.e. ). For the runs are:There are adjacent-difference positions, plus the base : . - GATE CS 2025 Set 2 · Q63Programming & Data Structuresnat2 marksConsider the C program:
The output of the given C program is ________. (Answer in integer)int g(int n) { return (n+10); } int f(int n) { return g(n*2); } int main() { int sum, n; sum = 0; for (n=1; n<3; n++) sum += g(f(n)); printf("%d", sum); return 0; }Open GATE CS 2025 Set 2 Q63 · discuss this question →Answer: 46
Solution
Answer: 46 , so . - . - . - GATE CS 2024 Set 1 · Q19Programming & Data Structuresmcq1 markConsider the C program (input is 1234 then newline):
Which statement is CORRECT?void fX(); int main(){ fX(); return 0; } void fX(){ char a; if((a=getchar()) != '\n') fX(); if(a != '\n') putchar(a); }- A.The program will not terminate
- B.The program will terminate with no output
- C.The program will terminate with 4321 as output✓
- D.The program will terminate with 1234 as output
Open GATE CS 2024 Set 1 Q19 · discuss this question →Answer: C
Solution
Answer: (C) 4321 Each call reads one character and recurses before printing. The recursion reads 1,2,3,4 then newline (stops recursing). As the recursion unwinds, characters are printed in reverse of reading order: 4,3,2,1 -> "4321". - A.
- GATE CS 2024 Set 1 · Q48Programming & Data Structuresmsq2 marksruns
for(i=0;i<y;i++) x = x+x+y;and returns . Which statements is/are TRUE?- A.x=20,y=10 -> return > 2^20
- B.x=20,y=20 -> return > 2^20✓
- C.x=20,y=10 -> return < 2^10
- D.x=10,y=20 -> return > 2^20✓
Open GATE CS 2024 Set 1 Q48 · discuss this question →Answer: B, D
Solution
Answer: (B) and (D) Each iteration does , roughly doubling . After iterations grows like . - : factor ; starting from 20, result is around –, which is but not . So (A) false, (C) false. - : factor , so the result far exceeds -> (B) true; same for -> (D) true. - A.
- GATE CS 2024 Set 2 · Q13Programming & Data Structuresmcq1 markParameters are evaluated right to left.
Correct output?int g(int p){printf("%d",p);return p;} int h(int q){printf("%d",q);return q;} void f(int x,int y){ g(x); h(y); } int main(){ f(g(10),h(20)); }- A.20101020✓
- B.10202010
- C.20102010
- D.10201020
Open GATE CS 2024 Set 2 Q13 · discuss this question →Answer: A
Solution
Answer: (A) 20101020f(g(10), h(20)): arguments evaluated right-to-left, soh(20)prints 20, theng(10)prints 10 -> "2010". Then insidef,g(x)prints 10,h(y)prints 20 -> "1020". Total: "2010"+"1020" = "20101020". - A.
- GATE CS 2021 Set 2 · Q33Programming & Data Structuresnat1 markConsider the following ANSI C function.
The value returned byint SomeFunction(int x, int y) { if ((x == 1) || (y == 1)) return 1; if (x == y) return x; if (x > y) return SomeFunction(x - y, y); if (y > x) return SomeFunction(x, y - x); }SomeFunction(15, 255)is ______.Open GATE CS 2021 Set 2 Q33 · discuss this question →Answer: 15
Solution
The function performs the subtractive Euclidean algorithm, returning (with 1 as the base case). . - GATE CS 2021 Set 2 · Q59Programming & Data Structuresnat2 marksConsider the following ANSI C program.
The output of the program upon execution is ______.#include <stdio.h> int foo(int x, int y, int q) { if ((x <= 0) && (y <= 0)) return q; if (x <= 0) return foo(x, y - q, q); if (y <= 0) return foo(x - q, y, q); return foo(x, y - q, q) + foo(x - q, y, q); } int main() { int r = foo(15, 15, 10); printf("%d", r); return 0; }Open GATE CS 2021 Set 2 Q59 · discuss this question →Answer: 60
Solution
The recursion counts the number of monotone lattice paths (steps of size decreasing or ) from down to the base region, each contributing . Evaluating the recursion gives the output 60.
Programming & Data Structures: Heaps — GATE CS PYQ (8)
- GATE CS 2026 Set 1 · Q23Programming & Data Structuresmsq1 markLet be an odd number greater than 100. Consider a binary minheap with elements stored in an array whose index starts from 1. Which of the following indices of do/does NOT correspond to any leaf node of the minheap?
- A.
- B.✓
- C.✓
- D.
Open GATE CS 2026 Set 1 Q23 · discuss this question →Answer: B, C
Solution
Answer: B, C In a 1-indexed heap, node has a child if . So the internal nodes are and the leaves are . For odd , , so the leaves start at . • and are leaves. • and are internal (not leaves). Example: has leaves 51…101, while 50 and 49 are internal. - A.
- GATE CS 2025 Set 1 · Q35Programming & Data Structuresnat1 markThe 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 stores 32 keys. The height of is _____________. (Answer in integer)Open GATE CS 2025 Set 1 Q35 · discuss this question →
Answer: 5
Solution
Answer: 5 A heap is a complete binary tree, whose height (in edges) for nodes is .(Levels – hold nodes; the 32nd node sits on level , so the longest root-to-leaf path has 5 edges.) - GATE CS 2025 Set 2 · Q38Programming & Data Structuresmcq2 marksA meld combines two instances of a data structure into one. Consider: P: Unsorted doubly linked list with head and tail pointers. Q: Min-heap implemented using an array. R: Binary Search Tree. Which ONE gives the worst-case time complexities for melding instances of size ?
- A.P: , Q: , R:✓
- B.P: , Q: , R:
- C.P: , Q: , R:
- D.P: , Q: , R:
Open GATE CS 2025 Set 2 Q38 · discuss this question →Answer: A
Solution
Answer: (A) P: , Q: , R: - P: link the tail of one list to the head of the other — . - Q: concatenate the two arrays and run build-heap — . - R: in the worst case, merge by collecting elements and rebuilding; a balanced-independent merge takes (e.g. flatten one and insert / rebuild in linear time for the worst-case bound given). Option (A) matches. - A.
- GATE CS 2024 Set 1 · Q41Programming & Data Structuresmcq2 marksThe array [82, 101, 90, 11, 111, 75, 33, 131, 44, 93] is heapified (max-heap). Which represents the first three elements?
- A.82, 90, 101
- B.82, 11, 93
- C.131, 11, 93
- D.131, 111, 90✓
Open GATE CS 2024 Set 1 Q41 · discuss this question →Answer: D
Solution
Answer: (D) 131, 111, 90 Building a max-heap, the maximum element 131 becomes the root. After heapify, the root's two children are the next two largest along the chosen paths, giving 111 and 90 as the first level below. So the first three array positions are 131, 111, 90. - A.
- GATE CS 2024 Set 1 · Q43Programming & Data Structuresmcq2 marksA binary min-heap has 105 distinct elements. Let be the array index of the maximum element. The number of possible values of is
- A.53✓
- B.52
- C.27
- D.1
Open GATE CS 2024 Set 1 Q43 · discuss this question →Answer: A
Solution
Answer: (A) 53 In a min-heap the maximum is always a leaf (it cannot have a child smaller than it). For (1-indexed), leaves are the nodes with no children: indices through . That is positions, and the max can be at any of them. So 53 possible values of . - A.
- GATE CS 2023 · Q12Programming & Data Structuresmcq1 markWhich sequence stored at forms a max-heap?
- A.23, 17, 10, 6, 13, 14, 1, 5, 7, 12
- B.23, 17, 14, 7, 13, 10, 1, 5, 6, 12✓
- C.23, 17, 14, 6, 13, 10, 1, 5, 7, 15
- D.23, 14, 17, 1, 10, 13, 16, 12, 7, 5
Open GATE CS 2023 Q12 · discuss this question →Answer: B
Solution
Answer: (B) 23, 17, 14, 7, 13, 10, 1, 5, 6, 12 A max-heap requires and . Checking (B):
all hold. In (A) 13<14 (A[5]<A[6])? ok, but fails. (C) fails. (D) ? root ok but ... fails. Only (B) is a valid max-heap.23>=17,14 | 17>=7,13 | 14>=10,1 | 7>=5,6 | 13>=12 - A.
- GATE CS 2023 · Q46Programming & Data Structuresmcq2 marksA priority queue is a max-heap. Extract-Max deletes the max; Insert adds a key; both preserve the heap. With elements, the worst-case running times are
- A.Both O(1).
- B.Both O(log n).✓
- C.Extract-Max O(1), Insert O(n).
- D.Extract-Max O(1), Insert O(log n).
Open GATE CS 2023 Q46 · discuss this question →Answer: B
Solution
Answer: (B) Both O(log n) Insert appends at the end and sifts up — . Extract-Max removes the root, moves the last element to the root, and sifts down — . Both are in the worst case. - A.
- GATE CS 2021 Set 2 · Q12Programming & Data Structuresmcq1 markConsider a binary min-heap containing distinct elements stored as an array. What is the worst-case time complexity to find the maximum element in the heap?
- A.
- B.
- C.✓
- D.
Open GATE CS 2021 Set 2 Q12 · discuss this question →Answer: C
Solution
In a min-heap the maximum can be any of the leaves, so all leaves must be examined, requiring time. - A.
Programming & Data Structures: Basics & Control Structures — GATE CS PYQ (8)
- GATE CS 2026 Set 2 · Q19Programming & Data Structuresmcq1 markConsider the following three ANSI-C programs, P1, P2, and P3.
Which one of the following statements is true?c// P1 #include <stdio.h> int a=5; int main(){ int a=7; return(0); } // P2 #include <stdio.h> int main(){ int a=5; int a=7; return(0); } // P3 #include <stdio.h> int main(){ int a=5; float a=7; return(0); }- A.Only P1 will compile without any error✓
- B.Only P2 will compile without any error
- C.Only P3 will compile without any error
- D.All three programs P1, P2, and P3 will compile without any error
Open GATE CS 2026 Set 2 Q19 · discuss this question →Answer: A
Solution
Answer: (A) In P1 the localint asimply shadows the globala— legal. In P2 and P3 the identifierais redeclared in the same scope (asint/intandint/float), which is a compile-time error. Hence only P1 compiles. - A.
- GATE CS 2025 Set 1 · Q63Programming & Data Structuresnat2 marksConsider the C program:
The value printed by the given C program is _______. (Answer in integer)#include <stdio.h> int gate (int n) { int d, t, newnum, turn; newnum = turn = 0; t = 1; while (n >= t) t *= 10; t /= 10; while (t > 0) { d = n/t; n = n%t; t /= 10; if (turn) newnum = 10*newnum + d; turn = (turn + 1) % 2; } return newnum; } int main () { printf ("%d", gate(14362)); return 0; }Open GATE CS 2025 Set 1 Q63 · discuss this question →Answer: 46
Solution
Answer: 46 The function reads digits of from most significant to least, and keeps a digit only on alternate turns, starting by skipping the first ( begins at 0, so digit 1 is skipped, digit 2 kept, ...). Digits of : .
So it keeps the 2nd and 4th digits → .turn=0: d=1 skipped turn->1 turn=1: d=4 newnum = 4 turn->0 turn=0: d=3 skipped turn->1 turn=1: d=6 newnum = 46 turn->0 turn=0: d=2 skipped - GATE CS 2025 Set 2 · Q33Programming & Data Structuresnat1 markConsider the C code:
The output of the given C code segment is ________. (Answer in integer)int x=126, y=105; do { if(x>y) x=x-y; else y=y-x; } while(x!=y); printf("%d", x);Open GATE CS 2025 Set 2 Q33 · discuss this question →Answer: 21
Solution
Answer: 21 This is Euclid's subtraction method, which computes :
, so it prints .126,105 -> 21,105 -> 21,84 -> 21,63 -> 21,42 -> 21,21 (stop) - GATE CS 2024 Set 1 · Q18Programming & Data Structuresmcq1 markConsider the C program:
Which statement is CORRECT?int main(){ int a = 6; int b = 0; while(a < 10) { a = a / 12 + 1; a += b; } printf("%d", a); return 0; }- A.The program prints 9
- B.The program prints 10
- C.The program gets stuck in an infinite loop✓
- D.The program prints 6
Open GATE CS 2024 Set 1 Q18 · discuss this question →Answer: C
Solution
Answer: (C) The program gets stuck in an infinite loop Start (<10), . Inside: (integer division), then . Now . Next iteration: , . stays forever, so the loop never ends. - A.
- GATE CS 2023 · Q35Programming & Data Structuresnat1 markThe integer printed by the C program (funcp uses
static int x=1; x++; return x;, called twice in main) is __________int funcp(){ static int x = 1; x++; return x; } int main(){ int x,y; x = funcp(); y = funcp()+x; printf("%d\n", (x+y)); }Open GATE CS 2023 Q35 · discuss this question →Answer: 7
Solution
Answer: 7x = funcp(): static x becomes 2, returns 2, so x=2.y = funcp()+x: static x becomes 3, returns 3, so y=3+2=5. Printx+y = 2+5 = 7. - GATE CS 2022 · Q44Programming & Data Structuresmcq2 marksWhat is printed?
char a='P', b='x'; char c=(a&b)+'*'; char d=(a|b)-'-'; char e=(a^b)+'+'; printf("%c %c %c", c, d, e);(ASCII: P=80, x=120, *=42, -=45, +=43)- A.z K S✓
- B.122 75 83
- C.* - +
- D.P x +
Open GATE CS 2022 Q44 · discuss this question →Answer: A
Solution
Answer: (A) z K S ASCII: P=80 (01010000), x=120 (01111000). - ; 'z'. - ; 'K'. - ; 'S'. Printed as characters: "z K S". - A.
- GATE CS 2022 · Q62Programming & Data Structuresnat2 marksQueue has four elements (1,2,3,4; 1 at head), empty. Only Enqueue and Dequeue are allowed. The minimum number of Enqueue operations on required to place 's elements into in reverse order without extra storage is __________.Open GATE CS 2022 Q62 · discuss this question →
Answer: 0
Solution
Answer: 0 With only Enqueue/Dequeue on and (FIFO queues) and no additional storage, it is impossible to reverse the order of the elements — any sequence of moves preserves relative order. So the minimum number of enqueue operations that could achieve the reversal is 0 (the task cannot be done, hence no valid positive count). - GATE CS 2021 Set 1 · Q47Programming & Data Structuresmcq2 marksConsider the following ANSI C program.
Which one of the following options is correct?#include <stdio.h> int main() { int i, j, count; count = 0; i = 0; for (j = -3; j <= 3; j++) { if ((j >= 0) && (i++)) count = count + j; } count = count + i; printf("%d", count); return 0; }- A.The program will not compile successfully.
- B.The program will compile successfully and output 10 when executed.✓
- C.The program will compile successfully and output 8 when executed.
- D.The program will compile successfully and output 13 when executed.
Open GATE CS 2021 Set 1 Q47 · discuss this question →Answer: B
Solution
For the first operand is false andi++is not evaluated. For :i++returns 0 (false) then , so count unchanged. For :i++returns 1,2,3 (true), count becomes and reaches 4. Final count . - A.
Programming & Data Structures: Pointers — GATE CS PYQ (7)
- GATE CS 2026 Set 2 · Q60Programming & Data Structuresnat2 marksConsider the following ANSI-C program.
The output of this program is __________. (answer in integer)c#include <stdio.h> int main(){ int *ptr, a, b, c; a=5; b=11; c=20; ptr=&a; *ptr=c; ptr=&c; a=*(&b); c=*ptr-a; printf("%d",c); return(0); }Open GATE CS 2026 Set 2 Q60 · discuss this question →Answer: 9
Solution
ptr=&a; *ptr=c;sets .ptr=&c;now points to .a=*(&b);sets .c=*ptr-a. So the program prints . - GATE CS 2025 Set 1 · Q34Programming & Data Structuresnat1 markConsider the C program:
The output of the given C program is __________. (Answer in integer)#include <stdio.h> void foo(int *p, int x){ *p = x; } int main(){ int *z; int a = 20, b = 25; z = &a; foo(z, b); printf("%d", a); return 0; }Open GATE CS 2025 Set 1 Q34 · discuss this question →Answer: 25
Solution
Answer: 25zpoints toa. The callfoo(z, b)passes this pointer and the valueb = 25. Insidefoo,*p = xwrites25through the pointer, i.e. intoa. Soabecomes25, andprintf("%d", a)prints . - GATE CS 2025 Set 2 · Q19Programming & Data Structuresmcq1 markConsider the C program:
Which ONE of the following will be the output of the program?#include <stdio.h> void stringcopy(char *, char *); int main(){ char a[30] = "@#Hello World!"; stringcopy(a, a + 2); printf("%s\n", a); return 0; } void stringcopy(char *s, char *t) { while(*t) *s++ = *t++; }- A.@#Hello World!
- B.Hello World!
- C.ello World!
- D.Hello World!d!✓
Open GATE CS 2025 Set 2 Q19 · discuss this question →Answer: D
Solution
Answer: (D) Hello World!d!t=a+2points at 'H';s=a. The loop copies*tinto*swhile*tis non-zero, advancing both. It copies the 12 characters "Hello World!" froma[2..13]intoa[0..11]. The loop stops at the null terminator but does NOT copy it, so no new '\0' is written. The old charactersa[12]='d',a[13]='!', and the terminatora[14]='\0'remain. Printingatherefore readsa[0..13]= "Hello World!" followed by the leftover "d!" → "Hello World!d!". - A.
- GATE CS 2025 Set 2 · Q62Programming & Data Structuresnat2 marksConsider the C program:
The output of the above program is ___________. (Answer in integer)int main(){ int a; int arr[5] = {30,50,10}; int *ptr; ptr = &arr[0] + 1; a = *ptr; (*ptr)++; ptr++; printf("%d", a + (*ptr) + arr[1]); return 0; }Open GATE CS 2025 Set 2 Q62 · discuss this question →Answer: 111
Solution
Answer: 111arr = {30,50,10,0,0}.ptr = &arr[0]+1 = &arr[1], soa = *ptr = 50. Then(*ptr)++makesarr[1] = 51.ptr++now points to&arr[2], where*ptr = 10. Output - GATE CS 2024 Set 2 · Q36Programming & Data Structuresmcq2 marks
Output?double a[2]={20.0,25.0},*p,*q; p=a; q=p+1; printf("%d,%d",(int)(q-p),(int)(*q-*p));- A.4,8
- B.1,5✓
- C.8,5
- D.1,8
Open GATE CS 2024 Set 2 Q36 · discuss this question →Answer: B
Solution
Answer: (B) 1,5q-pis pointer difference in units of elements .*q-*p = 25.0-20.0 = 5.0, cast to int . Output: "1,5". - A.
- GATE CS 2022 · Q21Programming & Data Structuresmcq1 markWhat is printed?
int x = 1, z[2] = {10, 11}; int *p = NULL; p = &x; *p = 10; p = &z[1]; *(&z[0] + 1) += 3; printf("%d, %d, %d\n", x, z[0], z[1]);- A.1, 10, 11
- B.1, 10, 14
- C.10, 14, 11
- D.10, 10, 14✓
Open GATE CS 2022 Q21 · discuss this question →Answer: D
Solution
Answer: (D) 10, 10, 14*p=10withp=&xsets .&z[0]+1is&z[1], so*(&z[0]+1) += 3makes . is unchanged (10). Output: "10, 10, 14". - A.
- GATE CS 2021 Set 2 · Q20Programming & Data Structuresmcq1 markConsider the following ANSI C program.
The output of the program is ______.#include <stdio.h> int main() { int arr[4][5]; int i, j; for (i = 0; i < 4; i++) for (j = 0; j < 5; j++) arr[i][j] = 10 * i + j; printf("%d", *(arr[1] + 9)); return 0; }- A.14
- B.20
- C.24✓
- D.30
Open GATE CS 2021 Set 2 Q20 · discuss this question →Answer: C
Solution
arr[1]points toarr[1][0]. Adding 9 (row-major) reaches element at offset 9, i.e.arr[2][4](row length 5: ). Its value is . - A.
Programming & Data Structures: Advanced Concepts — GATE CS PYQ (6)
- GATE CS 2026 Set 1 · Q24Programming & Data Structuresmsq1 markConsider a hash table that is initially empty. The hash table is maintained using open addressing with linear probing. The hash function used is . Consider the following sequence of insertions performed on : 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
Open GATE CS 2026 Set 1 Q24 · discuss this question →Answer: C
Solution
Answer: C (position 2 only)Among the options only 2 is empty. The trap is the wrap-around from 10 back to 0 when placing 24.Key | h(x) = (x+7) mod 11 | Probes | Slot ----+---------------------+----------------------+----- 1 | 8 | – | 8 13 | 9 | – | 9 22 | 7 | – | 7 15 | 0 | – | 0 11 | 7 | 7 ✗ → 8 ✗ → 9 ✗ → 10 | 10 24 | 9 | 9 ✗ → 10 ✗ → 0 ✗ → 1 | 1
- A.
- GATE CS 2026 Set 1 · Q34Programming & Data Structuresnat1 markConsider the following program in C:
The output of the program is _________. (answer in integer) Note: Assume that the program compiles and runs successfully.#include <stdio.h> void func(int i, int j) { if(i < j) { int i = 0; while (i < 10) { j += 2; i++; } } printf("%d", i); } int main() { int i = 9, j = 10; func(i, j); return 0; }Open GATE CS 2026 Set 1 Q34 · discuss this question →Answer: 9
Solution
Answer: 9 Inside theifblock,int i = 0;declares a newithat shadows the parameter. The loop changes only that inneri(andj). The innerigoes out of scope when the block ends, soprintfsees the parameteri = 9. - GATE CS 2026 Set 2 · Q17Programming & Data Structuresmcq1 markIn C runtime environment, which one of the following is stored in heap?
- A.A static variable declared inside a function
- B.An array of integers declared inside a function
- C.A dynamically allocated array of integers created using malloc() function call✓
- D.Return address of a function
Open GATE CS 2026 Set 2 Q17 · discuss this question →Answer: C
Solution
Answer: (C) Static variables live in the static/data segment; local (automatic) arrays and the return address live on the stack. Only memory obtained viamalloc()is allocated on the heap. - A.
- GATE CS 2026 Set 2 · Q30Programming & Data Structuresnat1 markThe keys 5, 28, 19, 15, 26, 33, 12, 17, 10 are inserted into a hash table using the hash function . The collisions are resolved by chaining. After all the keys are inserted, the length of the longest chain is __________. (answer in integer)Open GATE CS 2026 Set 2 Q30 · discuss this question →
Answer: 3
Solution
Slots (): . Chains: slot 1 = (length 3), slot 6 = , slot 8 = . Longest chain length . - GATE CS 2025 Set 1 · Q65Programming & Data Structuresnat2 marksIn a double hashing scheme, and are the auxiliary hash functions. The table size . The -th probe uses . The keys are inserted in order: . The slot at which key 24 gets stored is ___________. (Answer in integer)Open GATE CS 2025 Set 1 Q65 · discuss this question →
Answer: 10 to 11
Solution
Answer: 10 Insert each key (slots –):
For key 24: (occupied by 79), . - (occupied by 50). - (empty) → slot 10. Key 24 is stored at slot .63: h1 = 63 mod 11 = 8 -> slot 8 50: h1 = 50 mod 11 = 6 -> slot 6 25: h1 = 25 mod 11 = 3 -> slot 3 79: h1 = 79 mod 11 = 2 -> slot 2 67: h1 = 67 mod 11 = 1 -> slot 1 - GATE CS 2022 · Q16Programming & Data Structuresmcq1 markGiven keys, slots, and two simple uniform hash functions , where is used for odd keys and for even keys, what is the expected number of keys in a slot?
- A.
- B.✓
- C.
- D.
Open GATE CS 2022 Q16 · discuss this question →Answer: B
Solution
Answer: (B) Each key (whether hashed by or , both simple uniform) lands in any slot with probability . By linearity of expectation, the expected load of a slot , independent of the split between odd and even keys. - A.
Programming & Data Structures: Stacks & Queues — GATE CS PYQ (5)
- GATE CS 2026 Set 2 · Q50Programming & Data Structuresmsq2 marksConsider a stack and a queue , both initially empty with capacity ten. The elements 1,2,3,4,5 arrive one by one in that order; each is assigned to (pushed) or (enqueued). After all five are stored, output is generated by first popping all of , then dequeueing all of . The output obtained is . Given the output, predict whether each element went to or . Which of the following is/are possible valid assignment(s)? Note: means was assigned to and means was assigned to .
- A.✓
- B.✓
- C.
- D.
Open GATE CS 2026 Set 2 Q50 · discuss this question →Answer: A, B
Solution
Answer: (A), (B) Popping gives the reverse of its push order; dequeuing gives its arrival order. Output (reverse of ) then (). - (A) pop ; → ✓ - (B) pop ; → ✓ - (C) ✗ - (D) ✗ - A.
- GATE CS 2025 Set 2 · Q45Programming & Data Structuresmsq2 marksWe augment a stack (distinct positive keys) with an MIN operation returning a pointer to the smallest key, (1) without deleting it and (2) without increasing the complexity of standard stack operations. Which approach(es) achieve this?
- A.Keep with every record in the stack a pointer to the record with the smallest key below it.✓
- B.Keep a pointer to the record with the smallest key in the stack.
- C.Keep an auxiliary array in which the key values are maintained in sorted order.
- D.Keep a Min-Heap in which the key values are maintained.
Open GATE CS 2025 Set 2 Q45 · discuss this question →Answer: A
Solution
Answer: (A) (A) Store with each pushed record the pointer to the minimum of the stack below it; on push, the new record's "min" is computed in ; MIN and pop stay . This works. - (B) A single min pointer cannot be restored in after popping the minimum. Fails. - (C) Maintaining a sorted array costs per push/pop. Increases complexity. - (D) A min-heap makes push/pop . Increases complexity. - A.
- GATE CS 2024 Set 2 · Q48Programming & Data Structuresmsq2 marksS1 (capacity 4) holds 100,200,300,400 (400 top); S2 (capacity 2) empty. Operations: PushToS2 (S1->S2), PushToS1 (S2->S1), GenerateOutput (pop S1 to output). No pop on empty / push on full. Which output sequences can be generated?
- A.100, 200, 400, 300
- B.200, 300, 400, 100✓
- C.400, 200, 100, 300✓
- D.300, 200, 400, 100✓
Open GATE CS 2024 Set 2 Q48 · discuss this question →Answer: B, C, D
Solution
Answer: (B), (C) and (D) This is a stack-permutation question with the auxiliary stack S2 (capacity 2) allowing limited reordering. Simulating the allowed moves, sequences (B) 200,300,400,100, (C) 400,200,100,300, and (D) 300,200,400,100 are each reachable, while (A) 100,200,400,300 cannot be produced under the capacity constraints. - A.
- GATE CS 2023 · Q59Programming & Data Structuresnat2 marksSequence . Push into stack S; enqueue into queue Q; then: pop S, dequeue Q, pop S, dequeue Q; dequeue Q and push into S (repeat 3 times); pop S; pop S. The top element of S afterwards is __________Open GATE CS 2023 Q59 · discuss this question →
Answer: 8
Solution
Answer: 8 After the pushes S (top->bottom) = 2,9,8,7,5,1 and Q (front->back)=1,5,7,8,9,2.Top of S is 8.pop S -> 2 (S: 9 8 7 5 1) dequeue Q -> 1 (Q: 5 7 8 9 2) pop S -> 9 (S: 8 7 5 1) dequeue Q -> 5 (Q: 7 8 9 2) dequeue+push x4: move 7,8,9,2 from Q onto S -> S: 2 9 8 7 8 7 5 1 (pushes 7, then 8, then 9, then 2 on top) pop S -> 2 (S: 9 8 7 8 7 5 1) pop S -> 9 (S: 8 7 8 7 5 1) - GATE CS 2021 Set 1 · Q31Programming & Data Structuresnat1 markConsider the following sequence of operations on an empty stack.
Consider the following sequence of operations on an empty queue.push(54); push(52); pop(); push(55); push(62); s = pop();
The value of is ______.enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();Open GATE CS 2021 Set 1 Q31 · discuss this question →Answer: 86
Solution
Stack: after the operations the top popped into is 62. Queue (FIFO): the second dequeue returns 24, so . Hence .
Programming & Data Structures: Arrays & Strings — GATE CS PYQ (4)
- GATE CS 2024 Set 2 · Q33Programming & Data Structuresmsq1 mark
int fX(char *a){ char *b=a; while(*b) b++; return b-a; }Which statements is/are TRUE?- A.fX("abcd") will always return a value✓
- B.With char c[]="abcd" in main, fX(c) always returns a value✓
- C.The function will not compile
- D.With char *c="abcd" in main, fX(c) always returns a value✓
Open GATE CS 2024 Set 2 Q33 · discuss this question →Answer: A, B, D
Solution
Answer: (A), (B) and (D)fXcomputes the string length by advancing to the terminating '\0'. It works for any valid, null-terminated string: a string literal argument (A), a char array (B), and a char pointer to a literal (D) all give length 4. The code compiles fine, so (C) is false. - A.
- GATE CS 2024 Set 2 · Q42Programming & Data Structuresmcq2 marksA C snippet computes the longest subarray with at most two distinct integers. len1 = longest suffix of equal values, len2 = longest suffix with <=2 distinct values. Fill (P) and (Q):
- 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
Open GATE CS 2024 Set 2 Q42 · discuss this question →Answer: B
Solution
Answer: (B) (P) 1 (Q) len1+1 WhenX[i]equals the second-most-recent distinct value (theelse ifbranch), the run of equal values restarts at this element, solen1 = 1(P). WhenX[i]is a brand-new third value (theelsebranch), the at-most-two-distinct window can only extend the previous equal-run plus this element, solen2 = len1+1(Q). - A.
- GATE CS 2022 · Q43Programming & Data Structuresmcq2 marksWhat is printed? (a[3][3][3] initialized row-wise with 1..27; loops over i and k with j=0, printing a[i][j][k].)
- A.1 2 3 / 10 11 12 / 19 20 21✓
- B.1 4 7 / 10 13 16 / 19 22 25
- C.1 2 3 / 4 5 6 / 7 8 9
- D.1 2 3 / 13 14 15 / 25 26 27
Open GATE CS 2022 Q43 · discuss this question →Answer: A
Solution
Answer: (A) 1 2 3 / 10 11 12 / 19 20 21 The array stores values in row-major order, so is element at offset . For : ; : ; : . With fixed at 0 the output is those three rows. - A.
- GATE CS 2021 Set 1 · Q58Programming & Data Structuresnat2 marksConsider the following ANSI C function:
Let Z be an array of 10 elements with Z[i]=1, for all such that . The value returned byint SimpleFunction(int Y[], int n, int x) { int total = Y[0], loopIndex; for (loopIndex = 1; loopIndex <= n - 1; loopIndex++) total = x * total + Y[loopIndex]; return total; }SimpleFunction(Z, 10, 2)is ______.Open GATE CS 2021 Set 1 Q58 · discuss this question →Answer: 1023
Solution
This is Horner evaluation of the polynomial with all coefficients 1 at : .
Programming & Data Structures: Arrays & Linked Lists — GATE CS PYQ (3)
- GATE CS 2026 Set 1 · Q39Programming & Data Structuresmcq2 marksConsider 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 head.
Which one of the following options gives the correct replacements for the expressions E1 and E2?struct node{ int elt; struct node *next; }; int getListSize (struct node *head) { if( E1 ) return 1; return E2; }- A.E1: head == NULL E2: 1 + getListSize(head)
- B.E1: head->next == NULL E2: 1 + getListSize(head->next)✓
- C.E1: head == NULL E2: 1 + getListSize(head->next)
- D.E1: head->next == NULL E2: 1 + getListSize(head)
Open GATE CS 2026 Set 1 Q39 · discuss this question →Answer: B
Solution
Answer: (B) The base case returns 1, so it must fire at the last node, wherehead->next == NULL. Otherwise, count this node and recurse on the rest. • (A), (D): recursing onheaditself never ends. • (C): returns 1 for NULL, which counts one node too many (a 3-node list gives 4). - A.
- GATE CS 2025 Set 1 · Q62Programming & Data Structuresnat2 marksTwo linked lists are given (contents shown). L1 has 9 nodes, L2 has 7 nodes:
The code below modifies L1. The number of nodes in L1 after execution is ________. (Answer in integer)L1: 1 -> 7 -> 12 -> 3 -> 9 -> 5 -> 11 -> 15 -> 8 -> NULL L2: 1 -> 11 -> 6 -> 9 -> 15 -> 12 -> 4 -> NULLint find (int query, LIST *list) { while (list != NULL){ if(list->data == query) return 1; list = list->next; } return 0; } int main () { ptr1 = L1; ptr2 = L2; while (ptr1->next != NULL){ query = ptr1->next->data; if (find (query, L2)) ptr1->next = ptr1->next->next; // unlink else ptr1 = ptr1->next; } return 0; }Open GATE CS 2025 Set 1 Q62 · discuss this question →Answer: 5
Solution
Answer: 5 The loop walks L1 and deletes every node (after the head) whose value also appears in L2. The head node (value 1) is never examined for deletion (the loop looks atptr1->next). L2 as a set . Checking L1's non-head nodes:
Removed: → 4 nodes deleted. Remaining L1 .7 -> keep 12 -> remove 3 -> keep 9 -> remove 5 -> keep 11 -> remove 15 -> remove 8 -> keep - GATE CS 2021 Set 2 · Q45Programming & Data Structuresmcq2 marksConsider the following ANSI C program.
Which one of the statements below is correct about the program?#include <stdio.h> #include <stdlib.h> struct Node { int value; struct Node *next; }; int main() { struct Node *boxE, *head, *boxN; int index = 0; boxE = head = (struct Node *) malloc(sizeof(struct Node)); head->value = index; for (index = 1; index <= 3; index++) { boxN = (struct Node *) malloc(sizeof(struct Node)); boxE->next = boxN; boxN->value = index; boxE = boxN; } for (index = 0; index <= 3; index++) { printf("Value at index %d is %d\n", index, head->value); head = head->next; printf("Value at index %d is %d\n", index + 1, head->value); } }- A.Upon execution, the program creates a linked-list of five nodes.
- B.Upon execution, the program goes into an infinite loop.
- C.It has a missing
returnwhich will be reported as an error by the compiler. - D.It dereferences an uninitialized pointer that may result in a run-time error.✓
Open GATE CS 2021 Set 2 Q45 · discuss this question →Answer: D
Solution
The list has 4 nodes; the last node'snextis never initialized. In the final iterationhead = head->nextfollows that uninitialized pointer and the subsequenthead->valuedereferences it, causing a possible run-time error. - A.
Programming & Data Structures: Structures & Unions — GATE CS PYQ (2)
- GATE CS 2023 · Q13Programming & Data Structuresmcq1 markSLLdel deletes a node in a singly-linked list given a pointer to the node and to the head. DLLdel does the same for a doubly-linked list. With nodes, what is the worst-case time complexity?
- A.SLLdel is O(1) and DLLdel is O(n)
- B.Both are O(log n)
- C.Both are O(1)
- D.SLLdel is O(n) and DLLdel is O(1)✓
Open GATE CS 2023 Q13 · discuss this question →Answer: D
Solution
Answer: (D) SLLdel is O(n) and DLLdel is O(1) In a singly-linked list, deleting a given node needs its predecessor; the copy-next trick works except for the last node, so the worst case is (traverse from head). In a doubly-linked list, the node has a prev pointer, so deletion adjusts prev/next in . - A.
- GATE CS 2022 · Q15Programming & Data Structuresmcq1 markFor reversing a singly linked list in space, which is TRUE about the time complexity?
- A.The best algorithm takes time in the worst case.✓
- B.The best algorithm takes time.
- C.The best algorithm takes time.
- D.It is not possible to reverse a singly linked list in space.
Open GATE CS 2022 Q15 · discuss this question →Answer: A
Solution
Answer: (A) time in the worst case Reversing a singly linked list requires rewiring each of the pointers exactly once using three pointers (prev, curr, next) — extra space and time, which is optimal since every node must be visited. - A.
Take a Programming & Data Structures chapter-wise test
Pick any chapters and attempt their PYQs with a timer in the GATE interface, with instant analysis — free.
Create free account →