Sunteți pe pagina 1din 6

List of Experiments from Virtual Lab portal http://vlab.co.

in/

Use Appropriate Data Structure to code and solve the following:

String Problems (http://ps-


iiith.vlabs.ac.in/exp5/Introduction.html?domain=Computer%20Science&lab=Problem%20So
lving%20Lab)
Problem 1: A palindrome is a symmetric character sequence that looks the same
when read backwards, i.e. from right to left. Interestingly, human being usually
discards blank spaces and punctuation marks while recognizing a palindromes like:
(a) “Madam, I’m Adam.” (b) “Eve.” etc.
Write a program that detects palindromes. Your program should accept a string from
the key board and output YES/NO.

Input Specification
Input will contain a string S entered from keyboard, with number of characters
less than 1000.
Output Specification
Output must be either "YES" or "NO"(quotes for clarity) depending upon
whether or not it is a palindrome.
Sample Input and Output
Input: Madam, I'm Adam
Output: YES
Input: Eve.
Output: NO

Problem 2: Given two strings, PRINT (YES or NO) whether the second string can be
obtained from the first by deletion of none, one or more characters.

Input Specification
Input will contain two strings separated by a space and size of each string is at
max 107.
Output Specification
Output must be either "YES" or "NO" depending on whether 2nd string can be
obtained from 1st string.
Sample Input and Output
Input: hyderabad her
Output: YES
Input: hyderabad dear
Output: NO

Recursion
(http://psiiith.vlabs.ac.in/exp6/Introduction.html?domain=Computer%20Science&lab=Proble
m%20Solving%20Lab)

Problem 1: You are given scales for weighing loads. On the left side lies a single
stone of known weight W < 2N. You own a set of N different weights, weighing 1, 2,
4, ..., 2N−1 units of mass respectively. Determine how many possible ways there are
of placing some weights on the sides of the scales, so as to balance them (put them in
a state of equilibrium).

Input Specification
The input line contains two integers: N W, where N denotes the number of
weights and W represents the weight to be placed on the left side.
Output Specification
Output must be a single integer denoting the number ways in which one can
balance the weight W by placing weights on any side.
Sample Input and Output
Input: 24
Output: 3
Input: 5 10
Output: 14

Problem 2: Given a weighing pan, n weights and a destination weight D, print “YES”
or “NO” depending whether you can weight D using other weights given.

Input Specification
Input begins with numbers of weights n, then n values denoting mass of each
weight and then in the end destination weight D.
Output Specification
As the output, print “YES” if it is possible to weight D, otherwise “NO”.
Sample Input and Output
Input: 31342
Output: YES
Input: 2135
Output: NO

Advanced Arithmetic
http://ps-
iiith.vlabs.ac.in/exp7/Introduction.html?domain=Computer%20Science&lab=Problem%20So
lving%20Lab)
Problem 2: Find the square root of a positive integer using binary search.
Input Specification
Input will contain a single positive integer N input from keyboard, lesser than
10^9.
Output Specification
Print the square root of the number upto 4 decimal places. A difference upto
0.001 from the the judges solution will be accepted as correct.
Sample Input and Output
Input: 5
Output: 2.2361
Input: 25
Output: 5.0000
Searching and Sorting
(http://ps-
iith.vlabs.ac.in/exp8/Introduction.html?domain=Computer%20Science&lab=Problem%20Sol
ving%20Lab)
Problem 1: You are given some set of numbers. By choosing three numbers we may
be able to construct a triangle. In this problem we ask you to find out the number of
invalid triangles formed from these set of numbers. The degenerate triangles are
considered to be valid triangles.

Input Specification
Input contains N(<= 1000), followed by N numbers in the next line. The
numbers are separated by spaces and each number is > 0 and < 106.
Output Specification
Print the number of invalid triangles that can be formed.
Sample Input and Output
Input: 5
1 2 3 4 10
Output: 7

Problem 2: Given a String and a character set we need to find out the length of the
smallest substring which contains all the characters of the set.

Input Specification
The input contains a string(length<=105) made up of small letters from
English language followed by number of characters N(< 26) and then N
characters in the next line follow separated by spaces.
Output Specification
The output must contain the length of the smallest substring which contains all
the characters from the character set. If no such substring exists, report -1.
Sample Input and Output
Input: applelooksred 3 e a p
Output: 5

Sequences
(http://ps-
iiith.vlabs.ac.in/exp10/Introduction.html?domain=Computer%20Science&lab=Problem%20S
olving%20Lab)
Problem 1: Given a list of n distinct numbers (where n <= 1000), find the length of
the longest monotone decreasing subsequence.
Definition : A monotone decreasing subsequence is a sequence of numbers
(contiguously placed in the list) which are strictly decreasing from left to right. For
example, if we have the series “2 1 9 8 10 7 5 4 3 1 10”, then the subsequence “10 7 5
4 3 1” is contiguously arranged and monotonically decreasing. Thus, the answer to be
printed out for the length of “10 7 5 4 3 1” which is 6. Note that the sequence “9 8 7 5
4 3 1” is not a subsequence we are looking for as the numbers are not contiguous
placed in the list.

Input Specification
Input will contain some positive integers separated by spaces. The number of
integers is not specified and the input will be terminated by end of file. Each
integer is <= 109 and the length of the input sequence is <= 107.
Output Specification
Print the length of the longest continuous decreasing subsequence.
Sample Input and Output
Input: 2 1 9 8 10 7 5 4 3 1 10
Output: 6
Input: 12345678
Output: 1

Problem 2: Given a set of n distinct numbers, find the length of the longest monotone
increasing subsequence. Note that the sequence need not be continuous.

Input Specification
Input will contain two lines. The first line contains the number of integers in
the sequence, N(<=1000) and the next line contains N positive integers (each
<=109).
Output Specification
Print the length of the longest increasing subsequence of the given input
sequence.
Sample Input and Output
Input: 10
1 2 9 4 7 3 11 8 14 6
Output: 6
Input: 6
1 3 2 10 4 5
Output: 4

Polynomial Operations
(http://cse01-
iiith.vlabs.ac.in/exp4/Quizzes.html?domain=Computer%20Science&lab=Data%20Structures)
Easy Problems
1. Write a program to reverse the contents of a singly linked list. Your program cannot
use any additional space beyond O(1) space.
2. Implement the following set operations using linked lists. A set S is represented using
a linked list by having one element of the set in each node. For instance, S = {12, 17,
9, 22} is represented as head->12->17->9->22. The universe is the set of positive
integers. Now, write programs to :
(a) compute the union of two sets S1 and S2. The output is a new list S that
contains elements in S1 U S2.
(b) compute the intersection of two sets S1 and S2. The output is a new list
that contains the elements in S1 interesect S2.
(c) the difference of sets S1 and S2.

3. Given a linked list L with n nodes and a permutation P of the first n integers. Write a
program to output a linked list L' so that the i^th element in L is at position P(i) in L'.
An example is given below. Let L be 1->3->8->5. Let P be (4,1,2,3). Then L' should
be 3->8->5->1. Print output (L') for each test case on seperate lines.
Difficult Problems
1. A sparse matrix is matrix where each row has very few nonzero elements. Therefore,
it is not worthwhile to store a sparse matrix in a two -dimensional array as most of the
array would be unused. There are several ways to represent sparse matrices. One of
them is to use the Compressed Sparse Row (CSR) format where there are 3 arrays,
say data, cols, and ptr. The data array contains all the values of the nonzero elements.
The cols array contains the indices of the columns that contain nonzero elements.
Indices of a row are kept in contiguous positions and in sorted order of column
indices. In the ptr array of size n, ptr[i] contains the index of the first element of
nonzero elements of row i. Further, elements of row i are stored in the cols array
before elements of row i+1. With the above representation, write a program to add
two sparse matrices and multiply two sparse matrices. The output should also be
written in the same format as the input.

2. Read Problem 4 for background on sparse matrices. There are several ways to
represent sparse matrices. One way is to store the nonzero elements of each row in a
linked list. Each element of the list contains a column index and the value of the
corresponding nonzero element. Use the above representation to write programs to
add and multiply two sparse matrices. The output matrix should be represented in the
same format. You are also not allowed to convert from this representation to other
representations.

Search Trees
(http://cse01-
iiith.vlabs.ac.in/exp5/Quizzes.html?domain=Computer%20Science&lab=Data%20Structures)
Easy Problems
1. Modify the operation delete in a binary search tree so that the maximum node in the
right subtree is used as the replacement node.
2. What is the maximum height of a binary search tree on n nodes. What is the minimum
height of a binary search tree on n nodes?
3. What is the maximum height of a binary search tree if every internal node at level l
has both their children before any node at level l+1 have some children?
Difficult Problems
1. Many problems do not require the full repertoire of dictionary operations but only a
subset. Consdier the subset of operations: insert(), and DeleteMin(). In this case, it is
not required to have a binary search tree and we can think of ways to support these
two oeprations efficiently. Here is a possible way.
2. Use the binary tree described in the third question above. Use the invariant that the
value at any node is at most the value of both of its children. In this data structure,
where is the minimum element?
3. Suppose you add one more element to the existing tree above. Where should you first
place it? How do you repair the invaraint if required? What is the runtime of this
operation?
4. Extend your solution above to also the DeleteMin() operation. What is the runtime of
your operation?
5. How can you use your above data structure to sort n data items? What is the runtime
of your solution?
Expression Trees
(http://cse01-
iiith.vlabs.ac.in/exp6/Quizzes.html?domain=Computer%20Science&lab=Data%20Structures)
Easy Questions
1. What do you obtain when doing an inorder, preorder, and postorder traversal of an
expression tree?
2. How can you represent a binary tree in an array? For this, you should map each node
in the binary tree to a unique position in the array. What is the (maximum) size of the
array required to store a binary tree of n nodes.
Difficult Questions
1. Given an inorder traversal sequence, how many different trees can you draw that have
the same inorder traversal?
2. Answer the above question for preorder traversal, and separately for the postorder
traversal.
3. How does your answer to the above question change if you are given both the inorder
traversal and the preorder traversal? How many trees exist that has the given inorder
traversal sequence and the given preorder traversal sequence.
4. Based on your answer to the previous question, write a program that takes as input an
inorder traversal sequence and a preorder traversal sequence, and outputs a binary tree
that has the given inorder and preorder traversal.

S-ar putea să vă placă și