GATE PDS questions fall into a small number of recurring patterns. Learn the fastest method for each, then try one question per pattern below. (These are practice questions written for this course in the GATE style, not actual past-paper questions.)
| Pattern | Fastest method |
|---|---|
| Pointer parameter changed inside a function | Draw boxes for each variable; a parameter is a copy — only writes through *p reach the caller. |
| Value returned by a recursive function | Write the recurrence; fill a table from the base case up. |
| Loop / call counting | Count iterations per outer pass, sum the series. |
| Scope and storage class | Mark each block’s x; static = one box for all calls. |
| Recursive tree code | Evaluate bottom-up on a small drawing; check the NULL base case. |
| Counting trees / BSTs / stack outputs | Catalan numbers. |
| Where can a key sit in a heap | Depth ≤ k − 1 for the k-th largest. |
| Graph counting | Handshake lemma; n − k minimum, (n − k)(n − k + 1)/2 maximum. |
| Valid BST search path | Track the open interval. |
| Undefined behavior | Unsequenced double modification, string-literal writes, use after free, out-of-bounds access: no fixed output. |
Practice set
Q1MCQ1 markPattern: pointer parameter
What does this print?
#include <stdio.h>
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int main(void)
{
int i = 0, j = 1;
f(&i, &j);
printf("%d %d\n", i, j);
return 0;
}
- (A)
2 1 - (B)
2 2 - (C)
0 2 - (D)
0 1
Q2NAT2 marksPattern: recursion value
What does this print?
#include <stdio.h>
int f(int n)
{
if (n == 0) return 0;
return f(n / 2) * 10 + n % 2;
}
int main(void)
{
printf("%d\n", f(13));
return 0;
}
Numerical answer: ________
Q3NAT1 markPattern: loop count
What does this print?
#include <stdio.h>
int main(void)
{
int i, j, c = 0;
for (i = 1; i <= 16; i *= 2)
for (j = 0; j < i; j++)
c++;
printf("%d\n", c);
return 0;
}
Numerical answer: ________
Q4MCQ1 markPattern: scope
What does this print?
#include <stdio.h>
int x = 10;
void f(void)
{
int x = 20;
{
int x = 30;
printf("%d ", x);
}
printf("%d ", x);
}
int main(void)
{
f();
printf("%d\n", x);
return 0;
}
- (A)
30 20 10 - (B)
30 30 30 - (C)
30 20 20 - (D)
10 20 30
Q5NAT2 marksPattern: tree code
What does this print?
#include <stdio.h>
#include <stdlib.h>
struct tnode {
int data;
struct tnode *left, *right;
};
struct tnode *insert(struct tnode *t, int x)
{
if (t == NULL) {
t = malloc(sizeof(struct tnode));
t->data = x;
t->left = t->right = NULL;
} else if (x < t->data) t->left = insert(t->left, x);
else t->right = insert(t->right, x);
return t;
}
int h(struct tnode *t)
{
int a, b;
if (t == NULL) return -1;
a = h(t->left);
b = h(t->right);
return (a > b ? a : b) + 1;
}
int main(void)
{
int a[] = {8, 3, 10, 1, 6, 14, 4, 7, 13}, i;
struct tnode *root = NULL;
for (i = 0; i < 9; i++) root = insert(root, a[i]);
printf("%d\n", h(root));
return 0;
}
Numerical answer: ________
Q6NAT2 marksPattern: count structures
How many structurally different binary trees have 6 nodes?
Numerical answer: ________
Q7MCQ2 marksPattern: heap position
In a max-heap of 31 distinct keys (0-based), which index can NOT hold the 4th largest key?
- (A) 3
- (B) 1
- (C) 15
- (D) 7
Q8NAT1 markPattern: graph counting
What is the minimum number of edges in a simple graph with 25 vertices and 5 connected components?
Numerical answer: ________
Q9MCQ2 marksPattern: BST path
Which sequence can be the keys visited while searching a BST for 60?
- (A)
90 30 80 50 70 60 - (B)
20 90 30 85 95 60 - (C)
50 70 40 60 - (D)
90 30 80 50 40 60
Q10MSQ1 markPattern: UB spotting
Which statements have undefined behavior in C?
- (A)
char *p = "ab"; p[0] = 'x'; - (B)
i = i++ + 1; - (C)
int a[3]; a[2] = 5; - (D)
int *p = malloc(4); free(p); free(p);
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (C)1 mark
p now points to j; only j changes.
Output
0 2
- (A) is wrong: i is never written.
- (B) is wrong: Only one variable is written.
- (D) is wrong: j becomes 2.
Re-read: Lesson 1.4.2 — Simulating call by reference with pointers (swap)
Q2 Answer: 11012 marks
The binary digits of 13 as a decimal number.
Output
1101
Re-read: Lesson 2.1.4 — Computing what a recursive function returns via its recurrence; static and global variables inside recursion
Q3 Answer: 311 mark
1 + 2 + 4 + 8 + 16.
Output
31
Re-read: Lesson 1.2.2 — for, while, do-while; break and continue; tracing loop output and counting iterations
Q4 Answer: (A)1 mark
Each block’s x hides the outer one.
Output
30 20 10
- (B) is wrong: The inner x ends with its block.
- (C) is wrong: main sees the global x.
- (D) is wrong: Innermost prints first.
Re-read: Lesson 1.3.2 — Scope vs lifetime; local, global, static, extern, register; shadowing in blocks; static local variables
Q5 Answer: 32 marks
Height in edges: 8 → 3 → 6 → 4.
Output
3
Re-read: Lesson 4.4 — Recursive code on trees: count nodes, leaves, height, mirror, sums; expression trees
Q6 Answer: 1322 marks
C(6).
Re-read: Lesson 4.3.3 — Rebuilding a tree from two traversals; counting distinct binary trees (Catalan)
Q7 Answer: (C)2 marks
Depth ≤ 3 means index ≤ 14.
- (A) is wrong: Depth 2 is allowed.
- (B) is wrong: Depth 1 is allowed.
- (D) is wrong: Depth 3 is allowed.
Re-read: Lesson 5.4 — Heap GATE drills: k-th largest, number of heaps, comparisons, d-ary heaps
Q8 Answer: 201 mark
n − k.
Re-read: Lesson 6.1 — Graph vocabulary
Q9 Answer: (A)2 marks
true,false,false,false.
- (B) is wrong: 95 is above the upper bound 85.
- (C) is wrong: 40 is below the lower bound 50.
- (D) is wrong: 40 is below the lower bound 50.
Re-read: Lesson 4.5.4 — BST GATE drills: BST from a preorder/postorder sequence; number of BSTs; valid search paths; insertion orders giving the same BST
Q10 Answer: (A) (B) (D)1 mark
a[2] = 5 is an ordinary in-bounds write; the other three are undefined.
- (C) is wrong: Index 2 is the last valid element.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior