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.

Programming & Data Structures — average marks per paper
202620252024202320222021
1211.599107

Programming & Data Structures: Trees — GATE CS PYQ (13)

  1. GATE CS 2026 Set 1 · Q33Programming & Data Structuresnat1 mark
    The 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)

    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 Question 33 solution figure
    Open GATE CS 2026 Set 1 Q33 · discuss this question →
  2. GATE CS 2026 Set 1 · Q40Programming & Data Structuresmcq2 marks
    Let 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
      ✓

    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.
    GATE CS 2026 Set 1 Question 40 solution figure
    Open GATE CS 2026 Set 1 Q40 · discuss this question →
  3. GATE CS 2026 Set 1 · Q62Programming & Data Structuresnat2 marks
    The 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.

    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 1 Question 62 solution figure
    Open GATE CS 2026 Set 1 Q62 · discuss this question →
  4. GATE CS 2026 Set 2 · Q12Programming & Data Structuresmcq1 mark
    The set represents various traversals over a binary tree. The set represents the order of visiting nodes during a traversal.
    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
    Which one of the following is the correct match from to ?
    • 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

    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.
    Open GATE CS 2026 Set 2 Q12 · discuss this question →
  5. GATE CS 2026 Set 2 · Q49Programming & Data Structuresmsq2 marks
    Consider 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

    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 ✗
    Open GATE CS 2026 Set 2 Q49 · discuss this question →
  6. GATE CS 2025 Set 1 · Q26Programming & Data Structuresmsq1 mark
    Which 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.

    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.
    Open GATE CS 2025 Set 1 Q26 · discuss this question →
  7. GATE CS 2025 Set 2 · Q13Programming & Data Structuresmcq1 mark
    Consider 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.

    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
    Open GATE CS 2025 Set 2 Q13 · discuss this question →
  8. GATE CS 2025 Set 2 · Q35Programming & Data Structuresnat1 mark
    Suppose 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)

    Answer: 4

    Solution

    Answer: 4 Insertion builds:
    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.
    Path from 19 up to root: = 4 edges.
    Open GATE CS 2025 Set 2 Q35 · discuss this question →
  9. GATE CS 2024 Set 2 · Q39Programming & Data Structuresmcq2 marks
    A 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

    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.
    Open GATE CS 2024 Set 2 Q39 · discuss this question →
  10. GATE CS 2023 · Q47Programming & Data Structuresmcq2 marks
    foo(p) returns p->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?
    GATE CS 2023 Question 47 figure
    • 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

    Answer: C

    Solution

    Answer: (C) 3 8 16 13 24 50 foo computes subtree sums and prints each AFTER recursing (post-order), printing the subtree sum:
    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
    Output: 3 8 16 13 24 50.
    Open GATE CS 2023 Q47 · discuss this question →
  11. GATE CS 2022 · Q28Programming & Data Structuresnat1 mark
    Suppose 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 __________.

    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.
    Open GATE CS 2022 Q28 · discuss this question →
  12. GATE CS 2021 Set 1 · Q20Programming & Data Structuresmcq1 mark
    A 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.
      ✓

    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.
    Open GATE CS 2021 Set 1 Q20 · discuss this question →
  13. GATE CS 2021 Set 2 · Q26Programming & Data Structuresnat1 mark
    Consider 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 ______.

    Answer: 1

    Solution

    Number the nodes (root), (level 1), (level 2). BFS first three: . DFS first three: . , so .
    Open GATE CS 2021 Set 2 Q26 · discuss this question →

Programming & Data Structures: Functions — GATE CS PYQ (9)

  1. GATE CS 2026 Set 1 · Q61Programming & Data Structuresnat2 marks
    Consider the recursive functions represented by the following code segment:
    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));
    }
    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer) Note: Ignore syntax errors (if any) in the function.

    Answer: 65536

    Solution

    Answer: 65536 bar(n) , and foo counts how many times you can apply before reaching 1. That is an iterated logarithm, plus 1. Build the smallest for each value, working backwards:
    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
    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).
    Open GATE CS 2026 Set 1 Q61 · discuss this question →
  2. GATE CS 2026 Set 2 · Q61Programming & Data Structuresnat2 marks
    Consider the following ANSI-C function.
    c
    int 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)); } }
    The maximum possible value that can be returned from this function is __________. (answer in integer)

    Answer: 1

    Solution

    The return value depends only on . Let be the value: - : . - : . - : . - : . Computing: The value never exceeds .
    Open GATE CS 2026 Set 2 Q61 · discuss this question →
  3. GATE CS 2025 Set 1 · Q61Programming & Data Structuresnat2 marks
    Consider the C program:
    #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;
    }
    The value printed by the given C program is _______. (Answer in integer)

    Answer: 5

    Solution

    Answer: 5 foo scans adjacent pairs, adding 1 whenever S[0] != S[1], and adds a final 1 at the size==1 base 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 : .
    Open GATE CS 2025 Set 1 Q61 · discuss this question →
  4. GATE CS 2025 Set 2 · Q63Programming & Data Structuresnat2 marks
    Consider the C program:
    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;
    }
    The output of the given C program is ________. (Answer in integer)

    Answer: 46

    Solution

    Answer: 46 , so . - . - .
    Open GATE CS 2025 Set 2 Q63 · discuss this question →
  5. GATE CS 2024 Set 1 · Q19Programming & Data Structuresmcq1 mark
    Consider the C program (input is 1234 then newline):
    void fX();
    int main(){ fX(); return 0; }
    void fX(){
       char a;
       if((a=getchar()) != '\n') fX();
       if(a != '\n') putchar(a);
    }
    Which statement is CORRECT?
    • 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

    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".
    Open GATE CS 2024 Set 1 Q19 · discuss this question →
  6. GATE CS 2024 Set 1 · Q48Programming & Data Structuresmsq2 marks
    runs 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
      ✓

    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.
    Open GATE CS 2024 Set 1 Q48 · discuss this question →
  7. GATE CS 2024 Set 2 · Q13Programming & Data Structuresmcq1 mark
    Parameters are evaluated right to left.
    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)); }
    Correct output?
    • A.
      20101020
      ✓
    • B.
      10202010
    • C.
      20102010
    • D.
      10201020

    Answer: A

    Solution

    Answer: (A) 20101020 f(g(10), h(20)): arguments evaluated right-to-left, so h(20) prints 20, then g(10) prints 10 -> "2010". Then inside f, g(x) prints 10, h(y) prints 20 -> "1020". Total: "2010"+"1020" = "20101020".
    Open GATE CS 2024 Set 2 Q13 · discuss this question →
  8. GATE CS 2021 Set 2 · Q33Programming & Data Structuresnat1 mark
    Consider the following ANSI C function.
    int 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);
    }
    The value returned by SomeFunction(15, 255) is ______.

    Answer: 15

    Solution

    The function performs the subtractive Euclidean algorithm, returning (with 1 as the base case). .
    Open GATE CS 2021 Set 2 Q33 · discuss this question →
  9. GATE CS 2021 Set 2 · Q59Programming & Data Structuresnat2 marks
    Consider the following ANSI C program.
    #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;
    }
    The output of the program upon execution is ______.

    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.
    Open GATE CS 2021 Set 2 Q59 · discuss this question →

Programming & Data Structures: Heaps — GATE CS PYQ (8)

  1. GATE CS 2026 Set 1 · Q23Programming & Data Structuresmsq1 mark
    Let 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.

    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.
    Open GATE CS 2026 Set 1 Q23 · discuss this question →
  2. GATE CS 2025 Set 1 · Q35Programming & Data Structuresnat1 mark
    The 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)

    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.)
    Open GATE CS 2025 Set 1 Q35 · discuss this question →
  3. GATE CS 2025 Set 2 · Q38Programming & Data Structuresmcq2 marks
    A 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:

    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.
    Open GATE CS 2025 Set 2 Q38 · discuss this question →
  4. GATE CS 2024 Set 1 · Q41Programming & Data Structuresmcq2 marks
    The 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
      ✓

    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.
    Open GATE CS 2024 Set 1 Q41 · discuss this question →
  5. GATE CS 2024 Set 1 · Q43Programming & Data Structuresmcq2 marks
    A 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

    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 .
    Open GATE CS 2024 Set 1 Q43 · discuss this question →
  6. GATE CS 2023 · Q12Programming & Data Structuresmcq1 mark
    Which 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

    Answer: B

    Solution

    Answer: (B) 23, 17, 14, 7, 13, 10, 1, 5, 6, 12 A max-heap requires and . Checking (B):
    23>=17,14 | 17>=7,13 | 14>=10,1 | 7>=5,6 | 13>=12
    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.
    Open GATE CS 2023 Q12 · discuss this question →
  7. GATE CS 2023 · Q46Programming & Data Structuresmcq2 marks
    A 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).

    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.
    Open GATE CS 2023 Q46 · discuss this question →
  8. GATE CS 2021 Set 2 · Q12Programming & Data Structuresmcq1 mark
    Consider 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.

    Answer: C

    Solution

    In a min-heap the maximum can be any of the leaves, so all leaves must be examined, requiring time.
    Open GATE CS 2021 Set 2 Q12 · discuss this question →

Programming & Data Structures: Basics & Control Structures — GATE CS PYQ (8)

  1. GATE CS 2026 Set 2 · Q19Programming & Data Structuresmcq1 mark
    Consider the following three ANSI-C programs, P1, P2, and P3.
    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); }
    Which one of the following statements is true?
    • 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

    Answer: A

    Solution

    Answer: (A) In P1 the local int a simply shadows the global a — legal. In P2 and P3 the identifier a is redeclared in the same scope (as int/int and int/float), which is a compile-time error. Hence only P1 compiles.
    Open GATE CS 2026 Set 2 Q19 · discuss this question →
  2. GATE CS 2025 Set 1 · Q63Programming & Data Structuresnat2 marks
    Consider the C program:
    #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; }
    The value printed by the given C program is _______. (Answer in integer)

    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 : .
    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
    So it keeps the 2nd and 4th digits → .
    Open GATE CS 2025 Set 1 Q63 · discuss this question →
  3. GATE CS 2025 Set 2 · Q33Programming & Data Structuresnat1 mark
    Consider the C code:
    int x=126, y=105;
    do {
        if(x>y) x=x-y;
        else    y=y-x;
    } while(x!=y);
    printf("%d", x);
    The output of the given C code segment is ________. (Answer in integer)

    Answer: 21

    Solution

    Answer: 21 This is Euclid's subtraction method, which computes :
    126,105 -> 21,105 -> 21,84 -> 21,63 -> 21,42 -> 21,21  (stop)
    , so it prints .
    Open GATE CS 2025 Set 2 Q33 · discuss this question →
  4. GATE CS 2024 Set 1 · Q18Programming & Data Structuresmcq1 mark
    Consider the C program:
    int main(){
       int a = 6;
       int b = 0;
       while(a < 10) {
          a = a / 12 + 1;
          a += b;
       }
       printf("%d", a);
       return 0;
    }
    Which statement is CORRECT?
    • A.
      The program prints 9
    • B.
      The program prints 10
    • C.
      The program gets stuck in an infinite loop
      ✓
    • D.
      The program prints 6

    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.
    Open GATE CS 2024 Set 1 Q18 · discuss this question →
  5. GATE CS 2023 · Q35Programming & Data Structuresnat1 mark
    The 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));
    }

    Answer: 7

    Solution

    Answer: 7 x = funcp(): static x becomes 2, returns 2, so x=2. y = funcp()+x: static x becomes 3, returns 3, so y=3+2=5. Print x+y = 2+5 = 7.
    Open GATE CS 2023 Q35 · discuss this question →
  6. GATE CS 2022 · Q44Programming & Data Structuresmcq2 marks
    What 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 +

    Answer: A

    Solution

    Answer: (A) z K S ASCII: P=80 (01010000), x=120 (01111000). - ; 'z'. - ; 'K'. - ; 'S'. Printed as characters: "z K S".
    Open GATE CS 2022 Q44 · discuss this question →
  7. GATE CS 2022 · Q62Programming & Data Structuresnat2 marks
    Queue 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 __________.

    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).
    Open GATE CS 2022 Q62 · discuss this question →
  8. GATE CS 2021 Set 1 · Q47Programming & Data Structuresmcq2 marks
    Consider the following ANSI C program.
    #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;
    }
    Which one of the following options is correct?
    • 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.

    Answer: B

    Solution

    For the first operand is false and i++ 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 .
    Open GATE CS 2021 Set 1 Q47 · discuss this question →

Programming & Data Structures: Pointers — GATE CS PYQ (7)

  1. GATE CS 2026 Set 2 · Q60Programming & Data Structuresnat2 marks
    Consider the following ANSI-C program.
    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); }
    The output of this program is __________. (answer in integer)

    Answer: 9

    Solution

    ptr=&a; *ptr=c; sets . ptr=&c; now points to . a=*(&b); sets . c=*ptr-a . So the program prints .
    Open GATE CS 2026 Set 2 Q60 · discuss this question →
  2. GATE CS 2025 Set 1 · Q34Programming & Data Structuresnat1 mark
    Consider the C program:
    #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;
    }
    The output of the given C program is __________. (Answer in integer)

    Answer: 25

    Solution

    Answer: 25 z points to a. The call foo(z, b) passes this pointer and the value b = 25. Inside foo, *p = x writes 25 through the pointer, i.e. into a. So a becomes 25, and printf("%d", a) prints .
    Open GATE CS 2025 Set 1 Q34 · discuss this question →
  3. GATE CS 2025 Set 2 · Q19Programming & Data Structuresmcq1 mark
    Consider the C 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++;
    }
    Which ONE of the following will be the output of the program?
    • A.
      @#Hello World!
    • B.
      Hello World!
    • C.
      ello World!
    • D.
      Hello World!d!
      ✓

    Answer: D

    Solution

    Answer: (D) Hello World!d! t=a+2 points at 'H'; s=a. The loop copies *t into *s while *t is non-zero, advancing both. It copies the 12 characters "Hello World!" from a[2..13] into a[0..11]. The loop stops at the null terminator but does NOT copy it, so no new '\0' is written. The old characters a[12]='d', a[13]='!', and the terminator a[14]='\0' remain. Printing a therefore reads a[0..13] = "Hello World!" followed by the leftover "d!" → "Hello World!d!".
    Open GATE CS 2025 Set 2 Q19 · discuss this question →
  4. GATE CS 2025 Set 2 · Q62Programming & Data Structuresnat2 marks
    Consider the C program:
    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;
    }
    The output of the above program is ___________. (Answer in integer)

    Answer: 111

    Solution

    Answer: 111 arr = {30,50,10,0,0}. ptr = &arr[0]+1 = &arr[1], so a = *ptr = 50. Then (*ptr)++ makes arr[1] = 51. ptr++ now points to &arr[2], where *ptr = 10. Output
    Open GATE CS 2025 Set 2 Q62 · discuss this question →
  5. GATE CS 2024 Set 2 · Q36Programming & Data Structuresmcq2 marks
    double a[2]={20.0,25.0},*p,*q;
    p=a; q=p+1;
    printf("%d,%d",(int)(q-p),(int)(*q-*p));
    Output?
    • A.
      4,8
    • B.
      1,5
      ✓
    • C.
      8,5
    • D.
      1,8

    Answer: B

    Solution

    Answer: (B) 1,5 q-p is pointer difference in units of elements . *q-*p = 25.0-20.0 = 5.0, cast to int . Output: "1,5".
    Open GATE CS 2024 Set 2 Q36 · discuss this question →
  6. GATE CS 2022 · Q21Programming & Data Structuresmcq1 mark
    What 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
      ✓

    Answer: D

    Solution

    Answer: (D) 10, 10, 14 *p=10 with p=&x sets . &z[0]+1 is &z[1], so *(&z[0]+1) += 3 makes . is unchanged (10). Output: "10, 10, 14".
    Open GATE CS 2022 Q21 · discuss this question →
  7. GATE CS 2021 Set 2 · Q20Programming & Data Structuresmcq1 mark
    Consider the following ANSI C program.
    #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;
    }
    The output of the program is ______.
    • A.
      14
    • B.
      20
    • C.
      24
      ✓
    • D.
      30

    Answer: C

    Solution

    arr[1] points to arr[1][0]. Adding 9 (row-major) reaches element at offset 9, i.e. arr[2][4] (row length 5: ). Its value is .
    Open GATE CS 2021 Set 2 Q20 · discuss this question →

Programming & Data Structures: Advanced Concepts — GATE CS PYQ (6)

  1. GATE CS 2026 Set 1 · Q24Programming & Data Structuresmsq1 mark
    Consider 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

    Answer: C

    Solution

    Answer: C (position 2 only)
    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
    Among the options only 2 is empty. The trap is the wrap-around from 10 back to 0 when placing 24.
    GATE CS 2026 Set 1 Question 24 solution figure
    Open GATE CS 2026 Set 1 Q24 · discuss this question →
  2. GATE CS 2026 Set 1 · Q34Programming & Data Structuresnat1 mark
    Consider the following program in C:
    #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;
    }
    The output of the program is _________. (answer in integer) Note: Assume that the program compiles and runs successfully.

    Answer: 9

    Solution

    Answer: 9 Inside the if block, int i = 0; declares a new i that shadows the parameter. The loop changes only that inner i (and j). The inner i goes out of scope when the block ends, so printf sees the parameter i = 9.
    Open GATE CS 2026 Set 1 Q34 · discuss this question →
  3. GATE CS 2026 Set 2 · Q17Programming & Data Structuresmcq1 mark
    In 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

    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 via malloc() is allocated on the heap.
    Open GATE CS 2026 Set 2 Q17 · discuss this question →
  4. GATE CS 2026 Set 2 · Q30Programming & Data Structuresnat1 mark
    The 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)

    Answer: 3

    Solution

    Slots (): . Chains: slot 1 = (length 3), slot 6 = , slot 8 = . Longest chain length .
    Open GATE CS 2026 Set 2 Q30 · discuss this question →
  5. GATE CS 2025 Set 1 · Q65Programming & Data Structuresnat2 marks
    In 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)

    Answer: 10 to 11

    Solution

    Answer: 10 Insert each key (slots –):
    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
    For key 24: (occupied by 79), . - (occupied by 50). - (empty) → slot 10. Key 24 is stored at slot .
    Open GATE CS 2025 Set 1 Q65 · discuss this question →
  6. GATE CS 2022 · Q16Programming & Data Structuresmcq1 mark
    Given 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.

    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.
    Open GATE CS 2022 Q16 · discuss this question →

Programming & Data Structures: Stacks & Queues — GATE CS PYQ (5)

  1. GATE CS 2026 Set 2 · Q50Programming & Data Structuresmsq2 marks
    Consider 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.

    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) ✗
    Open GATE CS 2026 Set 2 Q50 · discuss this question →
  2. GATE CS 2025 Set 2 · Q45Programming & Data Structuresmsq2 marks
    We 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.

    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.
    Open GATE CS 2025 Set 2 Q45 · discuss this question →
  3. GATE CS 2024 Set 2 · Q48Programming & Data Structuresmsq2 marks
    S1 (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
      ✓

    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.
    Open GATE CS 2024 Set 2 Q48 · discuss this question →
  4. GATE CS 2023 · Q59Programming & Data Structuresnat2 marks
    Sequence . 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 __________

    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.
    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)
    Top of S is 8.
    Open GATE CS 2023 Q59 · discuss this question →
  5. GATE CS 2021 Set 1 · Q31Programming & Data Structuresnat1 mark
    Consider the following sequence of operations on an empty stack.
    push(54); push(52); pop(); push(55); push(62); s = pop();
    Consider the following sequence of operations on an empty queue.
    enqueue(21); enqueue(24); dequeue(); enqueue(28); enqueue(32); q = dequeue();
    The value of is ______.

    Answer: 86

    Solution

    Stack: after the operations the top popped into is 62. Queue (FIFO): the second dequeue returns 24, so . Hence .
    Open GATE CS 2021 Set 1 Q31 · discuss this question →

Programming & Data Structures: Arrays & Strings — GATE CS PYQ (4)

  1. 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
      ✓

    Answer: A, B, D

    Solution

    Answer: (A), (B) and (D) fX computes 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.
    Open GATE CS 2024 Set 2 Q33 · discuss this question →
  2. GATE CS 2024 Set 2 · Q42Programming & Data Structuresmcq2 marks
    A 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

    Answer: B

    Solution

    Answer: (B) (P) 1 (Q) len1+1 When X[i] equals the second-most-recent distinct value (the else if branch), the run of equal values restarts at this element, so len1 = 1 (P). When X[i] is a brand-new third value (the else branch), the at-most-two-distinct window can only extend the previous equal-run plus this element, so len2 = len1+1 (Q).
    Open GATE CS 2024 Set 2 Q42 · discuss this question →
  3. GATE CS 2022 · Q43Programming & Data Structuresmcq2 marks
    What 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

    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.
    Open GATE CS 2022 Q43 · discuss this question →
  4. GATE CS 2021 Set 1 · Q58Programming & Data Structuresnat2 marks
    Consider the following ANSI C function:
    int 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;
    }
    Let Z be an array of 10 elements with Z[i]=1, for all such that . The value returned by SimpleFunction(Z, 10, 2) is ______.

    Answer: 1023

    Solution

    This is Horner evaluation of the polynomial with all coefficients 1 at : .
    Open GATE CS 2021 Set 1 Q58 · discuss this question →

Programming & Data Structures: Arrays & Linked Lists — GATE CS PYQ (3)

  1. GATE CS 2026 Set 1 · Q39Programming & Data Structuresmcq2 marks
    Consider 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.
    struct node{
       int elt;
       struct node *next;
    };
    
    int getListSize (struct node *head)
    {
      if( E1 ) return 1;
      return E2;
    }
    Which one of the following options gives the correct replacements for the expressions E1 and 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)

    Answer: B

    Solution

    Answer: (B) The base case returns 1, so it must fire at the last node, where head->next == NULL. Otherwise, count this node and recurse on the rest. • (A), (D): recursing on head itself never ends. • (C): returns 1 for NULL, which counts one node too many (a 3-node list gives 4).
    Open GATE CS 2026 Set 1 Q39 · discuss this question →
  2. GATE CS 2025 Set 1 · Q62Programming & Data Structuresnat2 marks
    Two linked lists are given (contents shown). L1 has 9 nodes, L2 has 7 nodes:
    L1: 1 -> 7 -> 12 -> 3 -> 9 -> 5 -> 11 -> 15 -> 8 -> NULL
    L2: 1 -> 11 -> 6 -> 9 -> 15 -> 12 -> 4 -> NULL
    The code below modifies L1. The number of nodes in L1 after execution is ________. (Answer in integer)
    int 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;
    }

    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 at ptr1->next). L2 as a set . Checking L1's non-head nodes:
    7  -> keep     12 -> remove   3  -> keep
    9  -> remove   5  -> keep     11 -> remove
    15 -> remove   8  -> keep
    Removed: → 4 nodes deleted. Remaining L1 .
    Open GATE CS 2025 Set 1 Q62 · discuss this question →
  3. GATE CS 2021 Set 2 · Q45Programming & Data Structuresmcq2 marks
    Consider the following ANSI C 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);
        }
    }
    Which one of the statements below is correct about the program?
    • 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 return which will be reported as an error by the compiler.
    • D.
      It dereferences an uninitialized pointer that may result in a run-time error.
      ✓

    Answer: D

    Solution

    The list has 4 nodes; the last node's next is never initialized. In the final iteration head = head->next follows that uninitialized pointer and the subsequent head->value dereferences it, causing a possible run-time error.
    Open GATE CS 2021 Set 2 Q45 · discuss this question →

Programming & Data Structures: Structures & Unions — GATE CS PYQ (2)

  1. GATE CS 2023 · Q13Programming & Data Structuresmcq1 mark
    SLLdel 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)
      ✓

    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 .
    Open GATE CS 2023 Q13 · discuss this question →
  2. GATE CS 2022 · Q15Programming & Data Structuresmcq1 mark
    For 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.

    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.
    Open GATE CS 2022 Q15 · discuss this question →

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 →

More GATE CS PYQ subject-wise