DAA LAB - Fast Learner's - Problem - 22CSH - ITH-311
DAA LAB - Fast Learner's - Problem - 22CSH - ITH-311
Given the pointer to the head node of a linked list, change the next pointers of the nodes so
that their order is reversed. The head pointer given may be null meaning that the initial list is
empty.
Example
head references the list 1->2->3->4->NULL
Manipulate the pointers of each node in place and return head, now referencing the head of
the list 3->2->1->NULL .
Function Description
Complete the reverse function.
reverse has the following parameter:
● SinglyLinkedListNode pointer head: a reference to the head of a list
Returns
● SinglyLinkedListNode pointer: a reference to the head of the reversed list
Input Format
The first line contains an integer t, the number of test cases.
Each test case has the following format:
The first line contains an integer n, the number of elements in the linked list.
Each of the next n lines contains an integer, the data values of the elements in the linked list.
Constraints
Problem 2:
Write the postOrder function. It received 1 parameter: a pointer to the root of a binary tree.
It must print the values in the tree's postorder traversal as a single line of space-separated
values.
Input Format
Our test code passes the root node of a binary tree to the postOrder function.
Constraints
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Problem 3:
You are given a pointer to the root of a binary search tree and values to be inserted into the
tree. Insert the values into their appropriate position in the binary search tree and return the
root of the updated binary tree.
Input Format
You are given a function,
Node * insert (Node * root ,int data) {
}
Constraints
Problem 4:
Huffman Coding assigns variable length codewords to fixed length input characters based on
their frequencies. More frequent characters are assigned shorter codewords and less frequent
characters are assigned longer codewords. All edges along the path to a character contain a
code digit. If they are on the left side of the tree, they will be a 0 (zero). If on the right, they'll
be a 1 (one). Only the leaves will contain a letter and its frequency count. All other nodes will
contain a null instead of a character, and the count of the frequency of all of it and its
descendant characters.
For instance, consider the string ABRACADABRA. There are a total of 11 characters in the
string. This number should match the count in the ultimately determined root of the tree. Our
frequencies are A= 5, B=2, R=2,C=1 and D-1. The two smallest frequencies are for C and D,
both equal to 1 , so we'll create a tree with them. The root node will contain the sum of the
counts of its descendants, in this case 1+1 =2 . The left node will be the first character
encountered, C , and the right will contain D . Next we have 3 items with a character count
of 2 : the tree we just created, the character B and the character R. The tree came first, so it
will go on the left of our new root node. B will go on the right. Repeat until the tree is
complete, then fill in the 1 's and 0 's for the edges. The finished graph looks like:
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Input characters are only present in the leaves. Internal nodes have a character value of ϕ
(NULL). We can determine that our values for characters are:
A-0
B - 111
C - 1100
D - 1101
R - 10
Our Huffman encoded string is:
AB R AC AD AB R A
0 111 10 0 1100 0 1101 0 111 10 0
or
01111001100011010111100
To avoid ambiguity, Huffman encoding is a prefix free encoding technique. No codeword
appears as a prefix of any other codeword.
To decode the encoded string, follow the zeros and ones to a leaf and return the character
there.
You are given pointer to the root of the Huffman tree and a binary coded string to decode.
You need to print the decoded string.
Function Description
Complete the function decode_huff in the editor below. It must return the decoded string.
decode_huff has the following parameters:
● root: a reference to the root node of the Huffman tree
Input Format
There is one line of input containing the plain string, . Background code creates the Huffman
tree then passes the head node and the encoded string to the function.
Constraints
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Problem 5:
we're covering a divide-and-conquer algorithm called Quicksort (also known as Partition
Sort). This challenge is a modified version of the algorithm that only addresses partitioning. It
is implemented as follows:
Step 1: Divide
Choose some pivot element, p, and partition your unsorted array, arr, into three smaller
arrays: ,left ,right and equal, where each element in left < p, each element in right > p, and
each element in equal = p .
Example
arr = {5,7,4,3,8}
In this challenge, the pivot will always be at arr[0], so the pivot is 5.
arr is divided into left = {4,3},equal = {5} , and right = {7,8}.
Putting them all together, you get {4,3,5,7,8} . There is a flexible checker that allows the
elements of left and right to be in any order. For example, {3,4,5,8,7} is valid as well.
Given arr and p =arr[0], partition arr into ,left, right and equal. using the Divide instructions
above. Return a 1-dimensional array containing each element in left first, followed by each
element in equal, followed by each element in right.
Function Description
quickSort has the following parameter(s):
● int arr[narr[0] is the pivot element
Returns
● int[n]: an array of integers as described above
Input Format
The first line contains n, the size of arr .
The second line contains n space-separated integers arr[i] (the unsorted array). The first
integer,arr[0] , is the pivot element,p .
Constraints
Problem 6:
Given an amount and the denominations of coins available, determine how many ways
change can be made for amount. There is a limitless supply of each coin type.
Example
n =3
c = [8, 3, 1, 2]
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
There are 3 ways to make change for n =3: {1,1,1} ,{1,2} , and {3}.
Function Description
Complete the getWays function in the editor below.
getWays has the following parameter(s):
● int n: the amount to make change for
Problem 7.
Samantha and Sam are playing a numbers game. Given a number as a string, no leading
zeros, determine the sum of all integer values of substrings of the string.
Given an integer as a string, sum all of its substrings cast as integers. As the number may
become large, return the value modulo 109 + 7.
Example
N = “42”
Here n is a string that has 3 integer substrings: 4,2, and 42. Their sum is 48, and 48 modulo
(109 + 7 ) = 48.
Function Description
Complete the substrings function in the editor below.
substrings has the following parameter(s):
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Input Format
A single line containing an integer as a string, without leading zeros.
Constraints
Problem 8:
Marc loves cupcakes, but he also likes to stay fit. Each cupcake has a calorie count, and Marc
can walk a distance to expend those calories. If Marc has eaten j cupcakes so far, after eating
a cupcake with c calories he must walk at least 2i *c miles to maintain his weight.
Example
calorie = [5,10,7]
If he eats the cupcakes in the order shown, the miles he will need to walk are (20 *5) + (21*
10) +(22*7) = 5+20+28 = 53. This is not the minimum, though, so we need to test other
orders of consumption. In this case, our minimum miles is calculated as (20 *10) + (21* 7)
+(22*5) = 10+14+20 = 44.
Given the individual calorie counts for each of the cupcakes, determine the minimum number
of miles Marc must walk to maintain his weight. Note that he can eat the cupcakes in any
order.
Function Description
Complete the marcsCakewalk function in the editor below.
marcsCakewalk has the following parameter(s):
● int calorie[n]: the calorie counts for each cupcake
Returns
● long: the minimum miles necessary
Input Format
The first line contains an integer n, the number of cupcakes in .
The second line contains n space-separated integers,calorie[i] .
Constraints
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Problem 9.
Priyanka works for an international toy company that ships by container. Her task is to the
determine the lowest cost way to combine her orders for shipping. She has a list of item
weights. The shipping company has a requirement that all items loaded in a container must
weigh less than or equal to 4 units plus the weight of the minimum weight item. All items
meeting that requirement will be shipped in one container.
What is the smallest number of containers that can be contracted to ship the items based on
the given list of weights?
For example, there are items with weights w = [1,2,3,4,5,10,11,12,13] . This can be broken
into two containers:[1,2,3,4,5] and [10,11,12,13] . Each container will contain items
weighing within units of the minimum weight item.
Function Description
It should return the minimum number of containers required to ship.
toys has the following parameter(s):
● w: an array of integers that represent the weights of each order to ship
Input Format
The first line contains an integer n, the number of orders to ship.
The next line contains space-separated integers, w[1], w[2],…..,w[n] , representing the
orders in a weight array.
Constraints
Problem 10.
In this problem, we'll use the term "longest common substring" loosely. It refers to substrings
differing at some number or fewer characters when compared index by index. For example,
'abc' and 'adc' differ in one position, 'aab' and 'aba' differ in two.
Given two strings and an integer k , determine the length of the longest common substrings of
the two strings that differ in no more than k positions.
For example, k=1. Strings s1 = abcd and s2 = bbca. Check to see if the whole string (the
longest substrings) matches. Given that neither the first nor last characters match and 2>k ,
we need to try shorter substrings. The next longest substrings are s1’ = [abc, bcd] and s2’ =
[bbc,bca]. Two pairs of these substrings only differ in 1 position: [abc, bbc]and [bcd,bca] .
They are of length 3 .
Function Description
Complete the substringDiff function in the editor below. It should return an integer that
represents the length of the longest common substring as defined.
substringDiff has the following parameter(s):
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Input Format
The first line of input contains a single integer,t , the number of test cases follow.
Each of the next t lines contains three space-separated values: an integer k and two
strings, s1 and s2.
Constraints