0% found this document useful (0 votes)
5 views7 pages

Midterm DSA INT2210 5

The document outlines the instructions and tasks for a DSA midterm exam, including rules against cheating and file submission requirements. It contains programming tasks related to data structures and algorithms, such as sorting a doubly linked list, converting infix to postfix expressions, AVL tree insertion, heap sort implementation, and finding minimum button clicks to transform numbers. Additionally, it includes a task for grouping anagrams from a list of strings.
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)
5 views7 pages

Midterm DSA INT2210 5

The document outlines the instructions and tasks for a DSA midterm exam, including rules against cheating and file submission requirements. It contains programming tasks related to data structures and algorithms, such as sorting a doubly linked list, converting infix to postfix expressions, AVL tree insertion, heap sort implementation, and finding minimum button clicks to transform numbers. Additionally, it includes a task for grouping anagrams from a list of strings.
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/ 7

DSA Midterm

Nguyen Tien Dat


March 17, 2025

Midterm Exam Instructions:

• No documents, bots, or cheating.

• Use the provided template; do not alter it.

• Additional helper functions allowed.

• Save all work to drive Z only.

• Submission format:

• Each question in separate files (e.g., 1.cpp, 2.cpp).

• Zip all files into MSSV.zip before submitting.

1
1. Given a doubly linked list, the task is to sort the doubly linked list in non-decreasing
order using merge sort.
Examples:
Input: 10 ↔ 8 ↔ 4 ↔ 2
Output: 2 ↔ 4 ↔ 8 ↔ 10
Input: 5 ↔ 3 ↔ 2
Output: 2 ↔ 3 ↔ 5
// C ++ program for merge sort on doubly linked list

# include < iostream >


using namespace std ;

class Node {
public :
int data ;
Node * next ;
Node * prev ;

Node ( int x) {
data = x;
next = NULL ;
prev = NULL ;
}
};

// Function to merge two sorted doubly linked lists


Node * merge ( Node * first , Node * second ) {
}

// Function to perform merge sort on a doubly linked list


Node * MergeSort ( Node * head ) {
}

void printList ( Node * head ) {


Node * curr = head ;
while ( curr != NULL ) {
cout << curr - > data << " " ;
curr = curr - > next ;
}
cout << endl ;
}

int main () {

// Create a hard - coded doubly linked list :


// 10 <-> 8 <-> 5 <-> 2
Node * head = new Node (10) ;
head - > next = new Node (8) ;
head - > next - > prev = head ;
head - > next - > next = new Node (5) ;

2
head - > next - > next - > prev = head - > next ;
head - > next - > next - > next = new Node (2) ;
head - > next - > next - > next - > prev = head - > next - > next ;

head = MergeSort ( head ) ;

printList ( head ) ;

return 0;
}

2. Write a program to convert an Infix expression to Postfix form.


Infix expression: The expression of the form “a operator b” (a + b) i.e., when an
operator is in-between every pair of operands.
Postfix expression: The expression of the form “a b operator” (ab+) i.e., when every
pair of operands is followed by an operator.
Examples:
Input: A + B * C + D
Output: ABC*+D+
Input: ((A + B) – C * (D / E)) + F
Output: AB+CDE/*-F+
# include < bits / stdc ++. h >
using namespace std ;

// The main function to convert infix expression


// to postfix expression
// Students are allowed to use the c ++ standard stack library ,
with methods : top () , pop () , push () , empty ()
void infixToPostfix ( string s ) {
}

int main () {
string exp = " a + b *( c ^d - e ) ^( f + g * h ) -i " ;
infixToPostfix ( exp ) ;
return 0;
}

3. Given a AVL, the task is to insert a node in this AVL.


// C ++ program to insert a node in AVL tree
# include < bits / stdc ++. h >
using namespace std ;

// An AVL tree node


struct Node {
int key ;
Node * left ;
Node * right ;
int height ;

3
Node ( int k ) {
key = k ;
left = nullptr ;
right = nullptr ;
height = 1;
}
};

// A utility function to right


// rotate subtree rooted with y
Node * rightRotate ( Node * y ) {
}

// A utility function to left rotate


// subtree rooted with x
Node * leftRotate ( Node * x ) {
}

// Recursive function to insert a key in


// the subtree rooted with node
Node * insert ( Node * node , int key ) {
}

// A utility function to print


// preorder traversal of the tree
void preOrder ( Node * root ) {
if ( root != nullptr ) {
cout << root - > key << " " ;
preOrder ( root - > left ) ;
preOrder ( root - > right ) ;
}
}

// Driver Code
int main () {
Node * root = nullptr ;

// Constructing tree given in the above figure


root = insert ( root , 10) ;
root = insert ( root , 20) ;
root = insert ( root , 30) ;
root = insert ( root , 40) ;
root = insert ( root , 50) ;
root = insert ( root , 25) ;

/* The constructed AVL Tree would be


30
/ \
20 40
/ \ \
10 25 50
*/
cout << " Preorder traversal : \ n " ;

4
preOrder ( root ) ;

return 0;
}

4. Implement heap sort algorithms.


// C ++ program for implementation of Heap Sort using vector

# include < bits / stdc ++. h >


using namespace std ;

// To heapify a subtree rooted with node i


// which is an index in arr [].
void heapify ( vector < int >& arr , int n , int i ) {

// Main function to do heap sort


void heapSort ( vector < int >& arr ) {

// A utility function to print vector of size n


void printArray ( vector < int >& arr ) {
for ( int i = 0; i < arr . size () ; ++ i )
cout << arr [ i ] << " " ;
cout << " \ n " ;
}

// Driver ’s code
int main () {
vector < int > arr = { 9 , 4 , 3 , 8 , 10 , 2 , 5 };

// Function call
heapSort ( arr ) ;

cout << " Sorted array is \ n " ;


printArray ( arr ) ;
}

5. Vasya has found a strange device. On the front panel of a device there are: a red
button, a blue button and a display showing some positive integer. After clicking the red
button, device multiplies the displayed number by two. After clicking the blue button,
device subtracts one from the number on the display. If at some point the number stops
being positive, the device breaks down. The display can show arbitrarily large numbers.
Initially, the display shows number n. Bob wants to get number m on the display. What
minimum number of clicks he has to make in order to achieve this result?
Input: The first and the only line of the input contains two distinct integers n and m
(1 ≤ n, m ≤ 104 ), separated by a space.
Output: Print a single number — the minimum number of times one needs to push the
button required to get the number m out of number n.

5
Examples:
Input: 4 6
Output: 2
Input: 10 1
Output: 9
// C ++ program to find minimum number of clicks
# include < bits / stdc ++. h >
using namespace std ;

// Function to find minimum number of button clicks


int minClicks ( int n , int m ) {

// Driver ’s code
int main () {
int n = 4 , m = 6;
cout << " Minimum clicks to transform " << n << " to " << m <<
" : " << minClicks (n , m ) << endl ;

return 0;
}

6. Given an array of strings strs. An anagrams is a word or phrase formed by rearranging


the letters of a different word or phrase, using all the original letters exactly once. Your
task is to group the anagrams together. You can return the answer in any order.
Constraints: 1 ≤ strs.length ≤ 104 , 0 ≤ strs[i].length ≤ 100, strs[i] consists of
lowercase English letters
Examples:
Input 1: strs = [”eat”,”tea”,”tan”,”ate”,”nat”,”bat”]
Output 1:
3
bat
ate eat tea
nat tan
Input 2: strs = [”a”]
Output 2:
1
a
// C ++ program for grouping anagrams
# include < bits / stdc ++. h >
using namespace std ;

// Function to group anagrams together


vector < vector < string > > groupAnagrams ( vector < string >& strs ) {

6
// A utility function to print the grouped anagrams
void pri nt An ag ra mG ro up s ( vector < vector < string > >& groups ) {
cout << groups . size () << endl ;

for ( auto & group : groups ) {


for ( auto & word : group ) {
cout << word << " " ;
}
cout << endl ;
}
}

// Driver ’s code
int main () {
vector < string > strs = { " eat " , " tea " , " tan " , " ate " , " nat " , "
bat " };

// Function call
vector < vector < string > > anagramGroups = groupAnagrams ( strs ) ;

// Output
cout << " Grouped anagrams : " << endl ;
pr in tA na gr am Gr ou ps ( anagramGroups ) ;

return 0;
}

You might also like