0% found this document useful (0 votes)
4 views

Data Structures Module Bank-1 (Section-I)

The document outlines a comprehensive set of programming tasks related to data structures, including sorting algorithms, queue and stack operations, linked list manipulations, and searching techniques. Each question presents a specific problem requiring the design of algorithms to solve it, with examples and expected outputs provided. The tasks are aimed at enhancing understanding of data structures and algorithm efficiency in various scenarios.

Uploaded by

nandinivemula229
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structures Module Bank-1 (Section-I)

The document outlines a comprehensive set of programming tasks related to data structures, including sorting algorithms, queue and stack operations, linked list manipulations, and searching techniques. Each question presents a specific problem requiring the design of algorithms to solve it, with examples and expected outputs provided. The tasks are aimed at enhancing understanding of data structures and algorithm efficiency in various scenarios.

Uploaded by

nandinivemula229
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

VIGNAN’S FOUNDATION FOR SCIENCE, TECHNOLOGY & RESEARCH

Department of Computer Science and Engineering


Module Bank-1
Course Name: Data Structures Academic Year: 2024-25
Course Code: 22TP201 Year & Section: II & I

Question-1 (Sorting)
a) Mr. John is a chemist who receives ten medicine boxes with batch numbers 35, 33, 42, 10,
14, 19, 27, 44, 26, 31 printed on them. He always arranges the boxes manually and gets
frustrated every time. He thought he would have a lot of problems in the future arranging the
boxes if the number of boxes of medicine was large. He wants to make this task easier. As you
are a good programmer, Mr. John is asking for your help. Design an optimal quick sort
algorithm to arrange the boxes in increasing order of batch numbers and show each step of the
algorithm in detail using the above sequence of batch numbers. Also, write the best-case and
worst-case time complexity of the algorithm.
b) Given two sorted arrays A and B having numbers of elements m and n, respectively. Design
an algorithm of linear time complexity to merge A and B into a third array C, such that C is
also sorted and having m + n number of elements.

Question-2 (Queue & Stack)


a) A cable connection operator has n cable wires of various lengths and wants to give the cable
connection to a customer from his office. He wants to connect these wires into a single wire.
The cost to join two cable wires is equal to the sum of their lengths. Design an algorithm based
on priority queue to join these cable wires with minimum cost.
Example
Suppose the cable operator has four cable wires of lengths {5, 2, 3, 9}. He first joins the cables
of lengths 2 and 3. The cost of joining is {2+3=5}. Now he has three wires of lengths {5, 5, 9}.
Further, he joins the wire of lengths 5 and 5, and the cost is {5+5 = 10}. The total cost till now
becomes {5+10 = 15}. Now, he has two wires of lengths 10 and 9. Finally, he connects the last
two wires to have all the wires connected. The total cost becomes {15+19=34}.

b) A game will be organized for the children attending the birthday party of Rahul. There will
be ten gift packets put on one another in the form of a stack on a table. A number is printed on
all the gifts, and the numbers are {11, 7, 9, 45, 35, 46, 89, 23, 65, 90}. The gift with the 11
number is the top gift, and the number 90 is the bottom gift of the stack. A child will come near
the table and will arrange the gifts in decreasing order of numbers. Design a recursive algorithm
to help the child sort all the gifts in decreasing order using stack data structure only.
Question-3 (Stack & Sorting)
Design an algorithm for the given array of integers and you have to sort it using a single stack
A. You are allowed to only push elements from the given array of integers (treating it as a
queue), and your sequence of pops should form a sorted sequence of the given array. You have
to return the sequence of operations (described below) in order to sort the elements.
Allowed operations:
1. Extract one input element (with least index) and insert in the stack. Represented as PUSH
2. Pop the top-most element of the stack. Represented as POP
3. If it is not possible to sort the given numbers with a single stack, then NOT POSSIBLE.
Return the sequence of operations as an Array of String. Your sequence should correspond to
a valid sequence of operations, and the pop sequence should form a sorted array of given
numbers (in ascending order). Your number of PUSH and POP should be exactly equal to the
length of the given array of numbers.
Example 1:
Input: [10, 702, 36, 125, 82]
Output: [“PUSH”, “POP”, “PUSH”, “PUSH”, “POP”, “PUSH”, “PUSH”, “POP”, “POP”,
“POP”]
Explanation: The POP sequence forms the list [10, 36, 82, 125, 702] which is sorted.
Example 2:
Input: [1023, 5029, 158]
Output: [“NOT POSSIBLE”]
Question-4 (Linked list)
Design a Menu Driven based approach for the following operations:
a) Print all the elements at the index of multiples of k with the first element assumed to have
an index of 0. Do this for a single pass of the linked list.
Input: k=3
12 -> 15 -> 18 -> 17 -> 19 -> 20 -> 22 -> NULL
Output: 12 -> 17 -> 22 -> NULL

b) Remove in a linked list all the nodes that have a greater value to their right.
Input:
10 -> 12 -> 15 -> 20 -> 5 -> 16 -> 25 -> 8 -> NULL
10 -> 12 -> 15 -> 20 -> 25 -> 26 -> 30 -> 40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL
Output:
20 -> 25 -> 8 -> NULL
40 -> NULL
20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> NULL

c) Perform pair-wise swapping of nodes of a given linked list.


Input: 20 -> 18 -> 15 -> 10 -> 8 -> 6 -> 5 -> 3 -> 7 -> NULL
Output: 18 -> 20 -> 10 -> 15 -> 6 -> 8 -> 3 -> 5 -> 7 -> NULL

d) Swap k-th node from the beginning with k-th node from the end in a linked list.
Input: k=1 k=2 k=3
1 -> 2 -> 3 -> NULL
1 -> 2 -> 3 -> NULL
1 -> 2 -> 3 -> NULL
Output:
3 -> 2 -> 1 -> NULL
1 -> 2 -> 3 -> NULL
3 -> 2 -> 1 -> NULL
Question-5 (Sorting & Linked list)
Design an Algorithm that takes in the head of Linked List and an Integer 𝐾, rearranges the
list in place (i.e., doesn’t create a brand new list) around nodes with value 𝐾, and return its
new head. Rearranging a Linked List around nodes with value 𝐾, means moving all nodes
with a value smaller than 𝐾 before all nodes with value 𝐾 and moving all nodes with a value
greater than 𝐾 after all nodes with value 𝐾.
All moved nodes should maintain their original relative ordering if possible. Note that the
linked list should rearrange even if it doesn’t have any nodes with value 𝐾. Each Linked List
node has aninteger value as well as a next node pointing to the next node in the list or to None
/ Null if it’s thetail of the list. You can assume that the input Linked List will always have at
least one node; in other words, the head will never be None / Null.
Sample Input:
head = 3 -> 0 -> 5 -> 2 -> 1 -> 4 // the head node with value 3
k=3
Sample Output:
0 -> 2 -> 1 -> 3 -> 5 -> 4 // the new head with value 0
// Note that the nodes with values 0, 2, and 1 have maintained their original relative
ordering, andso have the nodes with values 5 and 4.
Hints
Hint 1:
The final linked list that you have to return essentially consists of three linked lists attached
to oneanother: one with nodes whose values are smaller than k, one with nodes whose
values are equal to k, and one with nodes whose values are greater than k.
Hint 2:
Iterate through the linked list once, build the three linked lists mentioned in Hint #1 as you
go, andfinally connect these three linked lists to form the rearranged lists.
Hint 3:
To build the three linked lists mentioned in Hints #1 and #2, you will have to keep track
of their heads and tails and update the appropriate linked list’s tail with each node that you
traverse as youiterate through the main linked list. You can determine which linked list is
the relevant one by simply comparing the value of the node that you’re traversing to k.
Hint 4:
Connecting the three linked lists mentioned in the previous Hint won’t be as simple as it
sounds, mainly because one or two of the linked lists might actually be empty, depending
on the various nodes’ values and the value of k.
Question-6 (Sorting)
Design an algorithm for the Given n*m array (2D) and you need to perform the following
operations:
N Rows and M columns (input from user). Your task is to sort an array based on columns
i.e.,
• Based on first column, sort the entire array using bubble sort and must be
sorted inascending order.
• Based on second column, sort the entire with array using selection sort but in
descendingorder.
• Based on third column, sort the entire array with insertion sort and in ascending
order.
• Based on fourth column, sort the entire array with Merge sort in descending order
• Based on fifth column, sort the entire array with quick sort in
descending order.Note: During sorting it must be sorted with minimum no of
swaps.
And if there are less columns, for example only 4 columns are present then you need to
perform up to merge sort only and if more than five columns then proceed again from the
first step i.e., from bubble sort. The order of sorting will be ascending for odd numbered
columns and descendingfor even numbered columns.
Sample I/P:
N = 4, M = 4
1450
2539
5 9 10 8
3741
Output: At every step display the elements in the array for every time it has been sorted.
Mentionthe sort that it had done.
Question-7 (Searching)
Design an Algorithm to implement following programs.
a) What is the best sorting algorithm for an array which is almost sorted? Illustrate with an
example using number of swapping.
b) Given a sequence of numbers
n such that the difference between the consecutive terms is
constant, find the missing term in logarithmic time. Assume that the first and last elements
are always part of the input sequence and the missing number lies between index 1 to n-1.
Input: [5, 7, 9, 11, 15]
Output: The missing term is 13
c) Given a sequence of n number and create a sub sequence with all prime numbers in an
optimal time.
Input: [5, 7, 9, 11, 15]
Output: [5,7,11]

d) In the given sequence of values, find out list of numbers which are having multiple divisors.
Don’t consider 1 and itself as divisors. Sort the sequence according to the numbers with
more divisors.
Input: [5, 7, 9, 11, 15]
Output: [15,9,5,7,11]
Question-8 (Sorting & Searching)
Design an algorithm for the following problem statement,
Given a list of sorted elements and a key element where we need to find its position in the given
list.
 Implement in at least two approaches and compare their complexities.
 Now instead of a single key, we are asking for certain queries, where each query
contains a key and we need to print the count of its occurrence in the given list.
 Implement in Brute force such that the time complexity is Q * N.
 Optimize it such that the time complexity is Q * log N (Hint : Use Binary
Search ).
 What if the list given is not sorted? Come up with best possible solution and
calculate its time and space complexities.
 Let us now consider a list of sorted elements and a key is specified at each query.
We need to print an element largest A value, such that A<=key, where A belongs to
the given list of elements. If no element found print -1.
Sample Testcase :
Input : -8, -3, -5, 10, 12, 15, 18, 21, 29 and say we have 2 queries.
Query1 : 14 Output1 : 12
Query2 : 21 Output2 : 21 Note : Use Binary Search only.
 We know that Binary Search is an improvement to Linear Search. However, there
is an improvement over Binary Search called Interpolation Search.
What kind of data is suitable to apply Interpolation Search and Why?
Take a set of data and apply both Binary and Interpolation Searching
Techniques and compare their performance as well as complexities.
Question-9 (Linked List)
Design an Algorithm to take an integer K and K number of sorted linked lists of various
lengths. implement the following functions:

● Store all K linked list heads in an array of pointers.


● Write a function which takes an array of linked lists and merges all sorted linked
lists intoa single linked list and returns the linked list.
● For the above sorted single linked list delete the duplicate nodes in the list a
● Print the resultant linked list.

Sample Input (Optional):


K=3
List1 = 1->1->4->7-
>NULLList2 = 2-
>4->NULL
List3 = 3->5->7->NULL
Sample Output (Optional):
List = 1->2->3->4->5->7->NULL
Hints If any:
Use a new linked list for merging the array of linked lists.
Question-9 (Stacks)
Design an algorithm to determine the number of Atoms using stack
Given a string formula representing a chemical formula, return the count of each atom. The
atomicelement always starts with an uppercase character, then zero or more lowercase letters,
representing the name. One or more digits representing that element's count may follow if the
count is greater than 1. If the count is 1, no digits will follow.
• For example, "H2O" and "H2O2" are possible, but "H1O2" is
impossible. Two formulas are concatenated together to produce
another formula.
• For example, "H2O2He3Mg4" is also a formula.
A formula placed in parentheses, and a count (optionally added) is also a formula.
• For example, "(H2O2)" and "(H2O2)3" are formulas.
Return the count of all elements as a string in the following form: the first name (in sorted
order),followed by its count (if that count is more than 1), followed by the second name (in
sorted order), followed by its count (if that count is more than 1), and so on.
The test cases are generated so that all the values in the output fit in a 32-bit
integer.Example 1:
Input: formula =
"H2O"Output:
"H2O"
Explanation: The count of elements are {'H': 2,
'O': 1}.Example 2:
Input: formula =
"Mg(OH)2"Output:
"H2MgO2"
Explanation: The count of elements are {'H': 2, 'Mg': 1,
'O': 2}.Example 3:
Input: formula =
"K4(ON(SO3)2)2"Output:
"K4N2O14S4"
Explanation: The count of elements are {'K': 4, 'N': 2, 'O': 14,
'S': 4}.
Constraints:
• 1 <= formula. length <= 1000
• formula consists of English letters, digits, '(', and ')'.
• formula is always valid
Question-10 (Queues)
Design an Algorithm with the following functionalities.
 Read the input array dynamically.
 Read the value of K.
 Command “IF” indicates Replacing first value of an array with difference of
arr[0] value andpresent Input value.

Note: Don’t use “–“ operator.


-> Command “IL” indicates replacing last value of an array with present input value.
-> Command “DF” Indicates replacing the first value with XOR
value of(first highest + second highest) XOR (first highest –
second highest) Note: Don’t use directly + and – operators.
Hint: Use Bitwise operators.
-> Command “DL” Indicates replacing the last value with AND value of third least
and thirdhighest value.
Continue the above process until first value of an array is strictly greater than the value of
“K”.

You might also like