0% found this document useful (0 votes)
52 views29 pages

Codevita Plan

Uploaded by

Yuva Teja
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)
52 views29 pages

Codevita Plan

Uploaded by

Yuva Teja
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/ 29

TCS Codevita Plan

TCS Codevita Tentative Date: 29th November 2024


Test Plan
Test Number Date

Practice Test 1 27 October 2024

Practice Test 2 3 November 2024

Practice Test 3 10 November 2024


Please register for the practice tests
Practice Test 4 17 November 2024 using this link:
https://tinyurl.com/42wmsyan
Practice Test 5 24 November 2024

Practice Test 6 25 November 2024

Practice Test 7 26 November 2024

Practice Test 8 27 November 2024

Webinar Plan
Webinar
Number Date Webinar Title Webinar Joining Link Timing

24 October Codevita Prep 1: Foundations of


1 2024 Coding – Build Your Basics https://tinyurl.com/w9ryr4jw 6:00 - 7:00 PM

31 October Codevita Prep 2: Algorithmic Thinking


2 2024 – The Key to CodeVita Success https://tinyurl.com/yb5d9vdm 6:00 - 7:00 PM

7 November Codevita Prep 3: Problem-Solving


3 2024 Strategies – Tackle Any Challenge https://tinyurl.com/yffe6u9v 6:00 - 7:00 PM

14 November Codevita Prep 4: Competitive Coding –


4 2024 Tips to Sharpen Your Skills https://tinyurl.com/4nsr6dnf 6:00 - 7:00 PM

21 November Codevita Prep 5: Competitive Coding 2


5 2024 – Mastering the essentials https://tinyurl.com/5cu292wp 6:00 - 7:00 PM

28 November Codevita Prep 6: Final Prep & Mindset


6 2024 – Gear Up for Exam Day https://tinyurl.com/mdnfhjxw 6:00 - 7:00 PM
TCS Codevita Preparation Resources
1. Registering for TCS Codevita - https://tinyurl.com/ys5t8azm

2. Tips and strategies for solving CodeVita problems - https://tinyurl.com/as59shdj

3. Best practices to excel in the TCS Codevita - https://tinyurl.com/2hhperry

4. Codevita Quest Rewind 1 - https://bit.ly/495mSWE

5. Codevita Quest Rewind 2 - https://bit.ly/3FKmPlD

6. Codevita Quest Rewind 3 - http://bit.ly/3QAFtkN

TCS Codevita Previous Years Questions with Solutions


TCS Codevita questions with solutions (24th November)

1. Zero Count

Problem Description

You are given a binary string B of length L which contains K ones and remaining zeros. You are
required to place the K ones in the binary string in such a way that

the longest consecutive zeros have the least length possible. Once such a binary string is constructed,
you are required to print the length of the contiguous block of zeros,

which has the largest length.

Constraints

0 <= K <= L

1 <= L <= 10^6

Input

Single line consisting of two space separated integers denoting L and K.

Output

Print a single integer denoting the length of the longest consecutive zeros as per the problem.

Time Limit (secs)

Examples

Example 1

Input

31

Output
1

Explanation

B is of length 3 and it has 1 one's.

So the possible strings as per the problem are 010, 001, 100.

In the first case, the maximum length of consecutive zeros is 1 whereas in the other two cases it is 2.
Hence the constructed binary string is 010 and the output is 1.

Example 2

Input

33

Output
0

Explanation

B is of length 3 and it has all three one's. There is no block of zeros, hence the output is 0.

Solution:

#include <iostream>

int maxZeros(int L, int K) {

if (K == 0) {

return L;

int zeros = L - K;

int maxLength = zeros / (K + 1);

int remaining = zeros % (K + 1);

if (remaining > 0) {

maxLength += 1;

return maxLength;

int main() {

int L, K;

std::cin >> L >> K;

std::cout << maxZeros(L, K);

return 0;

}
2. Klaara's Fortress

Problem Description

Queen Klaara is the ruler of Fairy Land. Her fortress has several adjacent rooms resembling a matrix-
like structure, and she is renowned for possessing unique diamonds. Kings and Queens of different
Kingdoms will visit her fortress to see the jewellery. The Fortress has one Entrance at the top left
(0,0) and exit at the bottom right (m-1,n-1).
One day a person from neighboring kingdom thought of stealing as much as he can, from the queen
Klaara's jewellery room. The thief thought that he can anonymously enter, rob, and exit the fort. But
at the entrance itself, he was caught and the watch keepers started chasing him from the point
where he entered into the fort. Now from there, he is trying to reach the room having the exit.
Klaara came to know about this and she thought of using the newly bought automatic locking
application. Using this device, she can lock a room from all sides. Some of the rooms are already
locked and no entry/exit into them. Every other room will be having its gates open for left,right,up
and down rooms (if there are adjacent rooms).

The thief is wise and already aware of the fortress structure so he is trying to escape early by
choosing the shortest path, but Klaara is aiming for increasing the path length using this application.
She cannot lock a room if it results in no path between Entrance and exit, since visitors will be
blocked and then klaara will have to make robbery information public. Klaara can use this application
only one time. To move from one room to another room, thief needs 1 minute. The structure of the
fortress is given. "1" represents locked room and "0" represents open room.

Find the max time that thief will take to reach the exit from the entrance of the fortress. considering

klaara can block at most one room.

klaara cannot block the room which result is no path between entrance and exit.

the thief will choose the shortest path to exit.

Constraints

3 <= M, N <=20

Input

First line consists of two integers M,N denoting the number of rows and columns.

Next M lines contain N space separated integers which will represent the structure of the fortress.

Output

Print the time taken to reach the exit by the thief.

Time Limit (secs)

1
Examples

Example 1

Input

55
00000

11100

00001

01101

00000

Output
15

Explanation

If Klaraa locks the room at position (3,3) (0-indexed), then the thief have to take the path - (0,0) ->
(0,1) -> (0,2) -> (0,3) -> (1,3) -> (2,3) -> (2,2) -> (2,1) -> (2,0) -> (3,0) -> (4,0) -> (4,1) -> (4,2) -> (4,3) ->
(4,4) i.e. he will need 15 minutes for exiting the fortress.

Blocking no other cell will result in longest path taking more than 15 minutes between entry and exit

Example 2

Input

55

01000

00000

01001

01100

10110

Output

Explanation

Here max 9 minutes will be taken by the thief to reach the exit. Blocking rooms like (2,2),(1,3) will
result in the same time.

Solution:

#include<bits/stdc++.h>

using namespace std;

int fn(vector<vector<int>>& grid) {

if(grid[0][0]==1)return -1;

int n=grid.size(),m=grid[0].size();

vector<vector<int>>dp(n+1,vector<int>(m+1,-1));
queue<vector<int>>q;

q.push({0,0,1});

dp[0][0]=1;

int dx[]={0,0,1,-1};

int dy[]={1,-1,0,0};

while(q.size()){

vector<int>c=q.front();

q.pop();

int x=c[0],y=c[1];

for(int k=0;k<4;k++){

int nx=x+dx[k],ny=y+dy[k];

if(nx>=0 and nx<n and ny>=0 and ny<m and grid[nx][ny]==0 and dp[nx][ny]==-1){

dp[nx][ny]=c[2]+1;

q.push({nx,ny,c[2]+1});

return dp[n-1][m-1];

}
int main(){

int n,m;

cin>>n>>m;

vector<vector<int>>mat(n,vector<int>(m));

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

cin>>mat[i][j];

int mx=fn(mat);

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if((i==0 and j==0) or (i==n-1 and j==m-1)){

continue;

if(mat[i][j]==0){

mat[i][j]=1;

int ans=fn(mat);

mx=max(ans,mx);

mat[i][j]=0;

}
}

cout<<mx<<endl;

}
3. Place Finder

Problem Description

The Place Finder is a special tool made for finding locations in areas without internet. To know where
you are, you need a group of these devices because they work together. These devices can easily talk
to each other.

Each device can find where other devices are and how far they are. This information is shared among
devices, creating a network. This network helps with communication and figuring out where devices
are in relation to each other. To make the system more accurate, all devices in the network sync up
and point in the same direction.

In a real time scenario , each devices is able to communicate with each other, but it is not able to find
the direction and distances of all devices due to natural obstacles. However for few devices it may be
possible to know their direction and distances. Now Your job is to find how far apart two specific
devices are, in this connected network. Each device has a unique ID for easy communication and
identification.

The calculations required to find distance between devices can be understood from the through the
Examples section

Constraints

1 < N < 16

Angle is measured in degree.

Distance is in meters

Input

First line consists of an Integer 'N' denoting total number of place finder devices

Second line consist of 'N' pairs of space separated integers. Each pair is separated by a colon. First
value of the pair represents the device ID, second value represents the number of devices that can
be found by this device.

Next lines consist of "N" logical sections. Each logical section consists of

Device ID as its First line,

Followed as many number of lines as devices that can be found by this device. Each such lines has
three space separated Integers denoting "Device ID", " Distance" and "Angle".

Last line consists of two space separated integers representing Id of devices between whom we have
to find the distance .

Output

Print the Distance rounded off to two decimal points denoting the distance between the given two
devices

Time Limit (secs)

1
Examples

Example 1

Input

1:2 2:3 3:2 4:3 5:2

290

3 8 270

5 8 330

4 8 270

1 9 180

1 8 90

490

2 8 90

3 9 180

5 8 30

2 8 150

4 8 210

15

Output

16.42

Explanation
The first line describes there are totally five devices

The second line describes that the device "1" is able to find the direction and distance of 2 devices
and device "2" is able to find the direction and distance of 3 devices and so on.
From the third line Individual logical section starts

From the first line of this logical section, we say it is the data from device "1"

From second line of this logical section, device "2" is located at distance of 9 m from the device "1" at
the angle of 0 degree.

From third line of the logical section, device "3" is located at distance of 8 m from the device "1" at
an angle of 270 degree.

By continuing similarly for all the logical sections, we can visualize the network.

Now when we measure the distance between the device "1" and device "5" we get the output as
16.42.

Example 2

Input

1:2 2:3 3:4 4:3 5:2

250

3 5 60

1 5 180

3 5 120

4 5 60

2 5 300

450

5 5 60

1 5 240

2 5 240

3 5 180

5 5 120

3 5 240
4 5 300

15

Output

10.00

Explanation

com.tcs.cv.automata.ei.middleware.DocxToHtmlConverter@46044faa:image3.png

Above diagram depicts the network. The distance between the device "1" and device "5" are 10.

Hence the output 10.00.

Solution:

import math

n = int(input())

Line = input()

Line = Line.split()

no_devices = {}

for ele in Line:

ele = ele.split(':')

ele[0] = int(ele[0])

ele[1] = int(ele[1])

no_devices[ele[0]] = ele[1]

data = {}

for ele in no_devices:

curr = int(input())

de = no_devices[curr]

for i in range(de):
temp = input()

temp = temp.split()

for i in range(3):

temp[i] = int(temp[i])

if curr not in data:

data[curr] = []

data[curr].append(temp)

else:

data[curr].append(temp)

t = input()

t = t.split()

t[0] = int(t[0])
t[1] = int(t[1])

positions = {}

positions[1] = [0, 0]

i=1

def pos(src):

if len(positions) == n:

return

if src not in positions:

return

while len(data[src]) != 0:

temp = data[src][-1]

new_dev = temp[0]

dis = temp[1]

ang = temp[2]

if new_dev not in positions:

x = positions[src][0] + dis * math.cos(math.radians(ang))


y = positions[src][1] + dis * math.sin(math.radians(ang))

positions[new_dev] = [x, y]

pos(new_dev)

data[src].pop()

pos(1)

result = math.sqrt((positions[t[0]][0] - positions[t[1]][0]) ** 2 + (positions[t[0]][1] - positions[t[1]][1])


** 2)

result = format(result, ".2f")

print(result)

4. VIP Cafe

Problem Description

Raj is running the busiest cafe in the city. It is also the most visited cafe by VIPs.

VIPs buys costly Beverages. It gives Raj's Cafe high profit. Raj asked his workers to prefer serving VIPs
first, because VIPs don't like to wait much time. Whenever a person orders something, Raj gives
priority values to the orders and adds it to the queue. The greater the value more important the
order is.

The workers start to serve the orders with high priority in queue ensuring that the VIPs get theirs
orders quickly. However, those orders having low priority have to wait for a long time in
queue,upsetting those people. This reduces the total number of people visiting the cafe and reducing
the profit. But, making first come first serve basis reduces the VIPs visiting the cafe. This also reduces
the profit.

Raj came up with a new idea to keep the profit high. He introduced a new concept called dynamic
priority. The priority of orders changes while in the queue. Raj will maintain a queue of orders with
assigned priority,adding new orders at end. The order with high priority in the queue will be served
first .When an order got served due to its high priority, the priority of orders in the queue before this
will be increased by one. If two orders having same priority ,then the order which was en-queued
first will be served first. This strikes a balance between reducing the waiting time of VIPs and also
serving other people without much delay.

One day, his friend visited the cafe and ordered something. As usual his order got some priority and
got added in the queue. After some time his friend lost his patience and asked when will his order be
served. After that, Raj stopped adding new orders to the queue and started calculating after how
many orders his friend will get served, considering only orders currently in the queue. Given the
queue, can you find after how many orders will Raj's friend get served?

Constraints

2 <= N <= 25
1 <= Priority <= 100

1 <= K <= N

Input

First line contains a Integer 'N' denoting the total orders in cafe which are needed to served.

Second line consist 'N' space separated Integers indicating the priority of orders present in the
queue.

Third line consist of integer 'K' indicating the position of his friend in the queue.

Output

Single Integer denoting after how many orders will Raj's friend get served.

Time Limit (secs)

Examples

Example 1

Input

135246

Output
6

Explanation

There are 6 orders in the queue with priorities 1 3 5 2 4 6 and Raj's friend's order is at the 4th place.
Thus, Raj's friend's order priority is 2. The serving will be as illustrated below:

135246

24635x

35x35x

4xx35x

5xx4xx

xxx4xx

The order 6th will be served first because 6 is the maximum. The priority of orders before it will be
incremented by 1. Next order be 3rd , 2nd and so on. His friend's order will be served last i.e. 6th.

Example 2

Input

14325

3
Output

Explanation

There are 5 orders in the queue with priorities 1 4 3 2 5 and his friend's order is at the 3rd place.
Raj's friend's order priority is 3. The serving will be as illustrated below:

14325

2543x

3x43x

4xx3x

The order 5th will be served first because 5 is the maximum. The priority of orders before it will be
incremented by 1. Next order served will be the 2nd one in the queue. His friend's order
will be served 3rd.

Solution:

#include <stdio.h>

#define maxorders 25

int serveOrders(int orderp[], int totalo, int friendposition)

int served[maxorders] = {0},servedcount,orderindex;

for (servedcount = 0; servedcount < totalo; ++servedcount)

int maxp = -1;

int maxpriorityindex = -1;

for (orderindex = 0; orderindex < totalo; ++orderindex)


{

if (!served[orderindex] && orderp[orderindex] > maxp)

{maxp = orderp[orderindex];

maxpriorityindex = orderindex;}

served[maxpriorityindex] = 1;

for (int i = 0; i < maxpriorityindex; ++i)

if (!served[i])

{orderp[i]++;}

if (maxpriorityindex == friendposition - 1)

{printf("%d\n", servedcount + 1);

break;}

int main()

int totalo,friendposition,orderp[maxorders],i;

scanf("%d", &totalo);

for (i = 0; i < totalo; ++i)

{scanf("%d", &orderp[i]);}

scanf("%d", &friendposition);

serveOrders(orderp, totalo, friendposition);

return 0;

5. Compound Balance

Problem Description

Amir was given an unbalanced chemical compound in his chemistry class, and he was asked to
balance them based on their valency and present them.
The chemical compound can have two distinct elements and he must balance them such that they
reach the equivalent point.

Here the compounds are in the form of upper-case alphabets Ex AB, CS, ZO etc., and their valency is
the sum of their ascii reduced to one digit. These compounds are balanced by generating the
multiple of each of their valences such that the summation of those multiples reaches to an
equivalent point. If any compound cannot be balanced to the equivalent point, then print "Not
Possible" else print all the combinations in descending order of the multiple of valency of the first
element. See example section for better understanding.

Constraints

Compounds formed are between [A-Z].

1<=Equivalent point<=10^5

Input

The first line contains the non-balanced compound.

The second line contains the equivalent point.

Output

Print all the combination of the combinations in descending order of the multiple of valency of the
first element or "Not Possible".

Time Limit (secs)

Examples

Example 1

Input

AZ
100

Output

A41 Z2

A32 Z4

A23 Z6

A14 Z8

A5 Z10

Explanation

First we need to compute the valency of A and Z. Ascii A = 65 and Z = 90.

Hence valency of A = 6 + 5 = 11

1 + 1 = 2.

Similarly, valency of Z = 9 + 0 = 9.

Since, valency of A=2 and valency of Z=9 therefore the possible combination of the compounds is
A41 Z2 that is 2*41 + 9*2 = 100. Similarly, A32 Z4 that is 2*32 + 9*4 = 100 and so on.

Mathematically A50 Z0 is also possible but as A50 Z0 means 50 molecules of A and 0 molecules of Z
it is not really a compound. Hence A50 Z0 is not an output.

Example 2
Input

HZ

100

Output

Not Possible

Explanation

First we need to compute the valency of H and Z. Ascii H = 72 and Z = 90.

Hence valency of H = 7 + 2 = 9.

Similarly, valency of Z = 9 + 0 = 9.

Since, valency of H = 9 and Z = 9, therefore the closest it can get to the equivalent point is by
multiplying 9*5+9*6 = 99 therefore it's not possible to balance

Solution:

#include <iostream>

#include <vector>

using namespace std;

int sum(int n)

if(n % 9 == 0)

return 9;

else
return n % 9;

int main()

string s;

cin >> s;

int z, count = 0;

cin >> z;

std::vector<std::pair<int, int>> solutions;

int a = int(s[0]);

int b = int(s[1]);

a = sum(a);

b = sum(b);

// Calculate all possible integer solutions for 2x + 9y = 100

for(int x = z; x > 0; --x)

if((z - a * x) % b == 0)

int y = (z - a * x) / b;

if(x > 0 && y > 0)

solutions.push_back(std::make_pair(x, y));

count++;

}
if(count > 0)

for (const auto& solution : solutions)

std::cout << s[0] << solution.first << " " <<s[1]<< solution.second << "\n";

else

cout << "Not Possible";

return 0;

6. Best Bubble

Problem Description

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. The problem with bubble sort is its worst case scenario.
When the smallest element is in the last position, then it takes more time to sort in ascending order,
but takes less time to sort in descending order.

An array is called beautiful if all the elements of the array are in either ascending or descending
order. Given an array of numbers, find the minimum swap operations required to make the array
beautiful.

Constraints

0 < N < 1000

0 < Arr[i] < 1000

Input

First line contains of integer N denoting number of elements in the array.

Second line consist of N integers separated by space denoting the elements of the array.

Output

Single integer denoting the least numbers of swap operations required to make the array beautiful.

Time Limit (secs)

Examples
Example 1

Input

45321

Output

Explanation

The number of swaps required to sort the elements in ascending order is 9.

The number of swaps required to sort the elements in descending order is 1.

The best way is to sort in descending order and swaps required is 1.

Example 2

Input

5
45123

Output

Explanation

Ascending order:

Pass/Index a b c d e Comparison Need swap Swap count


Pass 1 4 5 1 2 3 ab No 0

4 5 1 2 3 bc Yes 1

4 1 5 2 3 cd Yes 2

4 1 2 5 3 de Yes 3

Pass 2 4 1 2 3 5 ab Yes 4

1 4 2 3 5 bc Yes 5

1 2 4 3 5 cd Yes 6
1 2 3 4 5 de No 6

Descending order:

Pass/index a b c d e Comparison Need swap Swap count

Pass 1 4 5 1 2 3 ab Yes 1
5 4 1 2 3 bc No 1
5 4 1 2 3 cd Yes 2
5 4 2 1 3 de Yes 3

Pass 2 5 4 2 3 1 ab No 3

5 4 2 3 1 bc No 3

5 4 2 3 1 cd yes 4
5 4 3 2 1 de No 4

The number of swaps required to sort the elements in ascending order is 6.

The number of swaps required to sort the elements in descending order is 4.

The best way is to sort in descending order and swaps required is 4.

Solution:

def merge_sort_and_count_inversions(arr):

if len(arr) <= 1:

return arr, 0

# Split the array into two halves

mid = len(arr) // 2

left_half, left_inversions = merge_sort_and_count_inversions(arr[:mid])

right_half, right_inversions = merge_sort_and_count_inversions(arr[mid:])

# Merge the two sorted halves and count inversions

merged, split_inversions = merge_and_count_inversions(left_half, right_half)

# Combine the inversion counts

total_inversions = left_inversions + right_inversions + split_inversions

return merged, total_inversions

def merge_and_count_inversions(left, right):

merged = []

i = j = inversions = 0
while i < len(left) and j < len(right):

if left[i] <= right[j]:

merged.append(left[i])

i += 1

else:

merged.append(right[j])

inversions += len(left) - i

j += 1

merged.extend(left[i:])

merged.extend(right[j:])

return merged, inversions

def merge_sort_and_count_swaps(arr):

if len(arr) <= 1:

return arr, 0

# Split the array into two halves

mid = len(arr) // 2

left_half, left_swaps = merge_sort_and_count_swaps(arr[:mid])

right_half, right_swaps = merge_sort_and_count_swaps(arr[mid:])

# Merge the two sorted halves and count swaps

merged, split_swaps = merge_and_count_swaps(left_half, right_half)

# Combine the swap counts

total_swaps = left_swaps + right_swaps + split_swaps

return merged, total_swaps


def merge_and_count_swaps(left, right):

merged = []

i = j = swaps = 0

while i < len(left) and j < len(right):

if left[i] < right[j]: # Check for inversion in descending order

merged.append(left[i])

swaps += len(right) - j

i += 1

else:

merged.append(right[j])

j += 1

merged.extend(left[i:])

merged.extend(right[j:])

return merged, swaps

n = int(input())

arr = list(map(int, input().strip().split()))

sorted_arr, inversions = merge_sort_and_count_inversions(arr)

sorted_arr, swaps_descending = merge_sort_and_count_swaps(arr)

if inversions < swaps_descending:

print(inversions)

else:

print(swaps_descending)

7. Supermarket Solution

#include<bits/stdc++.h>

using namespace std;


int main(){

int n,m;

cin>>n>>m;

vector<vector<int>>cust;

for(int i=0;i<n;i++){

int q,p;

cin>>q>>p;

cust.push_back({p,q});

vector<vector<int>>rice;

for(int i=0;i<m;i++){

int q,p;

cin>>q>>p;

rice.push_back({p,q});

sort(cust.begin(),cust.end());

sort(rice.begin(),rice.end());

vector<int>vis(m,0);

int ans=0;

for(int i=0;i<n;i++){

int quantity=-1,ind=-1;

for(int j=0;j<m;j++){

if(!vis[j]){
if(rice[j][0]>cust[i][0])break;

if(rice[j][1]>cust[i][

1]){

if(quantity==-1){

quantity=rice[j][1];

ind=j;

else{

if(quantity>rice[j][1]){

ind=j;

quantity=rice[j][1];

if(ind!=-1){

vis[ind]=1;

ans++;

cout<<ans<<endl;

*****

You might also like