DSA Assignment
DSA Assignment
1. Define data structure. Give two examples of linear and non-linear data
structures.
An array stores elements in contiguous memory locations and has a fixed size. It
provides fast access using indices. A linked list, however, is made of nodes with
pointers and allows dynamic memory allocation, making insertion and deletion more
flexible.
Time complexity represents the amount of time an algorithm takes to complete relative
to the input size. For linear search, the worst-case time complexity is O(n), where n is
the number of elements in the list.
Overflow occurs when an attempt is made to push an element into a full stack.
Underflow happens when trying to pop an element from an empty stack. Both are error
conditions that must be handled.
Space complexity refers to the total memory space an algorithm needs to run, including
input storage and auxiliary space. For example, a function using a single loop with a few
variables has O(1) space complexity.
A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.
Real-life applications include the undo feature in text editors and navigation history in
web browsers.
7. What is the height of a tree? Calculate the height of a BST with 7 nodes.
The height of a tree is the number of edges on the longest path from the root to a leaf.
For a balanced Binary Search Tree (BST) with 7 nodes, the height is 2.
Hashing is a technique used to map data to a fixed-size table using a hash function. A
collision occurs when two different keys are assigned the same hash value or index in
the table.
11. Explain how a circular queue avoids wastage of memory compared to a linear
queue.
In a linear queue, space at the front is not reused after deletions. A circular queue
connects the rear to the front, allowing this space to be reused and thus prevents
memory wastage.
12. Define a binary tree. Differentiate between a full and complete binary tree.
A binary tree is a hierarchical structure where each node has at most two children. A
full binary tree has all nodes with 0 or 2 children. A complete binary tree is filled level-
wise except possibly the last level, which is filled from left to right.
In linked lists, nodes are created dynamically at runtime using functions like malloc() in
C. This allows efficient memory usage, as memory is allocated only when needed and
can grow or shrink as required.
14. Write the recursive formula for Fibonacci series and compute fib(5).
Recursive formula: fib(n) = fib(n-1) + fib(n-2), with base cases: fib(0) = 0 and fib(1) = 1.
Thus, fib(5) = 5.
15. What is binary search? List one condition for its application.
Binary search is an efficient algorithm that divides the search interval in half each time.
It requires the input data to be sorted before application. Its time complexity is O(log n).
16. Write the steps to insert a node at the beginning of a singly linked list.
17. Compare divide and conquer and greedy algorithms with examples.
Divide and conquer breaks problems into subproblems (e.g., Merge Sort). Greedy
algorithms make locally optimal choices (e.g., Kruskal's Algorithm for Minimum
Spanning Tree). Both are useful in different problem-solving scenarios.
Example:
int factorial(int n) {
if(n == 0) return 1;
else return n * factorial(n - 1);
}
19. Implement a stack using an array (write pseudocode for ** and ** operations).
Push:
Pop:
If top == -1 → underflow