chapter

    Programming and Data Structures Pyqs for Gate Cs

    GATE CS Programming and Data Structures: 1 units and 7 chapters, weightage from 66 previous year questions across 10 papers, a study order by exam weight and

    A question from this chapter

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following three ANSI-C programs, P1, P2, and P3.

    P1P2P3
    #include <stdio.h>
    int a=5;
    int main(){
      int a=7;
      return(0);
    }
    #include <stdio.h>
    int main(){
      int a=5;
      int a=7;
      return(0);
    }
    #include <stdio.h>
    int main(){
      int a=5;
      float a=7;
      return(0);
    }


    Which one of the following statements is true?
    Question 2
    2026 Slot Set2 PYQ
    Level 3: Exam Standard

    In C runtime environment, which one of the following is stored in heap?

    Question 3
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider the following ANSI-C function.

    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)

    Note: Ignore syntax errors (if any) in the function.
    Question 4
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    Consider a stack and a queue . Both of them are initially empty and have the capacity to store ten elements each. The elements 1, 2, 3, 4, and 5 arrive one by one, in that order. When an element arrives, it is assigned either to (pushed on ) or to (enqueued to ). Once all the five elements are stored, the output is generated in two steps. First, stack S is emptied by popping all elements. Then queue is emptied by dequeueing all elements. The output obtained by following this process is 4 3 1 2 5 .
    Given the output, the objective is to predict whether an element was assigned to or .

    Which of the following options is/are possible valid assignment(s) of the elements?

    Note: In the options, the notation denotes that element was assigned to and denotes that element was assigned to .
    Question 5
    2026 Slot Set2 PYQ
    Level 3: Exam Standard
    The set T represents various traversals over binary tree. The set S represents the order of visiting nodes during a traversal.

    TS
    I: InorderL: left subtree, node, right subtree
    II: PreorderM: node, left subtree, right subtree
    III: PostorderN: left subtree, right subtree, node


    Which one of the following is the correct match from T to S ?
    Question 6
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    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?
    Question 7
    2026 Slot Set2 PYQ
    Level 3: Exam Standard

    The keys 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 __________. <i>(answer in integer)</i>

    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Programming and Data Structures PYQs for GATE CS

    GATE CS Programming and Data Structures: 1 units and 7 chapters, weightage from 66 previous year questions across 10 papers, a study order by exam weight and 1831 practice questions.

    About Programming and Data Structures Previous Year Questions (PYQs)

    66 previous year questions from Programming and Data Structures in GATE CS, grouped by chapter with the exam year, answer key and step-by-step solution for each.

    GATE CS Programming and Data Structures Unit-wise Weightage from Past Papers

    We counted every GATE CS Programming and Data Structures previous year question in our bank (66 questions from 10 papers) and grouped them by unit.

    UnitChaptersPYQsShare of sectionAvg per paper
    Programming and Data Structures766100%6.6

    Suggested Programming and Data Structures Study Order for GATE CS

    1. Programming and Data Structures: 100% of past Programming and Data Structures questions, about 6.6 per paper.

    Start where the marks are. Units at the top of this list have appeared most often in past GATE CS papers.

    Units in GATE CS Programming and Data Structures

    All Programming and Data Structures chapters

    One Solved Question from Each Programming and Data Structures Chapter

    Question 1 · C Programming, Control Flow and Array Algorithms · 2026_Set2 MCQ
    Consider the following three ANSI-C programs, P1, P2, and P3.

    P1P2P3
    #include <stdio.h>
    int a=5;
    int main(){
      int a=7;
      return(0);
    }
    #include <stdio.h>
    int main(){
      int a=5;
      int a=7;
      return(0);
    }
    #include <stdio.h>
    int main(){
      int a=5;
      float a=7;
      return(0);
    }


    Which one of the following statements is true?
    1. A.

      Only P1 will compile without any error

    2. B.

      Only P2 will compile without any error

    3. C.

      Only P3 will compile without any error

    4. D.

      All three programs P1, P2, and P3 will compile without any error

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: C allows variable shadowing across different scopes but forbids redeclaration within the same scope, regardless of type.

    Exam route: Check the scope of each variable declaration. Global vs local is shadowing. Two locals in the same block is redeclaration.

    Learning route:

    1. P1: int a=5; is at file scope. int a=7; is inside main (block scope). The local a shadows the global a. This is perfectly valid C and compiles without error.
    2. P2: int a=5; and int a=7; are both declared inside the exact same block scope (main). This is a redeclaration error. The compiler will reject it.
    3. P3: int a=5; and float a=7; are both in the same block scope. Even though the types differ, C does not allow overloading or redeclaration with different types in the same scope. This is a compilation error.
    4. Therefore, only P1 compiles successfully.

    Trap: Believing that shadowing causes a compilation error or that different types in the same scope are allowed. Shadowing is strictly cross-scope; redeclaration is strictly intra-scope.

    Verification: Compile P1 mentally: global a exists, local a hides it. No conflict. P2: compiler sees two as in main's symbol table -> error. P3: same symbol table, conflicting types -> error.

    Question 2 · Pointers, Arrays, Strings and Memory Management · 2026_Set2 MCQ

    In C runtime environment, which one of the following is stored in heap?

    1. A.

      A static variable declared inside a function

    2. B.

      An array of integers declared inside a function

    3. C.

      A dynamically allocated array of integers created using malloc() function call

    4. D.

      Return address of a function

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: This is a memory layout classification question, recognizable by asking where specific C constructs reside in the runtime environment.

    Exam route: Eliminate options based on standard C memory segments. Static variables go to Data/BSS. Local arrays and return addresses go to Stack. Only malloc targets the Heap.

    Learning route:

    1. Static variable inside a function: Stored in the Data segment (or BSS if uninitialized), not the Heap.
    2. Array of integers declared inside a function: This is a local variable, allocated on the Stack.
    3. Dynamically allocated array using malloc(): Explicitly requests memory from the Heap at runtime.
    4. Return address of a function: Pushed onto the Stack as part of the function call frame.

    Therefore, only the dynamically allocated array resides in the Heap.

    Question 3 · Recursion and Recursive Program Analysis · 2026_Set2 NAT
    Consider the following ANSI-C function.

    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)

    Note: Ignore syntax errors (if any) in the function.
    Correct Answer:

    1.00

    Step-by-Step Solution

    Insight: The function's return value depends on length % 3. The only branch that adds 1 is length % 3 == 1, which transitions the length to length - 1 (residue 0). From residue 0, the sequence of residues is 0 -> 2 -> 0 -> 2..., never returning to 1. Thus, 1 is added at most once.

    Exam route: Let be the initial length. If : adds 1, new . If : adds 0, new . If : adds 0, new . The residue 1 is visited at most once. The maximum return value is 1.

    Learning route:

    1. This is a modulo state-transition recursion question, recognisable because the branch taken and the amount subtracted from the effective length depend on length % 3.
    2. Define . The base case returns 0 if or if an index is negative.
    3. Determine how each branch changes :
    • If , the call is func(start+1, end), so new length is .
    • If , the return is func(start, end-1), so new length is .
    • If , the call is func(start+2, end), so new length is .
    1. Convert to residue transitions:

    add 1, .

    add 0, .

    add 0, .

    1. The only adding state is residue 1. If the computation starts in residue 1, it adds once and moves to residue 0. From 0, it goes to 2, then back to 0, never visiting 1 again.
    2. Therefore, the maximum possible value returned is 1.

    Tempting wrong path: assume the function can add 1 multiple times by cycling through residues. This breaks because the transitions are strictly , and residue 1 is never revisited.

    Verification: If (residue 1), it adds 1, new (residue 0). Next, adds 0, new (residue 2). Next, adds 0, new . Base case returns 0. Total sum = 1.

    Question 4 · Stacks, Queues and Linked Lists · 2026_Set2 MSQ
    Consider a stack and a queue . Both of them are initially empty and have the capacity to store ten elements each. The elements 1, 2, 3, 4, and 5 arrive one by one, in that order. When an element arrives, it is assigned either to (pushed on ) or to (enqueued to ). Once all the five elements are stored, the output is generated in two steps. First, stack S is emptied by popping all elements. Then queue is emptied by dequeueing all elements. The output obtained by following this process is 4 3 1 2 5 .
    Given the output, the objective is to predict whether an element was assigned to or .

    Which of the following options is/are possible valid assignment(s) of the elements?

    Note: In the options, the notation denotes that element was assigned to and denotes that element was assigned to .
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Insight: This is a stack-queue stream splitting question, recognizable because it asks to deduce the routing of elements into a stack and a queue given the final concatenated output.

    Exam route: Partition the output into a stack segment (reversed arrival) and a queue segment (original arrival), then interleave them to match 1, 2, 3, 4, 5.

    Learning route:

    Step 1: The final output is 4, 3, 1, 2, 5. This is formed by emptying the stack (LIFO) followed by emptying the queue (FIFO).

    Step 2: We must partition the output into a stack segment and a queue segment.

    Partition A: Stack outputs 4, 3, 1. Queue outputs 2, 5.

    For the stack to output 4, 3, 1, the elements must have been pushed in the order 1, 3, 4.

    For the queue to output 2, 5, the elements must have been enqueued in the order 2, 5.

    Interleaving 1, 3, 4 and 2, 5 to match the arrival order 1, 2, 3, 4, 5 gives: 1->S, 2->Q, 3->S, 4->S, 5->Q. This matches option A.

    Partition B: Stack outputs 4, 3. Queue outputs 1, 2, 5.

    For the stack to output 4, 3, the elements must have been pushed in the order 3, 4.

    For the queue to output 1, 2, 5, the elements must have been enqueued in the order 1, 2, 5.

    Interleaving 3, 4 and 1, 2, 5 to match 1, 2, 3, 4, 5 gives: 1->Q, 2->Q, 3->S, 4->S, 5->Q. This matches option B.

    Step 3: Verify other partitions. If stack outputs 4, queue outputs 3, 1, 2, 5. The queue arrival order would be 3, 1, 2, 5, which violates the increasing arrival order. Thus, only A and B are valid.

    Question 5 · Binary Trees, Binary Search Trees and Traversals · 2026_Set2 MCQ
    The set T represents various traversals over binary tree. The set S represents the order of visiting nodes during a traversal.

    TS
    I: InorderL: left subtree, node, right subtree
    II: PreorderM: node, left subtree, right subtree
    III: PostorderN: left subtree, right subtree, node


    Which one of the following is the correct match from T to S ?
    1. A.

      I – L, II – M, III – N

    2. B.

      I – M, II – L, III – N

    3. C.

      I – N, II – M, III – L

    4. D.

      I – L, II – N, III – M

    Correct Answer:

    A

    Step-by-Step Solution

    Insight: The names of the traversals (pre, in, post) directly indicate the position of the current node relative to its subtrees.

    Exam route: Preorder = Node first (pre). Inorder = Node in the middle (in). Postorder = Node last (post). Match these to the given descriptions: I (Inorder) is L (left, node, right). II (Preorder) is M (node, left, right). III (Postorder) is N (left, right, node). This matches option A.

    Learning route:

    1. Depth-first traversals are defined by when the current node is processed relative to its left and right subtrees.
    2. Preorder: The prefix "pre" means the node is visited before its subtrees. Order: Node, Left, Right (M).
    3. Inorder: The prefix "in" means the node is visited in between its left and right subtrees. Order: Left, Node, Right (L).
    4. Postorder: The prefix "post" means the node is visited after both subtrees. Order: Left, Right, Node (N).
    5. Matching these definitions to the given set S yields I-L, II-M, III-N.

    Verification: Consider a tree with root A, left B, right C. Preorder visits A, then B, then C (Node, Left, Right). Inorder visits B, then A, then C (Left, Node, Right). Postorder visits B, then C, then A (Left, Right, Node). This perfectly matches M, L, N respectively.

    Question 6 · Heaps, Priority Queues and Data Structure Operations · 2026_Set1 MSQ
    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?
    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    ["B","C"]

    Step-by-Step Solution

    Insight: In a complete binary tree with nodes, the leaf nodes are exactly those at indices to . The internal nodes are $1$ to . We just need to check which options fall in the internal node range.

    Exam route:

    1. Identify the range of leaf indices for a heap of size : from to .
    2. Since is odd, let . The internal nodes are $1$ to , and leaves are to .
    3. Evaluate each option:
    • (Leaf)
    • (Internal)
    • (Internal)
    • (Leaf)
    1. The question asks for indices that do NOT correspond to leaf nodes, which are the internal nodes.
    2. These are and .

    Learning route:

    1. A binary heap is a complete binary tree.
    2. The array representation maps the tree level by level.
    3. For an array of size , the parent of node is .
    4. A node is a leaf if it has no children, i.e., .
    5. Thus, internal nodes are .
    6. For odd , .
    7. So internal nodes are .
    8. Option A is , which is , so it's a leaf.
    9. Option B is , which is , so it's an internal node.
    10. Option C is , which is , so it's an internal node.
    11. Option D is , which is the last element, always a leaf.
    12. Therefore, B and C do not correspond to leaf nodes.
    Question 7 · Hashing and Collision Resolution · 2026_Set2 NAT

    The keys 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 __________. <i>(answer in integer)</i>

    Correct Answer:

    3.00

    Step-by-Step Solution

    Insight: In separate chaining, the longest chain length is simply the maximum frequency of any hash value (bucket size), not the total number of collisions.

    Exam route: Compute for all 9 keys, tally the frequencies of each remainder, and pick the maximum frequency.

    Learning route:

    The hash function is .

    Compute the bucket for each key:

    Tallying the remainders:

    • Index 0: 0 keys
    • Index 1: 28, 19, 10 (3 keys)
    • Index 2: 0 keys
    • Index 3: 12 (1 key)
    • Index 4: 0 keys
    • Index 5: 5 (1 key)
    • Index 6: 15, 33 (2 keys)
    • Index 7: 0 keys
    • Index 8: 26, 17 (2 keys)

    The maximum number of keys in any single bucket is 3 (at index 1). Thus, the length of the longest chain is 3.