DSA Assignment Answers
1. A data structure is a way of organizing and storing data to perform operations efficiently.
Examples of linear data structures: Array, Linked List. Examples of non-linear data structures: Tree,
Graph.
2. An array stores elements in contiguous memory and has a fixed size, whereas a linked list
consists of nodes that contain data and a pointer to the next node, allowing dynamic memory
allocation.
3. Time complexity indicates how the runtime of an algorithm increases with input size. In linear
search, the worst-case time complexity is O(n), where n is the number of elements in the array.
4. In stacks, overflow occurs when we try to push an element into a full stack. Underflow occurs
when we try to pop an element from an empty stack, both leading to errors.
5. Space complexity is the amount of memory used by an algorithm during its execution. For
example, an algorithm that sorts an array without extra space has O(1) space complexity.
6. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Real-life
applications include undo operations in editors and backtracking in mazes or puzzles.
7. The height of a tree is the number of edges on the longest path from the root to a leaf. A BST with
7 nodes in a balanced form has a height of 2.
8. A queue is a linear structure that follows FIFO order, while a circular queue connects the last
position back to the first, utilizing memory more efficiently and avoiding unused space.
9. The postfix expression of (A + B) * (C - D / E) is: AB+CDE/-*. This form eliminates the need for
parentheses and follows operand-operator order for evaluation.
10. Hashing is a technique for mapping data to a fixed-size table using a hash function. Collision
occurs when two keys map to the same index in the hash table.
11. A circular queue connects the end of the queue back to the front, allowing efficient use of
memory by reusing freed spaces, which is not possible in a simple linear queue.
12. 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, while a complete one is filled level-wise.
13. Dynamic memory allocation in linked lists allows memory to be allocated at runtime using
pointers. Each node is created as needed using functions like malloc() in C.
14. The recursive formula for Fibonacci is: fib(n) = fib(n-1) + fib(n-2), with base cases fib(0)=0,
fib(1)=1. So, fib(5) = 5.
15. Binary search is an efficient algorithm for finding an element in a sorted array. It requires that the
array must be sorted before applying the search.
16. To insert a node at the beginning of a singly linked list: create a new node, point its next to the
current head, and update the head to the new node.
17. Divide and conquer splits the problem into subproblems (e.g., merge sort), while greedy
algorithms make local optimal choices (e.g., Kruskal's algorithm). Both solve problems but use
different strategies.
18. Recursion is a technique where a function calls itself to solve smaller instances of the problem.
Example for factorial: factorial(n) = n * factorial(n-1), with factorial(0) = 1.
19. Push pseudocode: if top == size-1 then overflow else top++, stack[top] = item. Pop pseudocode:
if top == -1 then underflow else item = stack[top], top--.
20. A priority queue assigns priority to elements. The highest priority is dequeued first, unlike a
normal queue that follows FIFO regardless of priority.