0% found this document useful (0 votes)
6 views4 pages

DSA Assignment

The document outlines various concepts related to data structures, including definitions, examples, and differences between types such as arrays, linked lists, stacks, and queues. It explains key terms like time complexity, space complexity, and hashing, along with practical applications and algorithms like binary search and recursion. Additionally, it provides pseudocode for stack operations and discusses the characteristics of priority queues.
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)
6 views4 pages

DSA Assignment

The document outlines various concepts related to data structures, including definitions, examples, and differences between types such as arrays, linked lists, stacks, and queues. It explains key terms like time complexity, space complexity, and hashing, along with practical applications and algorithms like binary search and recursion. Additionally, it provides pseudocode for stack operations and discusses the characteristics of priority queues.
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/ 4

DSA Assignment

1. Define data structure. Give two examples of linear and non-linear data
structures.

A data structure is a way of organizing and storing data to perform operations


efficiently. It enables effective data manipulation and retrieval. Examples of linear data
structures include arrays and linked lists. Examples of non-linear data structures include
trees and graphs.

2. Differentiate between an array and a linked list.

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.

3. What is time complexity? Express the worst-case time complexity of linear


search.

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.

4. What is overflow and underflow in the context of stacks?

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.

5. Explain the term space complexity with an example.

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.

6. What is a stack? Mention two real-life applications.

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.

8. How does a queue differ from a circular queue?

A queue is a linear structure that follows First-In-First-Out (FIFO) order. A circular


queue connects the rear to the front, allowing unused memory at the beginning to be
reused, preventing overflow in full but unutilized queues.

9. Write the postfix form of the expression: (A + B) * (C - D / E).

The postfix form of the given expression is: AB+CDE/-*.

10. What is hashing? Define collision in hashing.

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.

13. Explain dynamic memory allocation in linked lists.

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.

1. Create a new node.


2. Set the new node's next pointer to the current head.
3. Update the head pointer to point to the new node.

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.

18. What is recursion? Write a recursive function to compute factorial.

Recursion is when a function calls itself to solve smaller subproblems.

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:

If top == size - 1 → overflow

Else → top++, stack[top] = item

Pop:

If top == -1 → underflow

Else → item = stack[top], top--

20. What is a priority queue? How is it different from a normal queue?


A priority queue serves elements based on priority rather than order of arrival. In
contrast, a normal queue follows FIFO order. Higher priority elements are dequeued
before lower ones.

You might also like