DSA Short Notes
DSA Short Notes
I'll
provide brief answers or explanations for each question:
1. Define stack. Write an algorithm to push and pop elements from the
stack.
• A stack is a linear data structure that follows the Last-In-First-Out (LIFO)
principle. Elements are added and removed from one end, known as
the top.
• Algorithm for push (add an element to the stack):
1. Check if the stack is full (if it's implemented as a fixed-size array).
2. Increment the top pointer.
3. Place the new element at the top position.
• Algorithm for pop (remove an element from the stack):
1. Check if the stack is empty.
2. Retrieve the element at the top position.
3. Decrement the top pointer.
2. Define stack. Write an algorithm to insert and delete elements from the
stack.
• Inserting elements in a stack typically follows the same push operation.
• Deleting elements is not a standard operation in stacks. You generally
only pop elements from the top.
3. Questions on infix to prefix and postfix expressions.
Converting infix expressions to prefix and postfix requires algorithms like the
shunting yard or using a stack-based approach. Each of your expressions can
be converted using these methods.
4. What is SLL (Singly Linked List)? Write an algorithm to insert elements at
i) start ii) middle iii) end.
A singly linked list is a linear data structure where each element is a node
pointing to the next element.
• To insert at the start, create a new node and make it the new head.
• To insert in the middle, find the node before the desired position and
update pointers.
• To insert at the end, traverse the list to the last node and append the
new node.
5. Write an algorithm to delete the first, middle, and end elements from a
singly linked list.
• Deleting the first element involves updating the head pointer.
• Deleting in the middle requires finding the node before the target and
updating pointers.
• Deleting the end element is similar to deleting in the middle but needs
special handling for the tail node.
6. What is DLL (Doubly Linked List)? Write an algorithm to insert elements
at i) start ii) middle iii) end.
A doubly linked list is similar to a singly linked list, but each node has pointers
to both the next and previous nodes.
• To insert at the start, create a new node, update pointers, and make it
the new head.
• To insert in the middle, find the node before the desired position and
update pointers.
• To insert at the end, traverse to the last node, update pointers, and
make the new node the tail.
7. Write an algorithm to delete the first, middle, and end elements from a
doubly linked list.
• Deleting the first element involves updating the head pointer and the
new head's previous pointer.
• Deleting in the middle requires finding the node before the target and
updating pointers.
• Deleting the end element involves updating the tail pointer and the
new tail's next pointer.
8. Explain tree representation in detail with a suitable example.
A tree is a hierarchical data structure composed of nodes. A node has a
parent-child relationship. A suitable example is a binary tree where each node
has at most two children.
9. Explain Graph representation in detail with a suitable example.
Graphs consist of nodes (vertices) and edges. Suitable examples include an
adjacency matrix, adjacency list, or an actual application like a social network
graph.
10. Explain Binary Search Tree (BST). Write an algorithm to traverse it in
preorder, inorder, and postorder.
• A BST is a binary tree where each node's left child is smaller, and the
right child is greater than the node.
• Preorder: Root, Left, Right
• Inorder: Left, Root, Right
• Postorder: Left, Right, Root
11. Create a BST and traverse it in preorder, inorder, and postorder.
• You can create a BST by inserting elements in a specific order. Then,
use the algorithms mentioned in question 10 to traverse it.
12. Explain DFS and BFS with examples.
• DFS (Depth-First Search) explores as far as possible along a branch
before backtracking. It's often implemented using recursion.
• BFS (Breadth-First Search) explores all neighbors before moving to the
next level. It's usually implemented using a queue.
13. What is searching? Explain Linear and Binary Search.
• Searching is the process of finding a specific element in a collection of
data.
• Linear Search checks each element sequentially.
• Binary Search works on sorted data and divides the search space in half
with each comparison.
14. Write an algorithm for binary search.
Here's a simple algorithm for binary search on a sorted array:
1. Set the left pointer to 0 and the right pointer to the length of the array
minus one.
2. While the left pointer is less than or equal to the right pointer: a.
Calculate the middle index as (left + right) / 2. b. If the middle element
is equal to the target, return its index. c. If the middle element is less
than the target, set the left pointer to (middle + 1). d. If the middle
element is greater than the target, set the right pointer to (middle - 1).
3. If the target is not found, return -1 (not found).
15. Write an algorithm for bubble sort.
Bubble sort repeatedly steps through the list, compares adjacent elements,
and swaps them if they are in the wrong order. The pass through the list is
repeated until no swaps are needed.
16. Write an algorithm for insertion sort.
Insertion sort builds the final sorted array one item at a time. It takes each
element and inserts it into its correct position within the sorted array.
17. Write an algorithm for selection sort.
Selection sort divides the input list into a sorted and an unsorted region. It
repeatedly selects the smallest (or largest, depending on sorting order)
element from the unsorted region and moves it to the end of the sorted
region.
18. Sort numbers using radix sort.
Radix sort is a non-comparative integer sorting algorithm. It processes digits
individually, from the least significant to the most significant. Sorting using
radix sort would require separate steps for each digit.
19. Sort numbers using Bubble, Insertion, and Selection sort.
You can sort the given list using the respective sorting algorithms mentioned
in questions 15, 16, and 17.
20. Apply Prim's and Kruskal's algorithms to find the Minimum Spanning
Tree (MST).
Both Prim's and Kruskal's algorithms find the MST in a graph by selecting
edges with the lowest weight while ensuring connectivity without forming
cycles. You'll need a graph example to demonstrate this.
21. Write an algorithm for quick sort.
Quick sort is a divide-and-conquer algorithm. The main steps are selecting a
"pivot," partitioning the array into elements less than the pivot, the pivot itself,
and elements greater than the pivot, and then recursively sorting the
subarrays.
22. Write an algorithm for merge sort.
Merge sort is a divide-and-conquer algorithm. It involves recursively splitting
the array in half, sorting the halves, and then merging them back together.
23. Find a solution generated by the job sequencing problem with deadlines
and profits.
The job sequencing problem involves scheduling jobs with deadlines and
profits. You would typically use a greedy algorithm to find a solution.
24. Explain optimal merge pattern.
The optimal merge pattern is a problem in computer science and data
structures where you have multiple sorted sequences and need to merge
them into a single sorted sequence optimally. The algorithm for this problem
minimizes the number of comparisons.
25. Obtain Huffman encoding for the given data.
Huffman encoding is a variable-length prefix coding algorithm that minimizes
the total encoding length. It would require constructing a Huffman tree for the
given data and assigning codes based on the tree structure.
26. Explain optimal storage on tapes.
Optimal storage on tapes involves arranging data efficiently on a magnetic
tape to minimize the number of tape reads and writes.
27. Explain hashing and linear probing in detail.
• Hashing is a technique that converts data into a fixed-size value (hash
code) to index data structures. Hash tables use this technique for fast
data retrieval.
• Linear probing is a method to handle hash collisions where, if a
collision occurs, you search for the next available slot (linearly) until you
find an empty slot to store the data.