0% found this document useful (0 votes)
2 views37 pages

Master C programming - Coding questions

The document outlines a structured learning approach called 'The Zone of Ankit,' which includes various levels of programming problems in C, categorized into Level 01, Level 02, and Carnation Level. It emphasizes solving theoretical questions and practical problems to master topics, particularly focusing on fundamentals, functions, arrays, structures, pointers, and dynamic memory allocation. Additionally, it includes past year questions (PYQs) and programming tasks to enhance understanding and skills in C programming.

Uploaded by

carnationethics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views37 pages

Master C programming - Coding questions

The document outlines a structured learning approach called 'The Zone of Ankit,' which includes various levels of programming problems in C, categorized into Level 01, Level 02, and Carnation Level. It emphasizes solving theoretical questions and practical problems to master topics, particularly focusing on fundamentals, functions, arrays, structures, pointers, and dynamic memory allocation. Additionally, it includes past year questions (PYQs) and programming tasks to enhance understanding and skills in C programming.

Uploaded by

carnationethics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

The Zone of Ankit

About the Zone: [Read before you start]


After the completion of each module, you must open this pdf and solve the respective module
problems. The theorical questions are all PYQs that is not included in the first pdf. The level
problems marked in yellow are all PYQs. The problems of default color (white) are the advanced
level questions of that particular topic. The CARNATION LEVEL is the most difficult one....

Zone of Ankit
LEVEL 01
LEVEL 02
Carnation level

The zone of Ankit is my zone, and hence I'll challenge you to take on the toughest problems from
each topic. If you can solve the Level 01 and Level 02 questions, I assure you that you will score
full grade in the examination.
And if you manage to crack the Carnation Level, congratulations—you’ve mastered that topic!
Hold your seat... it'll be fun...

MODULE I: Fundamentals of C
PYQ - When is do-while loop preferred over while loop, Give example? [2020 - 2M]
A do-while loop is preferred over a while loop when you want the loop body to execute at least
once, regardless of the condition. This is because a do-while loop checks the condition at the end
of the loop body, ensuring that the body executes at least once.

PYQ - Differentiate between compilation error and logical error. [2022 - 2M]
PYQ - Can we compile and run a program without the main() function. [2022 - 2M]
You can write a C program without the main() function, and it will still compile successfully.

The compiler does not enforce the presence of the main() function.

Linking: However, the linking process will fail because the linker expects the main() function

as the entry point of the program.

Execution: Without main() , the program cannot be executed since there is no entry point

defined. The operating system will not know where to start the execution.

PYQ - Difference between while and for loop [2024 - 2M]

for while
Loop Loop

Initialization Declared outside the


Declared within the loop; should be done
loop structure and explicitly before the loop.
executed once at
the beginning.

Update Executed inside the loop;


needs to be handled
Executed after each explicitly.
iteration.

Use Cases Useful when the number


Suitable for a of iterations is not known
known number of in advance or based on a
iterations or when condition.
looping over
ranges.

Initialization and Scope extends beyond


Update Scope the loop; needs to be
Limited to the loop handled explicitly.
body.

PYQ - Explain the use of static variable. [2022 - 2M]


Following is some use of static variable:
When a function needs to remember its previous state between calls, static variables are

used.

When you want a global variable that is only accessible within a single file (for encapsulation

and avoiding naming conflicts).

Static variables can keep track of the number of times a function is called or other metrics.

Zone of Ankit
LEVEL 01
1. Write a C program to calculate the grade of a student by considering the following range of
marks using switch-case statement. [2018 -10M]
O, 90 ≤ Marks ≤100
E, 80 ≤Marks < 90
A, 70 ≤Marks < 80
B, 60 ≤Marks < 70
C, 50 ≤Marks < 60
D, 40 ≤Marks < 50
F, Otherwise
2. Write a program in C to print pascal's triangle for 15 rows.
1
11
121
1331
14641

3. Write a program to print the first 50 Fibonacci numbers.

4. Write a program to print "Hello, World!" in C.

5. Write a program to take input from the user and display it.

6. Write a program to add two numbers.

7. Write a program to find the largest of three numbers.

8. Write a program to find the factorial of a number using a for loop.

9. Write a program to check if a number is even or odd.

10. Write a program to print the multiplication table of a number.

11. Write a program to convert Celsius to Fahrenheit.

12. Write a program to find the sum of natural numbers up to a given limit.

13. Write a program to check if a character is a vowel or consonant.

14. Write a program to find the ASCII value of a character.

15. Write a program to compute the simple interest.

16. Write a program to swap two numbers using a temporary variable.

17. Write a program to demonstrate the use of switch-case.

18. Write a program to find the sum of digits of a number.


LEVEL 02
1. Write suitable functions or procedures to perform the following string operations: [2019 -
6M]
a. Check whether a string is empty.
b. Comparing whether two strings are identical.
c. Reverse of a string.
2. Write a program to find the GCD of two numbers.
3. Write a program to check if a number is prime.

4. Write a program to print the Fibonacci series up to a given number.

5. Write a program to reverse a number.

6. Write a program to calculate the power of a number using a loop.

7. Write a program to find the LCM of two numbers.

8. Write a program to check if a number is an Armstrong number.

9. Write a program to count the number of digits in a number.

10. Write a program to generate a pattern of stars in a pyramid shape.

11. Write a program to implement a simple calculator using switch-case.

12. Write a program to check if a year is a leap year.

13. Write a program to perform matrix addition.

14. Write a program to implement a binary search.

15. Write a program to find the transpose of a matrix.

16. Write a program to find the sum of the series 1 + 1/2 + 1/3 + ... + 1/n.

CARNATION LEVEL
1. Write a program to solve the Tower of Hanoi problem.

2. Write a program to implement the Sieve of Eratosthenes to find prime numbers.

3. Write a program to solve a quadratic equation.

4. Write a program to find the inverse of a matrix.

5. Write a program to evaluate a postfix expression.

MODULE II: Functions, Array, Structure, unions


PYQ - Write suitable functions or procedures to perform the following string operations: [2019 -
6M]
a. Check whether a string is empty.
b. Comparing whether two strings are identical.
c. Reverse of a string.
PROCEDURE:
1. Procedure to Check Whether a String is Empty:

Input: A string.

Operation:

- Check if the first character of the string is the null character (`'\0'`).
Output: Return true if the string is empty; otherwise, return false.

2. Procedure to Compare Whether Two Strings are Identical:


Input: Two strings.

Operation:

- Compare the strings character by character.
- If all character's match, the strings are identical.
- If any character differs, the strings are not identical.
Output: Return true if the strings are identical; otherwise, return false.

3. Procedure to Reverse a String:


Input: A string.

Operation:

- Determine the length of the string.
- Swap the characters from the start and the end, moving towards the center.
Output: The string in reversed order.

PYQ - Write the algorithm in C-like language to sort and array of n numbers using quick sort.
[2022 - 6M]

1. QuickSort(arr, low, high)


- If low < high
- Partition the array such that elements less than pivot are on the left, and elements greater
than pivot are on the right
- `pi = Partition(arr, low, high)`
- Recursively apply the above steps to the subarrays:
- QuickSort(arr, low, pi - 1)
- QuickSort(arr, pi + 1, high)

2. Partition(arr, low, high)


- Choose the rightmost element as the pivot
- Initialize `i = low - 1`
- For each element `j` from `low` to `high - 1`
- If `arr[j] <= pivot`, swap `arr[i]` and `arr[j]`, and increment `i`
- Swap `arr[i + 1]` with the pivot
- Return `i + 1`

PYQ - What is self - referential structure. [2022 - 2M]


Self-Referential structures are those structures that have one or more pointers which point to the
same type of structure, as their member.
PYQ - Give an example of structure inside another structure. (nested structure) [2018 - 2M]

struct Organisation
{
char organisation_name[20];
char org_number[20];

struct Employee
{
int employee_id;
char name[20];
int salary;

};
};

Zone of Ankit
LEVEL 01
1. Write a C program using a function to compute the distance between two points (x,y) and
use it to create another function that will calculate area of triangle ABC, given its three
vertices A(x1,y1), B(x2,y2), C(x3,y3).

2. Write a program to count the number of 1's and 0's in a boolean matrix of order m x n.

3. Write a program in C to read a string and convert all the lower case characters to upper case
character and vice-versa

4. Write a recursive function to display the first 10 Fibonacci numbers.

5. Write a program in C to check whether a string is palindrome or not.

6. Write a function to find the largest among n numbers and use this function to sort and array
of n numbers.

7. Write a program to add and multiply two matrices.

8. Write a program to add and multiply two matrices by allocating memory dynamically.

9. Write a program to print the transpose of a matrix.

10. Write a program to insert an element Num at a specific position pos in the Array A with N
elements.

11. Write a program to merge two sorted arrays to a single sorted array.
12. Write a program in C to count the number of alphabets, digits, blank spaces, and special
characters in a text.

13. Write a function to calculate the factorial of a number and use this function to find
combination of two numbers c(n,r) = n!/r!(r-n)!

14. Write a program to find the sum of elements in an array.

15. Write a program to find the maximum and minimum elements in an array.

16. Write a program to count the number of vowels in a string.

17. Write a program to find the length of a string without using built-in functions.

18. Write a program to concatenate two strings.

19. Write a program to compare two strings.

20. Write a program to sort an array of integers in ascending order.

21. Write a program to find the second largest element in an array.

22. Write a program to search for an element in an array.

23. Write a program to print the elements of a matrix.

24. Write a program to find the sum of diagonal elements of a matrix.

25. Write a program to calculate the average of elements in an array.

26. Write a program to multiply two matrices.

27. Write a program to implement a function to check if a number is even or odd.

28. Write a program to demonstrate the use of structures.

LEVEL 02
1. Find the output of the code

main()
{
int a[4] = {5,3,6,2};
int i = 3, j = 0;
while (i)
{
j+=a[i];
i--;
}
printf("%d", j);
}
1. Write a program to implement a function to find the factorial of a number using recursion.

2. Write a program to implement a function to check if a number is prime.

3. Write a program to implement a function to find the GCD of two numbers.

4. Write a program to implement a function to find the LCM of two numbers.

5. Write a program to implement a function to find the nth Fibonacci number.

6. Write a program to implement a function to reverse a string.

7. Write a program to implement a function to count the number of words in a string.

8. Write a program to implement a function to check if a string is a palindrome.

9. Write a program to implement a function to find the length of a string using recursion.

10. Write a program to implement a function to find the sum of elements in an array.

11. Write a program to implement a function to find the average of elements in an array.

12. Write a program to implement a function to find the maximum and minimum elements in an
array.

13. Write a program to implement a function to search for an element in an array.

14. Write a program to implement a function to sort an array of integers in descending order.

15. Write a program to implement a function to find the transpose of a matrix.

CARNATION LEVEL
1. Write a program to implement matrix multiplication using recursion.

2. Write a program to implement a function to solve the 0/1 knapsack problem.

3. Write a program to implement a function to solve the Traveling Salesman problem using
dynamic programming.

4. Write a program to implement a function to solve the N-Queens problem.

5. Write a program to implement a function to solve the longest common subsequence


problem.

Module III - Pointer and dynamic memory


allocation
PYQ - Comment on the following statement: [2018 - 2M]
int (*a)[7];
a) An array “a” of pointers.
b) A pointer “a” to an array.
c) A ragged array.
d) None of the mentioned
ANS - A pointer “a” to an array.

PYQ - FILE reserved word is [2018 - 2M]


a) A structure tag declared in stdio.h
b) One of the basic datatypes in c
c) Pointer to the structure defined in stdio.h
d) It is a type name defined in stdio.h
ANS - It is a type name defined in stdio.h

PYQ - Strcat function adds null character [2018 - 2M]


a) Only if there is space
b) Always
c) Depends on the standard
d) Depends on the compiler
ANS - Always

PYQ - Differentiate between pointer to function and function returning a pointer with examples.
[2022 - 6M]
PYQ - Differentiate between pointer to array and array of pointers. [2024 - 2M]
PYQ - What is null pointer and far pointer in C. [2022 - 2M]

PYQ - What are the types of pointers? Explain with example. [2020 - 8M]
Pointers in C and C++ can be classified into various types based on their functionality and the data
they point to. Here are some common types of pointers:

1. Null Pointer
- A pointer that doesn't point to any valid memory location. It is typically initialized to NULL or
nullptr in C++.

int *ptr = NULL;

2. Void Pointer
- A generic pointer that can point to any data type. It's often used for generic data handling.
void *ptr;
int x = 5;
ptr = &x; // ptr can now point to an integer

3. Wild Pointer
- An uninitialized pointer that points to some arbitrary memory location. Using wild pointers can
lead to unpredictable behavior and bugs.
int *ptr; // wild pointer

4. Dangling Pointer
- As discussed earlier, a dangling pointer points to a memory location that has been freed or
deallocated.
int *ptr = (int *)malloc(sizeof(int));
free(ptr);
ptr = NULL; // Now ptr is no longer a dangling pointer

5. Function Pointer
- A pointer that points to a function, allowing for dynamic function calls.
void (*funcPtr)(int);
void myFunction(int x) { /*...*/ }
funcPtr = myFunction;
6. Array Pointer
- A pointer that points to an array, allowing for easy array traversal.
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // ptr points to the first element of the array

7. Pointer to Pointer
- A pointer that points to another pointer, useful for multi-dimensional arrays and dynamic
memory management.
int x = 5;
int *ptr = &x;
int **ptr2 = &ptr; // ptr2 points to ptr

8. Constant Pointer
- A pointer whose address or value can't be changed after initialization.
int x = 5;
int *const ptr = &x; // constant pointer to an integer

PYQ - What is dangling pointer in C? How to overcome the problem of a dangling pointer [2022 -
6M]
Dangling Pointer in programming refers to a pointer that doesn’t point to a valid memory
location. This usually happens when an object is deleted or deallocated, without modifying the
value of the pointer, so it still points to the memory location of the deallocated memory. The
below example demonstrates a simple program that creates a dangling pointer in C.
#include <stdio.h>

int main()
{
// Allocating memory
int* ptr = malloc(5 * sizeof(int));
// Deallocating memory
free(ptr);
// ptr is now a dangling pointer
printf("ptr is now a dangling pointer");
return 0;
}
To prevent dangling pointers, always set pointers to NULL or nullptr after you have finished using
them and have deallocated the memory they point to. This ensures that the pointers do not point
to deallocated memory and become dangling pointers.

Zone of Ankit
LEVEL 01
1. Write a program to demonstrate pointer arithmetic.

2. Write a program to swap two numbers using pointers.

3. Write a program to find the sum of elements in an array using pointers.

4. Write a program to find the maximum element in an array using pointers.

5. Write a program to count the number of vowels in a string using pointers.

6. Write a program to reverse a string using pointers.

7. Write a program to concatenate two strings using pointers.

8. Write a program to compare two strings using pointers.

9. Write a program to find the length of a string using pointers.

10. Write a program to copy one string to another using pointers.

11. Write a program to dynamically allocate memory for an array.

12. Write a program to dynamically allocate memory for a matrix.

13. Write a program to demonstrate the use of malloc() and free() functions.

14. Write a program to demonstrate the use of calloc() and free() functions.

15. Write a program to demonstrate the use of realloc() function.

LEVEL 02
1. Write a program to implement a function to find the sum of elements in an array using
pointers.

2. Write a program to implement a function to find the maximum element in an array using
pointers.

3. Write a program to implement a function to count the number of vowels in a string using
pointers.

4. Write a program to implement a function to reverse a string using pointers.

5. Write a program to implement a function to concatenate two strings using pointers.


6. Write a program to implement a function to compare two strings using pointers.

7. Write a program to implement a function to find the length of a string using pointers.

8. Write a program to implement a function to copy one string to another using pointers.

9. Write a program to implement a function to dynamically allocate memory for an array.

10. Write a program to implement a function to dynamically allocate memory for a matrix.

11. Write a program to implement a function to demonstrate the use of malloc() and free()
functions.

12. Write a program to implement a function to demonstrate the use of calloc() and free()
functions.

13. Write a program to implement a function to demonstrate the use of realloc() function.

14. Write a program to implement a function to solve the Tower of Hanoi problem using
pointers.

15. Write a program to implement a function to solve a quadratic equation using pointers.

CARNATION LEVEL
1. Write a program to implement a dynamic array data structure using pointers.

2. Write a program to implement a linked list data structure using pointers.

3. Write a program to implement a binary search tree using pointers.

4. Write a program to implement a function to solve the 0/1 knapsack problem using dynamic
memory allocation.

5. Write a program to implement a function to solve the Traveling Salesman problem using
dynamic programming and pointers.

Module IV - Data structures


PYQ - What would be the asymptotic time complexity of adding an element in the linked list?
[2018] [2 marks]
To add an element at the front of the linked list, we will create a new node which holds the data to
be added to the linked list and pointer which points to head position in the linked list. The entire
thing happens within O (1) time. Thus, the asymptotic time complexity is O (1).
PYQ - Differentiate between linear and nonlinear data structures. [2024 - 2M]

PYQ - Differentiate between arrays and linked list. [2018] [2 marks] [2018]

Array Linked list

An array is a collection of elements A linked list is a collection of objects known as a


of a similar data type. node where node consists of two parts, i.e., data and
address.

Array elements store in a Linked list elements can be stored anywhere in the
contiguous memory location. memory or randomly stored.

Array works with a static memory. The Linked list works with dynamic memory. Here,
Here static memory means that the dynamic memory means that the memory size can
memory size is fixed and cannot be be changed at the run time according to our
changed at the run time. requirements.

Array elements are independent of Linked list elements are dependent on each other. As
each other. each node contains the address of the next node so
to access the next node, we need to access its
previous node.

Array takes more time while Linked list takes less time while performing any
performing any operation like operation like insertion, deletion, etc.
insertion, deletion, etc.

Accessing any element in an array is Accessing an element in a linked list is slower as it


faster as the element in an array can starts traversing from the first element of the linked
be directly accessed through the list.
index.

In the case of an array, memory is In the case of a linked list, memory is allocated at
allocated at compile-time. run time.

Memory utilization is inefficient in Memory utilization is efficient in the case of a linked


the array. For example, if the size of list as the memory can be allocated or deallocated at
the array is 6, and array consists of 3 the run time according to our requirement.
elements only then the rest of the
space will be unused.

PYQ - Which data structure is applied while dealing with recursive functions? [2018] [2 marks]
[2018]
Recursion requires a mechanism to keep track of the multiple function calls, their arguments, and
the state of execution. Stack data structure is used to manage the flow of recursive calls, allowing
a program to ‘remember’ the various function contexts and handle control flow efficiently.

PYQ - Draw a binary search tree whose elements are inserted in the following order 50, 70, 90, 93,
100, 20, 10, 12, 9, 25, 51, 15, 95 [2018 - 10M]

PYQ - Differentiate between primitive and non-primitive data structures. [2019 - 2M]

Primitive data structure Non-primitive data structure

Primitive data structure is a kind of Non-primitive data structure is a type


data structure that stores the data of of data structure that can store the
only one type. data of more than one type.

Examples of primitive data structure Examples of non-primitive data


are integer, character, float. structure are Array, Linked list, stack.

Primitive data structure will contain Non-primitive data structure can


some value, i.e., it cannot be NULL. consist of a NULL value.

The size depends on the type of the In case of non-primitive data


data structure. structure, size is not fixed.

It starts with a lowercase character. It starts with an uppercase character.

Primitive data structure can be used to Non-primitive data structure cannot


call the methods. be used to call the methods.

PYQ - How many stacks are required to implement a queue? [2018] [2 marks] [2019]
To implement a queue using stacks, you generally need two stacks. This approach is known as the
"two-stack queue" or "two-stack implementation of a queue."
Here's the basic idea:
1. Stack 1 (Input Stack): Used to handle the enqueue (insertion) operations.

2. Stack 2 (Output Stack): Used to handle the dequeue (removal) operations.

PYQ - Write an algorithm to inserting and deleting an element from doubly linked list? [2018 -
5M] [2019 - 6M]
write algorithm only not the cod
Sure! Here's a high-level algorithm for inserting and deleting elements in a doubly linked list:
Insertion Algorithm for a Doubly Linked List:
1. Create a New Node
Allocate memory for the new node.

Set the new node's data.

2. Insert at the Beginning
If the list is empty, set the head to the new node.

Otherwise, set the new node's next to the current head.

Set the current head's prev to the new node.

Update the head to the new node.

3. Insert at the End
If the list is empty, set the head to the new node.

Otherwise, traverse to the last node of the list.

Set the last node's next to the new node.

Set the new node's prev to the last node.

Deletion Algorithm for a Doubly Linked List:
1. Find the Node to Delete
Start from the head and traverse the list to find the node containing the target data.

2. Delete the Node
If the node to be deleted is the head, update the head to the next node.

If the node to be deleted has a previous node, set the previous node's next to the

node's next .

If the node to be deleted has a next node, set the next node's prev to the node's prev .

3. Free the Memory
Free the memory allocated to the node to be deleted.

PYQ - Construct a binary tree from the given order [2018 - 5M]
Post Order: DFEBGLJKHCA
In Order: DBFEAGCLJHK
A
/ \
G C
/ / \
B L H
/ \ / \
D E J K
\
F

PYQ - List out few applications of data structures. [2019 - 2M]


Applications of Data Structures
1. Arrays and Linked Lists:
Used in implementing data collections like lists, queues, and stacks.

Arrays are particularly useful in situations where you need to quickly access elements

via an index.

2. Hash Tables:
Widely used in database indexing.

Perfect for implementing dictionaries, caches, and sets due to their fast access and

search times.

3. Trees:
Binary Search Trees (BST) are used in databases and file systems to allow efficient

searching, insertion, and deletion.

Decision Trees in machine learning help in making decisions by learning from data.

4. Graphs:
Used in social networks to model relationships between users.

Essential in routing algorithms, such as finding the shortest path in GPS navigation

systems.

PYQ - Write the conditions to test the circular queue to whether empty or full. [2024 - 2M]
To check if the circular queue is empty, you need to compare the front and rear pointers. If the
front pointer is equal to -1 , it indicates that the queue is empty.

In a circular queue, the queue is full if the next position of rear is the front pointer. This can be
checked using the modulo operator to wrap around the queue.

PYQ - List out the applications of linked list. [2018 - 2M]


Applications of linked list in computer science:
1. Implementation of stacks and queues

2. Implementation of graphs: Adjacency list representation of graphs is the most popular which
uses a linked list to store adjacent vertices.

3. Dynamic memory allocation: We use a linked list of free blocks.

4. Maintaining a directory of names

5. Performing arithmetic operations on long integers

6. Manipulation of polynomials by storing constants in the node of the linked list

7. Representing sparse matrices

PYQ - Write an algorithm to convert an infix expression to postfix expression using a stack. [2024
- 8M]
K + L - M*N + (O$P) W/U/V *T + Q
1. Initialize:

Create an empty stack for operators.



Create an empty list for the output.

2. Iterate through the infix expression:

For each character in the expression:



Operand: Directly add it to the output list.

Left Parenthesis ('('): Push it onto the stack.

Right Parenthesis (')'): Pop from the stack and add to the output list until a left

parenthesis is encountered. Discard the pair of parentheses.

Operator:

While the stack is not empty, and the operator at the top of the stack has greater

than or equal precedence to the current operator (considering associativity), pop
from the stack and add to the output list.

Push the current operator onto the stack.



3. Pop remaining operators from the stack and add to the output list.

PYQ - Is it possible to find a loop inside a linked list? Explain. [2019 - 2M]
Yes, it is possible to find a loop (or cycle) inside a linked list. A common method used to detect a
loop is Floyd’s Cycle-Finding Algorithm, also known as the Tortoise and Hare Algorithm.
Explanation:
1. Initialize Two Pointers: Start with two pointers, slow and fast . Both start at the head of
the linked list.

2. Move the Pointers:


Increment the slow pointer by one node.

Increment the fast pointer by two nodes.

3. Detection:
If there is no loop, the fast pointer will reach the end of the linked list.

If there is a loop, the fast pointer will eventually meet the slow pointer inside the loop.

PYQ - Differentiate between queues and linked list. [2018 - 2M]


Zone of Ankit
LEVEL 01
1. Write a function to delete a node from circular linked list. [2018] [5 marks]

2. Write an algorithm to insert a node into the double linked list. [2018 - 5M][2018 - 5M]

3. Convert the following infix expression to prefix notation -


E: (A+B*C*(M*N^P+T)-G+H) [2018] [5 marks] [2018]
4. Write a C program to create a single linked list and split it at the middle and make the second half
as the first. Display the final list. [2018 - 10M] [2018 -10M]

5. Convert the following expression to postfix expression:


E: ABC*D/+) where A = 2, B = 3, C = 4, D = 6 [2018 - 5M]
6. Construct a tree from a given postfix expression using stack E: AB + CDE +** [2018 - 5M]
7. Evaluate the arithmetic expression P written in postfix notation using stack. P: 3, 16, 2, +, *,
12, 6, /, -,) [2019 - 6M]

8. Write a C program to merge two sorted linked lists and a function to delete a node from a circular
linked list. [2018 - 10M]

9. Write a function to delete a node from a circular linked list. [2018 - 5M]

10. Convert the following infix expression to postfix notation E: (A+(B*C-(D E^F)*G)*H) [2018 - 5M]

11. Write a program to reverse a string using stack.

12. Write a program in C to read the roll number, name and marks in three subjects of ten
students using structure and display the list of students who have scored more than 60% of
marks.

13. Write a program in C to create a linked list with 10 numbers and then display it.

14. Find the prefix of the following infix expression. Show each step. (a-b)*d/e*f$g

15. Write a program to create a linked list and display its elements.

16. Write a program to insert an element at the beginning of a linked list.

17. Write a program to insert an element at the end of a linked list.

18. Write a program to delete an element from a linked list.

19. Write a program to find the length of a linked list.

20. Write a program to search for an element in a linked list.

21. Write a program to reverse a linked list.

22. Write a program to implement a stack using an array.

23. Write a program to implement a stack using a linked list.

24. Write a program to implement a queue using an array.

25. Write a program to implement a queue using a linked list.

26. Write a program to convert an infix expression to a postfix expression using a stack.

27. Write a program to evaluate a postfix expression using a stack.

28. Write a program to implement a circular queue using an array.

29. Write a program to implement a circular queue using a linked list.

LEVEL 02
1. Write a program to implement a doubly linked list.
2. Write a program to insert an element at the beginning of a doubly linked list.

3. Write a program to insert an element at the end of a doubly linked list.

4. Write a program to delete an element from a doubly

5. Write a program to delete an element from a doubly linked list.

6. Write a program to search for an element in a doubly linked list.

7. Write a program to find the length of a doubly linked list.

8. Write a program to reverse a doubly linked list.

9. Write a program to implement a circular doubly linked list.

10. Write a program to insert an element in a circular doubly linked list.

11. Write a program to delete an element from a circular doubly linked list.

12. Write a program to search for an element in a circular doubly linked list.

13. Write a program to implement a priority queue using an array.

14. Write a program to implement a priority queue using a linked list.

15. Write a program to implement a deque (double-ended queue) using an array.

CARNATION LEVEL
1. Write a program to implement a balanced binary search tree (AVL tree).

2. Write a program to implement a red-black tree.

3. Write a program to implement a hash table with chaining.

4. Write a program to implement a trie (prefix tree).

5. Write a program to implement a graph using an adjacency list and perform depth-first
search (DFS) and breadth-first search (BFS).

MODULE V: Tree, Introduction to Sorting &


Searching
PYQ - List out few applications of tree data structures. [2018 - 2M]
Followings are some applications of tree data structures:
1. Hierarchical Structure: One reason to use trees might be because you want to store
information that naturally forms a hierarchy.

2. Searching Efficiency: Trees provide an efficient way to search for data.

3. Sorting: Trees can be used to sort data efficiently. For example, in a self-balancing binary
search tree, the data is automatically sorted as it is inserted into the tree

4. Dynamic Data: Trees are dynamic data structures, which means that they can grow and
shrink as needed.

5. Efficient Insertion and Deletion: Trees provide efficient algorithms for inserting and
deleting data, which is important in many applications where data needs to be added or
removed frequently.

6. Easy to Implement: Trees are relatively easy to implement, especially when compared to
other data structures like graphs.

PYQ - What is tree transversal? Explain the methods of transversal [2022 - 8M]
Tree Traversal refers to the process of visiting or accessing each node of the tree exactly once in
a certain order. There are multiple tree traversal techniques which decide the order in which the
nodes of the tree are to be visited.
Depth First Search or DFS

Inorder Traversal

Preorder Traversal

Postorder Traversal

Level Order Traversal or Breadth First Search or BFS

Inorder Traversal:
Inorder traversal visits the node in the order: Left -> Root -> Right
Algorithm for Inorder Traversal:
Inorder(tree)
Traverse the left subtree, i.e., call Inorder(left->subtree)

Visit the root.

Traverse the right subtree, i.e., call Inorder(right->subtree)

Uses of Inorder Traversal:


In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing

order.

To get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder

traversal is reversed can be used.

Inorder traversal can be used to evaluate arithmetic expressions stored in expression trees.

Time Complexity: O(N)
Auxiliary Space: If we don’t consider the size of the stack for function calls then O(1) otherwise
O(h) where h is the height of the tree.
Preorder Traversal:
Preorder traversal visits the node in the order: Root -> Left -> Right

Algorithm for Preorder Traversal:


Preorder(tree)
Visit the root.

Traverse the left subtree, i.e., call Preorder(left->subtree)

Traverse the right subtree, i.e., call Preorder(right->subtree)

Uses of Preorder Traversal:
Preorder traversal is used to create a copy of the tree.

Preorder traversal is also used to get prefix expressions on an expression tree.

Time Complexity: O(N)
Auxiliary Space: If we don’t consider the size of the stack for function calls then O(1) otherwise
O(h) where h is the height of the tree.
Postorder Traversal:
Postorder traversal visits the node in the order: Left -> Right -> Root

Algorithm for Postorder Traversal:


Algorithm Postorder(tree)
Traverse the left subtree, i.e., call Postorder(left->subtree)

Traverse the right subtree, i.e., call Postorder(right->subtree)

Visit the root

Uses of Postorder Traversal:
Postorder traversal is used to delete the tree. See the question for the deletion of a tree for

details.

Postorder traversal is also useful to get the postfix expression of an expression tree.

Postorder traversal can help in garbage collection algorithms, particularly in systems where

manual memory management is used.

Level Order Traversal:


Level Order Traversal visits all nodes present in the same level completely before visiting the
next level.

Algorithm for Level Order Traversal:


LevelOrder(tree)

Create an empty queue Q



Enqueue the root node of the tree to Q

Loop while Q is not empty

Dequeue a node from Q and visit it

Enqueue the left child of the dequeued node if it exists

Enqueue the right child of the dequeued node if it exists

Uses of Level Order:
Level Order Traversal is mainly used as Breadth First Search to search or process nodes level-

by-level.

Level Order traversal is also used for Tree Serialization and Deserialization .

PYQ - Draw a binary search tree whose elements are inserted in the following order: 50, 70, 90,
93, 100, 20, 10, 12, 9, 25, 51, 15, 95 [2019 - 16M]
50
/ \
20 70
/ \ / \
10 25 51 90
/ \ / \
9 12 93 100
\ /
15 95

PYQ - Construct a binary search tree (BST) using list of letters J, R, D, G, T, E, M, H, P, A, F, Q. Find
the pre - order, in order, post-order transversal of the BST created. [2018 - 2M]
J
/ \
D R
/ \ / \
A G M T
/ \ \
E H P
\
F
\
Q

Pre order - J, D, A, G, E, F, H, R, M, P, Q, T
In order - A, D, E, F, G, H, J, M, P, Q, R, T
Post order - A, F, E, H, G, D, Q, P, M, T, R, J

PYQ - Give a basic algorithm for searching operation using linear searching technique. Find out
its time complexity. [2019 - 16M]

Basic Algorithm for Linear Search:


1. Start at the first element of the list.

2. Compare the current element with the target element.

3. If the current element matches the target element, return its position (index).
4. Otherwise, move to the next element.

5. Repeat steps 2-4 until the element is found or the list ends.

6. If the list ends without finding the target, return that the element is not in the list (often
indicated by returning -1).

Time Complexity:
Best Case: O(1) - When the target element is the first element in the list.

Average Case: O(n/2) - On average, it will take half the list length to find the target.

Worst Case: O(n) - When the target element is the last element or not in the list.

PYQ - For a sorted array of n numbers, modify the linear search algorithm so that the average
case time complexity of successful search will be (n+1)/2. [2022 - 8M]

Modified Linear Search Algorithm:


1. Initialize Two Pointers:
left pointing to the first element.

right pointing to the last element.

2. Iterate until the two pointers meet:
Compare the element at left pointer with the target.

Compare the element at right pointer with the target.

If the target is found at either pointer, return its position.

Increment the left pointer and decrement the right pointer.

3. If the target is not found, return that the element is not in the list (often indicated by
returning -1).

Time Complexity:
Best Case: O(1) - When the target element is found at either the first or the last position.

Average Case: O((n+1)/2) - Since we're checking from both ends, on average, we only need

to traverse half of the list.

Worst Case: O(n) - When the target element is in the middle of the list or not present.

PYQ - If an array is fully sorted what will be the time complexity of bubble sort? Explain your
answer with justification. [2022 - 8M]
When an array is fully sorted, the time complexity of the bubble sort algorithm changes from its
typical worst-case scenario.
Time Complexity in the Fully Sorted Case
Best Case Time Complexity: O(n)
Justification:
Bubble sort works by repeatedly swapping adjacent elements if they are in the wrong order. In a
fully sorted array, there will be no need for any swaps. Here’s how it works:
1. Single Pass: The algorithm makes a single pass through the array.

2. No Swaps: As the array is already sorted, no swaps are required.

3. Early Termination: Once the algorithm detects that no swaps were made in a pass, it can
terminate early.

PYQ - Construct a binary tree for the given: [2024 - 6M]


in order sequence - D B E A F C
Preorder sequence - A B D E C F

PYQ - Compute the maximum number of Comparisions required to search an element in an


ordered list of 2048 elements using binary search. Compare it with linear search. [2024 - 2M]

PYQ - Write the algorithm to illustrate the insertion and deletion operation in a binary search
tree. [2024 - 8M]
Here are the algorithms for inserting and deleting nodes in a Binary Search Tree (BST).

Insertion Algorithm:
1. Start at the root.

2. Compare the key to be inserted with the value of the current node.
If the key is less than the current node's value, move to the left child.

If the key is greater than the current node's value, move to the right child.

3. Repeat the process until an appropriate null position is found.

4. Insert the new node at this position.


Deletion Algorithm:
1. Start at the root.

2. Find the node to be deleted.


If the key is less than the current node's value, move to the left child.

If the key is greater than the current node's value, move to the right child.

If the key matches the current node's value, proceed with the deletion process.

3. Cases for Deletion:
Node with no children (leaf node): Simply remove the node.

Node with one child: Replace the node with its child.

Node with two children:

Find the minimum value node in the right subtree (in-order successor).

Replace the node's value with the in-order successor's value.

Recursively delete the in-order successor.

Zone of Ankit
LEVEL 01
1. Write a program to arrange the list of numbers in ascending order using quick sort. [2018 - 5M]

2. Write a program to arrange the list of number in ascending order using bubble sort. [2019 - 6M].

3. Represent a stack and queue in a single 1D array. Write functions for push, pop, operations
on the stack and add, delete functions on the queue. [2019 - 6M]

4. Sort the following array of elements using quicks sort - 43, 56, 12, 98, 27, 67, 29, 15, 36

5. Write a program in C to search an element from the array of n numbers using binary search.

6. Write a program to create a binary tree and perform inorder traversal.

7. Write a program to create a binary tree and perform preorder traversal.

8. Write a program to create a binary tree and perform postorder traversal.

9. Write a program to find the height of a binary tree.

10. Write a program to count the number of nodes in a binary tree.

11. Write a program to search for a value in a binary search tree.


12. Write a program to insert a value into a binary search tree.

13. Write a program to delete a value from a binary search tree.

14. Write a program to find the minimum and maximum values in a binary search tree.

15. Write a program to find the level of a node in a binary tree.

16. Write a program to perform bubble sort on an array.

17. Write a program to perform selection sort on an array.

18. Write a program to perform insertion sort on an array.

19. Write a program to perform merge sort on an array.

20. Write a program to perform quick sort on an array.

LEVEL 02
Write a program to perform heap sort on an array.

Write a program to perform radix sort on an array.

Write a program to perform shell sort on an array.

Write a program to perform counting sort on an array.

Write a program to perform binary search on a sorted array.

Write a program to perform ternary search on a sorted array.

Write a program to find the kth smallest element in an array using quick select.

Write a program to find the median of two sorted arrays.

Write a program to find the common elements in two binary search trees.

Write a program to find the inorder successor of a node in a binary search tree.

Write a program to convert a binary tree to a doubly linked list.

Write a program to check if a binary tree is balanced.

Write a program to find the diameter of a binary tree.

Write a program to find the lowest common ancestor of two nodes in a binary search tree.

Write a program to print all root-to-leaf paths in a binary tree.

CARNATION LEVEL
Write a program to implement Dijkstra's algorithm for finding the shortest path in a graph.

Write a program to implement Floyd-Warshall algorithm for finding the shortest paths

between all pairs of nodes in a graph.

Write a program to implement Kruskal's algorithm for finding the minimum spanning tree of

a graph.

Write a program to implement Prim's algorithm for finding the minimum spanning tree of a

graph.

Write a program to implement A* search algorithm for finding the shortest path in a graph.

author of the document - Ankit

You might also like