मौलाना आजाद राष्ट्रीय प्रौद्योगिकी संस्थान भोपाल- 462003
(शिक्षा मंत्रालय, भारत सरकार के तहत राष्ट्रीय महत्व का एक संस्थान)
MAULANA AZAD NATIONAL INSTITUTE OF TECHNOLOGY BHOPAL- 462003
(An Institute of National importance under Ministry of Education, Govt. of India)
B.Tech (CSE/ECE),
Subject: Data Structures and Algorithms
Subject Co-Ordinator(s):
o Dr. Meenu Chawla
o Dr. Vijay Bhaskar Semwal
o Dr. Shweta Bhandari
Assignment -1 Time Complexity
Q1. Calculate the time complexity of the following program?
1.
int Function(int arr[], int n, int key) {
int left = 0, right = n - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(arr[mid] == key) return mid;
else if(arr[mid] < key) left = mid + 1;
else right = mid - 1;
}
return -1;
}
2.
void Function (int n) {
while (n > 1) {
n /= 2;
printf( 'n \n');
}
}
3.
void Function (int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf( " %d,%d \n",arr[i],arr[j]);
}
}
}
4.
void Function (int n) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
Printf( "%d ",i+j+k);
}
5.
void Function (int n) {
while (n > 1) {
n = sqrt(n); // or log(n)
printf( '%d\n',n);
}
}
6.
void Function(int n) {
int limit = pow(2, n);
for (int i = 0; i < limit; i++) {
printf( "%d ",i);
}
}
Q2. For each of the following pairs of functions, either f(n) is O(g(n)), f(n) is
Ω(g(n)), or f(n) = Θ(g(n)).
Determine which relationship is correct.
a. f(n) = log n2; g(n) = log n + 5
b. f(n) = n; g(n) = log n2
c. f(n) = log log n; g(n) = log n
d. f(n) = n; g(n) = log2 n
e. f(n) = n log n + n; g(n) = log n
f. f(n) = 10; g(n) = log 10
g. f(n) = 2n; g(n) = 10n2
h. f(n) = 2n; g(n) = 3n
Q3. Which of the given options provides the increasing order of asymptotic
complexity of functions f1, f2, f3 and f4?
f1 (n) = 2 n , f 2 (n) = n 3 / 2 , f 3 (n) = n log 2 n, f 4 = n log 2 n
Assignment -2 Array
Q1. Design a character array of size 10 using the following functions. Print the
hexadecimal memory address of each allocated block. Demonstrate the changes
in memory addresses before and after reallocation. Functions to be used:
• malloc(size)
• calloc(n, size)
• realloc(pointer, new_size)
• free(pointer)
Print memory addresses after each allocation. Reallocate the array and observe
whether the address is retained or changed. Explain the behavior of each
function. Commented explanation of address retention or change after
realloc().
Q2. Demonstrate a memory leak using improper use of realloc() in C. Show
how reassigning a realloc() result without using a temporary pointer can lead
to lost memory.
Q3. Write a program to find duplicate elements in an integer array using in-
place modification techniques (e.g., using xor operation, sign marking or
indexing tricks).
• Constraints: Linear time complexity: O(n)
• Constant space: O(1) (No extra arrays or hash maps)
• Linear memory use (in-place)
Q4.
A. Represent a sparse matrix using a polynomial-style format stored in a
1D array. Write a program to store and display a sparse matrix efficiently.
B. Convert the above to a linked list-based sparse matrix representation.
C. Demonstrate and explain memory storage for:
• Upper triangular matrix
• Lower triangular matrix
• Band matrix
Provide index formulas for storing elements using minimal memory
Q5. For each of the following operations involving pointer arithmetic and
expressions, state whether the operation is valid or invalid, and explain why?
Expression Valid/Invalid
A. p = -q;
B. p <<= 1;
C. p = p - b;
D. p = p - q;
E. p = (int *)p - q; .
F. p = p - q - a;
G. p = p + a;
H. p = p + q;
I. p = p * q;
J. p = p / q;
K. p = p / a;
Assignment -3 Linked List
Q1: You are given the task to implement a circular singly linked list and
perform the following operations using two pointers (head and tail):
1. Create a circular linked list of n elements (input by the user).
2. Display the list starting from head and ensure the traversal stops when
the node points back to head.
3. Insert a new node at the end using the tail pointer efficiently (in O(1)
time).
4. Delete a node from the beginning using the head pointer.
Q2:- You are given a singly linked list. Your task is to:
1. Detect if a loop (cycle) exists in the list using two pointers (Rabbit and
Tortoise – Floyd’s algorithm).
2. If no loop is found, then use the same two-pointer approach to find the
middle element of the list in one traversal.
Assignment -4 Stack & Queue
Q1: -You are given an arithmetic expression in infix notation (e.g., "3 + 5 * (2
- 4)"). Your task is to:
1. Convert the infix expression to postfix (Reverse Polish Notation) using a
stack.
2. Evaluate the postfix expression using a stack-based approach.
Q2:- Write a recursive function to compute the factorial of a given number n.
Also, implement the same using tail recursion to optimize memory usage
Q3:- Implement a circular queue using an array of size N = 5. Perform the
following operations and display the queue after each step:
1. Enqueue: 10, 20, 30
2. Dequeue
3. Enqueue: 40, 50
4. Enqueue: 60 (should wrap around to the front if space is available)
5. Dequeue twice
6. Enqueue: 70
Expected Behavior:
• Proper wrapping around when rear reaches end
• Full/Empty checks
• Rear and front pointer updates
Q4:- Problem Statement
A single pair of rabbits (one male, one female) is placed in a field. Rabbits reach
reproductive age after one month, and each pair produces one new pair of
rabbits every month starting from the second month. Assuming no rabbits die:
How many pairs of rabbits will there be after n months?
This problem follows the Fibonacci sequence:
• Month 1: 1 pair
• Month 2: 1 pair
• Month 3: 2 pairs
• Month 4: 3 pairs
• Month 5: 5 pairs
• Month 6: 8 pairs
• ...
Assignment -5 Hashing, Searching, Sorting
1. A hash table of size 10 uses h(k) = k mod 10 and linear probing. After inserting 6 keys
into an empty table, the slots are:
Index Value
2 42
3 23
4 34
5 52
6 46
7 33
The keys inserted are 42, 23, 34, 52, 46, 33. How many different insertion orders
(sequences) of these keys could lead to exactly this final arrangement?
2. A hash table of size m = 13 stores integer keys using double hashing. The primary and
secondary hash functions are defined as:
h₁(k) = k mod 13
h₂(k) = 7 - (k mod 7)
To resolve collisions, the probe sequence is defined as:
h(k, i) = (h₁(k) + i × h₂(k)) mod 13, for i = 0, 1, 2, ...
You are asked to insert the following keys in order: 18, 41, 22, 44, 59, 32
a. Show the final state of the hash table after inserting all keys using double hashing.
b. Why must h₂(k) be relatively prime to the table size m in double hashing? What could
go wrong if it is not?
c. Describe the worst-case time complexity of search and insertion using double
hashing. What is the expected complexity when the load factor α < 0.7?
d. Compare double hashing with linear probing and quadratic probing in terms of
clustering behavior and performance.
3. Given an array of integer, the task is to find the first repeating element — that is, the
element which appears more than once and whose first occurrence is at the lowest index
in the array. Design an algorithm that solves this problem in linear time, i.e., O(n).
Hint: Use a hash-based data structure to achieve linear time complexity.
4. Assume the following two arrays:
A[1...n] — an array of distinct integers.
P[1...n] — a permutation of numbers from 1 to n.
You must reorder array A such that after sorting, the position of each element in A is
consistent with the order specified by array P. That is, after sorting A, the element that
was supposed to be the k-th smallest (based on P[k] == 1) should go to index k.
5. Given an array A[1...n] containing both positive and negative integers, find the length of
the longest subarray whose elements sum to zero. The proposed algorithm should be with
Time Complexity: O(n),Space Complexity: O(n). Elements can be negative, zero, or
positive.
6. Assume an optimized bubble sort that includes two enhancements:
a. After each pass through the array, reduce the range of comparisons (because the
largest elements “sink” into place).
b. Terminate early if a pass makes no swaps, indicating the array is already sorted.
1. Explain why this enhanced bubble sort still has a worst-case time complexity of
O(n²).
2. Describe its best-case time complexity and under what input conditions that occurs.
3. For an array of size n = 10, in the worst case, how many comparisons and swaps
occur? How does that compare to a nearly sorted input?
Optional Brainstorming Questions:
1. Why Merge Sort always runs in Θ(n log n) time in all cases, even if the input is
already sorted or reverse sorted.
2. Compare the space complexity of Merge Sort versus Bubble Sort. Why is Merge
Sort considered not in-place?
3. Is Merge Sort stable? Why does stability matter when sorting records with
multiple fields (e.g., sorting by city then by date)?