Data Structure & Algorithms 2024 Q&A
Data Structure & Algorithms 2024 Q&A |
Q1. Perform one round of partitioning using Quick sort with n/3 th element as pivot for given integer set {21,28,34,7,67,31,42,50,56} [5 Marks]
Answer:
One round of partitioning using Quick Sort with the n/3th element as the pivot for the given integer set {21, 28, 34, 7, 67, 31, 42, 50, 56}.
Here's Quick Sort partitioning works:
- Select Pivot: We choose the element at the n/3th position (rounded down) as the pivot. In this case, the array has 9 elements (n=9), so n/3 = 3. Since we round down for the index, the pivot will be at the second position (index 1) which is 28.
- Swap Pivot: We swap the pivot element with the last element in the sub-array (here the entire array is the sub-array). This places the pivot at its final position (ideally).
- Partitioning Loop: We iterate through the remaining elements (except the pivot). If an element is less than or equal to the pivot, we swap it with the element one position to the left of the current index we're iterating through. This effectively moves the smaller elements to the left side of the pivot.
- Swap Pivot Back: After the loop completes, we swap the pivot element with the element at the next index after the last swap (i+1). This places the pivot in its final sorted position.
- Original Array: {21, 28, 34, 7, 67, 31, 42, 50, 56}
- Selected Pivot (2nd element): 28 (swapped with the last element, 56) becomes {21, 56, 34, 7, 67, 31, 42, 50, 28}
- Partitioning Loop:
- Compare 21 with 56 (pivot), swap: {56, 21, 34, 7, 67, 31, 42, 50, 28}
- Compare 34 with 56 (pivot), no swap.
- Compare 7 with 56 (pivot), swap: {56, 21, 34, 7, 67, 31, 42, 50, 28}
- Continue comparing and swapping elements less than the pivot to the left side.
- Final Partitioned Array: {21, 7, 31, 28, 56, 34, 42, 50, 67} (pivot at its final position)
Q2. Sort the following array in ascending array using heap sort Show all the steps.
20,10,5, 7,9,25,30,15,12,35
- Build Max Heap: Build a max heap from the given array.
- Heapify the Array: Convert the max heap into a sorted array by repeatedly removing the largest element from the heap and placing it at the end of the array, then heapifying the remaining heap.
- Repeat Step 2: Repeat the process until the entire array is sorted.
- Starting Array: [20, 10, 5, 7, 9, 25, 30, 15, 12, 35]
- Start from the last non-leaf node: (n/2 - 1), where n is the number of elements.
- In this case, the last non-leaf node is at index 4 (for 10).
- Heapify the subtree rooted at index 4.
- 10 is smaller than its children (20, 5), so swap 10 with 20.
- Heap after swapping: [20, 5, 10, 7, 9, 25, 30, 15, 12, 35]
- Move to the next non-leaf node: index 3 (for 7).
- Heapify the subtree rooted at index 3.
- 7 is smaller than its children (15, 12), so no swap needed.
- Continue this process until the entire array is a max heap.
- Max Heap: [20, 15, 10, 12, 9, 25, 30, 5, 7, 35]
- Swap the root element (max) with the last element.
- Swap 20 with 35.
- Reduce the size of the heap (exclude the last element).
- Heapify the root element.
- After heapifying: [30, 15, 25, 12, 9, 20, 10, 5, 7]
- Repeat steps 2-4 until the array is sorted.
- Heap after each iteration:
- [30, 15, 25, 12, 9, 20, 10, 5, 7]
- [25, 15, 20, 12, 9, 7, 10, 5]
- [20, 15, 10, 12, 9, 7, 5]
- [15, 12, 10, 5, 9, 7]
- [12, 9, 10, 5, 7]
- [10, 9, 7, 5]
- [9, 5, 7]
- [7, 5]
- [5]
- Sorted Array: [5, 7, 9, 10, 12, 15, 20, 25, 30, 35]
Q3. Consider an application scenario where Bucket Sort is utilized for sorting large- scale genomic data consisting of DNA sequences. Discuss how the choice of bucket size impacts the efficiency and correctness of the sorting process in this context.
- Efficiency:
- Bucket Overhead: Choosing a small bucket size may result in a large number of buckets, leading to overhead in managing and sorting each bucket. Conversely, selecting a large bucket size might reduce the number of buckets but may result in some buckets being very large, potentially impacting the efficiency of sorting within each bucket.
- Memory Usage: Larger bucket sizes require more memory to store the elements within each bucket. For large-scale genomic data, memory usage can become a critical factor, especially if the dataset is enormous. Choosing an appropriate bucket size can help balance memory usage and sorting efficiency.
- Cache Efficiency: Modern processors utilize cache memory to speed up memory access. A smaller bucket size might lead to better cache efficiency as more data fits into cache at once, reducing memory access times.
- Correctness:
- Preservation of Relative Order: In the context of genomic data, the relative order of DNA sequences may be crucial. If the bucket size is too small, there is a risk of breaking the relative order within each bucket. Conversely, if the bucket size is too large, the sorting within each bucket might not be fine-grained enough to preserve the relative order effectively.
- Data Skewness: The choice of bucket size can impact how evenly the data is distributed among the buckets. Uneven distribution, especially if some buckets are much larger than others, might lead to inefficiencies in sorting and potentially incorrect results if not managed properly.
- Handling Outliers: Genomic data might contain outliers, such as extremely long or short sequences. The choice of bucket size should consider how these outliers are handled to ensure correct sorting. Extremely small bucket sizes might not effectively handle outliers, while extremely large bucket sizes might make outliers dominate a single bucket, affecting the overall sorting process.
- Algorithm Complexity:
- The choice of bucket size might also influence the overall complexity of the sorting algorithm. While larger bucket sizes might reduce the number of buckets and hence the overhead of managing them, it might increase the complexity of sorting within each bucket if not chosen appropriately.
Q4. Given the top pointer of stack implemented using arrays, write an algorithm to print minimum element of stack and find its running time complexity?
- Push Operation: O(1)
- We are simply appending elements to both mainStack and minStack, which operates in constant time.
- Pop Operation: O(1)
- We are popping elements from mainStack and potentially from minStack if the popped element is the minimum. Both operations are constant time.
- Get Minimum Operation: O(1)
- We are simply returning the top element of minStack, which operates in constant time.
Q5. Your company has assigned a task for you to develop software for a small inventory management system used by a local grocery store. The system needs to maintain an ordered list of products based on their expiry dates, with the product closest to expiration appearing first. The list will be updated frequently as new products arrive and as existing products are sold.
i) Considering the requirements of the inventory management system, discuss the suitability of using the Insertion Sort algorithm for the grocery store.
ii).Would you recommend any other algorithms.? Justify your recommendation.
- Good for small datasets: Insertion Sort excels with smaller datasets. Since a local grocery store likely has a manageable number of products, Insertion Sort's performance won't be too slow for frequent updates.
- Simple to implement: The code for Insertion Sort is relatively easy to understand and implement, which can be beneficial for a small project.
- Poor performance for large datasets: As the inventory grows, Insertion Sort's performance degrades significantly. Frequent insertions can lead to a lot of shifting elements, making it inefficient
- Selection Sort: Similar to Insertion Sort, but easier to understand. However, it also suffers from poor performance with larger datasets.
- Heap Sort: More efficient than Insertion Sort and Selection Sort for frequent insertions. It maintains a partially sorted structure (heap) for faster insertions with a time complexity of O(log n) for insertion.
- Faster insertions: As the grocery store adds new products, Heap Sort's logarithmic time complexity for insertion ensures faster updates compared to Insertion Sort's linear time complexity.
- Efficient for partially sorted data: Inventory data might already be somewhat sorted by expiry date as close-to-expiration items are prioritized during stocking. Heap Sort performs well with partially sorted data.
- For a very small inventory and simplicity is key, Insertion Sort might suffice.
- For a larger inventory or if faster updates are crucial, consider Heap Sort.
Q6.Consider the following data:
Hash(x)=x mod 11, hi=(Hash(x)+F(i))%11
Where F(i)=i*h2(x) with h2(x)=7-(xmod7) and i is the ith time hashing is applied for same value of x when collision occurs. Solve the following double hashing problem for the input 97,30,108,33,44 clearly indicating the collisions.
- Initialize an array of size 11 to store the values.
- For each input value:
- Compute the primary hash using Hash(x) = x mod 11.
- If the slot is empty, store the value there.
- If there's a collision, resolve it using double hashing: hi = (Hash(x) + F(i)) % 11, where F(i) = i * h2(x).
- Continue until an empty slot is found.
- Repeat this process for all input values.
- Input values: 97, 30, 108, 33, 44
- Compute hashes and handle collisions:
- For 97:
- Primary hash: Hash(97) = 97 % 11 = 9 (Slot 9 is empty)
- Store 97 at Slot 9.
- For 30:
- Primary hash: Hash(30) = 30 % 11 = 8 (Slot 8 is empty)
- Store 30 at Slot 8.
- For 108:
- Primary hash: Hash(108) = 108 % 11 = 9 (Collision with 97)
- Secondary hash: h2(108) = 7 - (108 % 7) = 7 - 6 = 1
- F(1) = 1 * h2(108) = 1 * 1 = 1
- h1 = (9 + 1) % 11 = 10 (Slot 10 is empty)
- Store 108 at Slot 10.
- For 33:
- Primary hash: Hash(33) = 33 % 11 = 0 (Slot 0 is empty)
- Store 33 at Slot 0.
- For 44:
- Primary hash: Hash(44) = 44 % 11 = 0 (Collision with 33)
- Secondary hash: h2(44) = 7 - (44 % 7) = 7 - 2 = 5
- F(1) = 1 * h2(44) = 1 * 5 = 5
- F(2) = 2 * h2(44) = 2 * 5 = 10
- h1 = (0 + 5) % 11 = 5 (Slot 5 is empty)
- Store 44 at Slot 5.
- Final hash table:
Scroll Job Updates
Scroll Job Updates is one of the most popular job sites in India, Scroll Job Updates helps jobseekers, mainly freshers to find the latest jobs in accordance with their qualifications in all possible fields like Govt, Private, Engineering, IT, Bank, Defense, etc. As a leading job portal, Scroll Job Updates provide free job alerts and send mailers to candidates, and helps them find the best job opportunities in their respective fields of expertise. Scroll Job Updates is the best website for freshers who just completed their education and looking for a job. As a top online job portal, we act as a medium to connect freshers with recruiters and employers and help them to find suitable jobs according to their profession and educational qualification.