Master C programming - Coding questions
Master C programming - Coding questions
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.
for while
Loop Loop
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
5. Write a program to take input from the user and display it.
12. Write a program to find the sum of natural numbers up to a given limit.
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.
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.
•
PYQ - Write the algorithm in C-like language to sort and array of n numbers using quick sort.
[2022 - 6M]
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
6. Write a function to find the largest among n numbers and use this function to sort and array
of n numbers.
8. Write a program to add and multiply two matrices by allocating memory dynamically.
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)!
15. Write a program to find the maximum and minimum elements in an array.
17. Write a program to find the length of a string without using built-in functions.
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.
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.
14. Write a program to implement a function to sort an array of integers in descending order.
CARNATION LEVEL
1. Write a program to implement matrix multiplication using recursion.
3. Write a program to implement a function to solve the Traveling Salesman problem using
dynamic programming.
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++.
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.
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.
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.
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.
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.
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.
PYQ - Differentiate between arrays and linked list. [2018] [2 marks] [2018]
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.
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.
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]
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.
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
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.
2. Implementation of graphs: Adjacency list representation of graphs is the most popular which
uses a linked list to store adjacent vertices.
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:
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.
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. Write an algorithm to insert a node into the double linked list. [2018 - 5M][2018 - 5M]
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]
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.
26. Write a program to convert an infix expression to a postfix expression using a stack.
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.
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.
CARNATION LEVEL
1. Write a program to implement a balanced binary search tree (AVL tree).
5. Write a program to implement a graph using an adjacency list and perform depth-first
search (DFS) and breadth-first search (BFS).
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)
•
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
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 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]
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]
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.
3. Early Termination: Once the algorithm detects that no swaps were made in a pass, it can
terminate early.
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.
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.
14. Write a program to find the minimum and maximum values in a binary search tree.
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.
•