Lab Workbook: 17Cs3554 - Competitive Coding Lab
Lab Workbook: 17Cs3554 - Competitive Coding Lab
Lab Workbook: 17Cs3554 - Competitive Coding Lab
STUDENT NAME
REG. NO
YEAR
SEMESTER
SECTION
FACULTY
TABLE OF CONTENTS
2.
3.
4.
5.
6.
7.
8.
9.
Lab Session 01 Arrays, Strings and Functions Using C
Date of Session
Time of Session
:
Pre Lab Questions:
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Sample Input:
s = "aa"
p = "a"
Sample Output: false
Sample Input:
2
4
geeksforgeeks geeks geek geezer
3
apple ape April
Sample Output:
gee
ap
Pre Requisites: Prepare Creating, accessing strings
2. Program Title: Find longest route possible in a Matrix
Problem Description: Given a rectangular path in the form of binary matrix, find the
length of longest possible route from source to destination position of the matrix by
moving to only non-zero adjacent positions i.e route can be formed from positions
having their value as 1. Note there should not be any cycles in the output path.
Pre Requisites: calling functions, Accessing elements row major and column major
ordering from 2-D Matrix
3. Project based program from any online platform
Post Lab Programs:
1. Program Title: Count pairs with given sum
Problem Description Given an array of integers, and a number ‘sum’, find the
number of pairs of integers in the array whose sum is equal to ‘sum’.
Sample Input: arr[] = {1, 5, 7, -1},
sum = 6
Sample Output: 2
Pairs with sum 6 are (1, 5) and (7, -1)
Pre Requisites: Prepare storing, accessing value of Arrays.
2. Program Title: Palindrome Index.
Problem Description: Given a string of lowercase letters in the range ascii[a-z],
determine a character that can be removed to make the string a palindrome. There
may be more than one solution, but any will do. For example, if your string is "bcbc",
you can either remove 'b' at index 0 or 'c' at index 3. If the word is already a
palindrome or there is no solution, return -1. Otherwise, return the index of a
character to remove.
Pre Requisites: Prepare Creating, accessing strings
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 02 OOPs concepts (Classes, Objects, constructors,
Overloading and Inheritance) Using C++
Date of Session
Time of Session
Input Format
Most of the input is handled for you by the locked code in the editor.
In the void Student::input() function, you must read 5 scores from stdin and save them to
your scores instance variable.
Constraints
Output Format
In the int Student::calculateTotalScore() function, you must return the student's total
grade (the sum of the values in scores ).The locked code in the editor will determine
how many scores are larger than Kristen's and print that number to the console.
Sample Input
The first line contains , n the number of students in Kristen's class. The n subsequent lines
contain each student's 5 exam grades for this semester.
3
30 40 45 10 10
40 40 40 10 10
50 20 30 10 10
Sample Output
1
Pre Requisites: Prepare OOPS concepts
3. Project based program from any online platform
Input Format
The first and only line of input contains two space separated integers denoting the width and
height of the rectangle.
Constraints
1 <= width,height <= 10^3
Output Format
The output should consist of exactly two lines:
In the first line, print the width and height of the rectangle separated by space.
In the second line, print the area of the rectangle.
Sample Test Case
Input
5 100
Output
5 100
500
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 03 Polymorphism, Templates and Exception Handling Using
C++.
Date of Session
Time of Session
3. Creating a class Numbers which has two generic type variable x and y; Now create
two objects NUM1 and NUM2 which will accept integer and float type data types.
Program Description: Hari has a problem for you. He will provide you a string consisting of
lowercase letters, uppercase letters and digits and you have to tell if the string is funny or not.
Input Format
3
Aeixy246
ExIOz8y02
asdxyzERTYUI
Output
NOT FUNNY
FUNNY
NOT FUNNY
Pre Requisites: Prepare Templates, Exception Handling
2. Program Title: Report Card
Program Description: The marks, name and grade of the student will be provided and you
have to create classes and work accordingly.
The Student class will have a read() function which will read the name of the student and
marks of 5 subjects. The Student class will have another function read_grade() to read the
grade of the student. The class Percentage will calculate the percentage up to 2 decimal
points. The class Grade will return the grade obtained by the Student. The class Report will
print the name of the student, marks in the 5 subjects, percentage and the grade.
Input Format
The first line of input consist of the number of test cases, T.
The first line of each test case consist of the name of the student.
The second line of each test case consist of the marks of the 5 subjects space separately.
The third line of each test case consist of the grade obtained.
Constraints
1<= |name| <=50
0<= M1, M2, M3, M4 M5 <=100
grade={A, B, C, D}
Output Format
The name of the student is to be printed in the first line.
The marks obtained in the 5 subjects is to printed in the second line.
The percentage and grade is to be printed in the third line space separately.
Sample Test Case
Input
2
Monti
10 20 30 40 50
C
Roy
30 40 50 60 70
C
Output
Monti
10 20 30 40 50
30.00% C
Roy
30 40 50 60 70
50.00% C
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 04 Collections (Stacks, Queues, linked List) using java
Date of Session
Time of Session
Output Format
For each string, return YES or NO.
Sample Input
3
{[()]}
{[(])}
{{[[(())]]}}
Sample Output
YES
NO
YES
Pre Requisites: Prepare stack Operations
In Lab Programs:
1. Program Title: Print the Elements of a Linked List
If you're new to linked lists, this is a great exercise for learning about them. Given a pointer
to the head node of a linked list, print its elements in order, one element per line. If the head
pointer is null (indicating the list is empty), don’t print anything.
Input Format
The first line of input contains n,, the number of elements in the linked list.
The next n, lines contain one element each, which are the elements of the linked list.
Note: Do not read any input from stdin/console. Complete the print Linked List function in
the editor below.
Constraints
Sample Input
2
16
13
Sample Output
16
13
Pre Requisites: Prepare Operations on linked list
2. Program Title: Castle on the Grid
Program Description: You are given a square grid with some cells open (.) and some
blocked (X). Your playing piece can move along any row or column until it reaches the edge
of the grid or a blocked cell. Given a grid, a start and an end position, determine the number
of moves it will take to get to the end position.
For example, you are given a grid with sides n=3 described as follows:
...
.X.
...
Input Format
The first line contains an integer n, the size of the array grid.
Each of the next n lines contains a string of length
.
The last line contains four space-separated integers, startX, startY, goalx, goalY
Sample Input
3
.X.
.X.
...
0002
Sample Output
3
Pre Requisites: Prepare queue operations
3. Project based program from any online platform
Post Lab Programs:
1. Program Title: Steps by Knight [Medium]
Program Description: Given a square chessboard of N x N size, the position of Knight and
position of a target is given.
We need to find out minimum steps a Knight will take to reach the target position.
Input Format
The first line of input contains an integer T denoting the number of test cases. Then T test
cases follow. Each test case contains an integer n denoting the size of the square chessboard.
The next line contains the X-Y coordinates of the knight. The next line contains the X-Y
coordinates of the target.
Constraints
1<=T<=100
1<=N<;=20
1<=knight_pos, targer_pos<=N
Output Format
Print the minimum steps the Knight will take to reach the target position.
Sample Input
2
6
45
11
20
57
15 20
Sample Output
3
9
Pre Requisites: Prepare Queue Operations
2. Program Title: Stamina Game
Program Description: A college in France is organizing a different type of game in which N
players are aligned in a queue. Each player has stamina Si associated with him. At each
minute 4 players from front of the queue are eliminated from the queue and their stamina
decreased by 1 and re-inserted again. If at any point the stamina of player is reduced to zero
then he is removed from the game and not inserted in the queue. Now your task is to print the
stamina of first player in current queue. If anytime Ti < T if number of players in the queue
are less than 4 then then eliminate all the remaining players are re-insert in same manner. If at
time T there are no players left in the game then print -1.
Input Format
First line contain Input N denoting number of players in the game followed by time T at
which position of top player is to be checked.
Second line input contain space separated N integers denoting the stamina of the players in
the queue.
Constraints
1 < N, T < 1000
1 <= Ai <= 10000
Output Format
Output single integer specifying the stamina of first player in current queue.
Sample Input
77
53
49758
Pre Requisites: Prepare Queue Operations
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 05 input streams and output stream Using Java
Date of Session
Time of Session
1. Program Title: Minimum Time required using file input output streams
Problem Description: You are planning production for an order. You have a number of
machines that each have a fixed number of days to produce an item. Given that all the
machines operate simultaneously, determine the minimum number of days to produce the
required order.
For example, you have to produce items. You have three machines that take days to produce
an item. The following is a schedule of items produced:
Sample Input
25
23
Sample Output
6
Explanation
In 6 days can machine 0 produce 3 items and machine 1 can produce 3 items. This totals up
to 5 .
Pre Requisites: Reading and writing data form various input output stream in java
2. Program Title : Boats to Save People
Program Description: The i-th person has weight people[i], and each boat can carry a
maximum weight of limit. Each boat carries at most 2 people at the same time, provided the
sum of the weight of those people is at most limits. Return the minimum number of boats to
carry every given person. (It is guaranteed each person can be carried by a boat.)
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 06 Matrix, Hashing and Bit Manipulation Using java
Date of Session
Time of Session
index Key
0
1 79
2
3
4 69
5 98
6
7 72
8
9
10
11 50
12
3. Applications of hashing?
You are given a matrix with M rows and N columns. Print count of all possible paths
from top left to bottom right of a m*n matrix
Sample Input
2
3
123
456
Output
3
Pre Requisites: Prepare Bit Manipulation Operations
In Lab Programs:
Sample Input
4
5
11100
11100
01010
00111
Sample Output
2$2
2. Program Title: Reverse bits of a given 32 bits unsigned integer.
Problem Description: For Example The input binary string
00000010100101000001111010011100 represents the unsigned integer 43261596, so
return 964176192 which its binary representation is
00111001011110000010100101000000.
Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Pre Requisites: Prepare reading and storing binary numbers.
3. Project based program from any online platform
Post Lab Programs:
1. Program Title: H-index
Problem Description:
Given an array of citations (each citation is a non-negative integer) of a researcher, write a
function to compute the researcher's h-index. According to the definition of h-index on
Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and
the other N − h papers have no more than h citations each."
Input: citations = [3,0,6,1,5]
Output: 3
Explanation: [3,0,6,1,5] means the researcher has 5 papers in total and each of them had
received 3, 0, 6, 1, 5 citations respectively. Since the researcher has 3 papers with at least 3
citations each and the remaining two with no more than 3 citations each, her h-index is 3.
Pre Requisites: Prepare different hashing Techniques
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 07 Binary Trees, Binary search Trees Using java
Date of Session
Time of Session
10
-15 20
- 3 15 50
Sample Output:
Triplet found: (-40,10,50)
Pre Requisites: creation of BST and retrieving its values.
In Lab Programs:
1. Program Title: Print the first shortest root to leaf path in a Binary Tree
Program Description: Given a Binary Tree with distinct values, the task is to print the first
smallest root to leaf path. We basically need to print the leftmost root to leaf path that has the
minimum number of nodes.
Input: Output:1 3 5
1
/ \
2 3
/ /\
4 5 7
/\ \
10 11 8
Input: Output: 10
11
/ \
8 10
/ / \
5 9 8
/\
4 6
2. Program Title: Convert a Binary tree into a Doubly Linked list in Spiral order
Problem Description: Given a binary tree, Convert it into a Doubly linked list in the spiral
order, the conversion should be done in such a way that the left child pointer of a binary
tree node should act as a previous pointer for doubly linked list node and right child
pointer should act as a next pointer for doubly linked list node.
Pre Requisites: binary tree, linked list creation, binary tree values retrieving process.
3. Project based program from any online platform
Post Lab Programs:
1. Program Title: Find Kth smallest and largest element in BST
Problem Description: Given a BST and a positive number K, Find Kth smallest and largest
element in BST.
For example, consider below BST if k=2,then find Kth smallest is 10 and Kth largest is
20 in the given tree.
Output: 10 20
Pre Requisites: creation of BST and retrieving its values.
2. Program Title: Sum and Product of minimum and maximum element of Binary Search Tree.
Problem Description: Given a Binary Search Tree. The task is to find the sum and product
of maximum and minimum value of tree
OUTPUT:
Sum and product of maximum and minimum value of tree is 26 and 88.
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 08 Sorting, List, Tuples, Regular Expressions Using Python
Date of Session
Time of Session
Example 1:
Input: nums = [3, 4, 2]
Output: 6
Explanation:
Delete 4 to earn 4 points, consequently 3 is also deleted.
Then, delete 2 to earn 2 points. 6 total points are earned.
Pre Requisites: Prepare various operations on List and tuples.
In Lab Programs:
1. Program Title: Minimum Window Substring.
Problem Description: Given a string S and a string T, find the minimum window in S
which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC"
Output: "BANC"
Note:
If there is no such window in S that covers all characters in T, return the empty
string "".
If there is such window, you are guaranteed that there will always be only one unique
minimum window in S.
Pre Requisites: Validating string using regular expressions.
2. Program Title: Interleaving String
Program Description: Given s1, s2, s3, find whether s3 is formed by the interleaving
of s1 and s2.
Example 1:
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
Output: true
Pre Requisites: Validating string using regular expressions.
3. Project based program from any online platform
Post Lab Programs:
1. Program Title: Word Break
Program Description: Given a non-empty string s and a dictionary wordDict containing a
list of non-empty words, determine if s can be segmented into a space-separated sequence of
one or more dictionary words.
Note:
The same word in the dictionary may be reused multiple times in the segmentation.
You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Pre Requisites: Validating string using regular expressions.
2. Program Title: Replace all occurrences of 0 that are not surrounded by 1 in a binary Matrix
Program Description: Given a MXN binary matrix, replace all occurrence of 0 by 1 which are not
completely surrounded by 1.
Sample input:
Sample output:
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 09 Puzzles using c/ c++ / java/ python
Date of Session
Time of Session
Each Pokemon can be represented by a unique ID number and its strength is equivalent to
the total number of factors (including 1 and number itself) of its ID number. In a
Pokemon battle, a Pokemon with higher strength will always win a battle against a
Pokemon with lower strength. Let the total number of Pokemon's in Kanto region
be N(their ID ranging from 1 to N). Given a Pokemon ID number K. Find the total
number of Pokemon, the given Pokemon can certainly beat.
Input Format:
First line of input contains space separated T and N, where T denotes the total number of
test cases.
Each test case contains an integer K.
Output Format:
For every test case, output the correct answer in new line.
Constraints:
1 ≤ T ≤ 105
1 ≤ N ≤ 106
1≤K≤N
Sample Input
2 8
3
4
Sample Output
1
5
In Lab Programs:
1. Program Title: Number of wins for each player in a series of Rock-Paper-Scissor game
Two players are playing a series of games of Rock–paper–scissors. There are a total
of K games played. Player 1 has a sequence of moves denoted by string A and similarly
player 2 has string B. If any player reaches the end of their string, they move back to the start
of the string. The task is to count the number of games won by each of the player when
exactly K games are being played.
Input: k = 237, r = 7
Output:1
It is enough for you to buy one cable.
Problem Description: Given integers L, S1 and S2 where L is the length of a circular track
in meters, S1and S2 are the speeds of two persons in kilometers/hour moving in the same
direction on the given track starting from the same starting point. The task is to find the
following:
The time after which they will meet for the first time.
The time at which there are going to meet at the starting point.
Input: L = 30, S1 = 5, S2 = 2
Input: L = 10, S1 = 1, S2 = 2
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 10 Case study- 1
Date of Session
Time of Session
We will solve this problem by first calculating the angles for the hour hand and minute hand
separately. The minute hand is the easiest and that is simply:
Minute hand angle = 6 * minutes because there are 360 degrees total on the clock and there
are a total of 60 minutes, so 360 / 60 = 6 degrees per minute. The hour hand is a bit trickier
because it moves slightly every time the minute hand moves. Therefore, to calculate the angle
of the hour hand we need to take into account how much the minute hand has moved.
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation
Lab Session 11 Case study-2 Snake and Ladder Problem
Date of Session
Time of Session
Problem Description: Given a snake and ladder board, find the minimum number of dice
throws required to reach the destination or last cell from source or 1st cell. Basically, the
player has total control over outcome of dice throw and wants to find out minimum number
of throws required to reach last cell.
If the player reaches a cell which is base of a ladder, the player has to climb up that ladder
and if reaches a cell is mouth of the snake, has to go down to the tail of snake without a dice
throw.
For example, consider the board shown, the minimum number of dice throws required to
reach cell 30 from cell 1 is 3.
Following are the steps:
a) First throw two on dice to reach cell number 3 and then ladder to reach 22
b) Then throw 6 to reach 28.
c) Finally through 2 to reach 30.
There can be other solutions as well like (2, 2, 6), (2, 4, 4), (2, 3, 5).. etc.
The idea is to consider the given snake and ladder board as a directed graph with number of
vertices equal to the number of cells in the board. The problem reduces to finding the shortest
path in a graph. Every vertex of the graph has an edge to next six vertices if next 6 vertices do
not have a snake or ladder. If any of the next six vertices has a snake or ladder, then the edge
from current vertex goes to the top of the ladder or tail of the snake. Since all edges are of
equal weight, we can efficiently find shortest path using Breadth First Search of the graph.
Following is the implementation of the above idea. The input is represented by two things,
first is ‘N’ which is number of cells in the given board, second is an array ‘move[0…N-1]’ of
size N. An entry move[i] is -1 if there is no snake and no ladder from i, otherwise move[i]
contains index of destination cell for the snake or the ladder at i.
Student Signature
Comments of the
Evaluator(if any)
Marks Secured
Name of
Evaluator
Signature of the
Evaluator
Date of
Evaluation