# TCS CodeVita 2014 Problems - Round 2

In our previous post we shared TCS coding contest CodeVita's Round-1 Problems. If you didn't check that, than first go thorough CodeVita 2014 Round-1.

## CodeVita Round-2 Problems

A : Valid Segments 2

Consider text comprised of sentences and sentences comprised of words. Words in a sentence will be space delimited. Text will comprise of two types of words, viz. alpha words and beta words. Alpha words are denoted in the input. All words in the text which are non-alpha words are beta words. Task is to find out the number of valid segment with number of beta words in the given range [lb, ub].

A segment is said to be valid if
- It contains all the K-strings
- It should start and end with any one of the K-strings

Input Format:
First line contains the text. Next line contains K, lb, ub. Next K lines consist of alpha words in the text.

Note:
1. String comparison should be case insensitive.
2. String comparison should be based only on words comprised of alphabets. Non-alphabet characters such as Full Stop ("."), Exclamation Marks ("!") etc. are called as Stop words. Stop words must be removed from sentences before comparison.
3. If more than one segment starts with the same index (i.e. position of the word in the text), then consider the shortest segment. In other words, segments should begin from unique indexes.

Output Format:
Print the number of valid segments with number of beta words in the given range [lb, ub].

Constraints:
1<= total number of words in the text <= 30,000
1<= length of alpha and beta words <= 500
1<=K<=total number of alpha words
1<=lb<=ub<=total number of beta words

Sample Input and Output
SNo.InputOutput
1
The European market crashes on Mondays. Crashes in the European market are quite common.
2 3 4
European
Crashes

1

Explanation:
Valid segments are:
European market crashes
crashes on Mondays. Crashes in the European
Crashes in the European

There is only one valid segment with number of beta words in the range [3, 4], which is segment 2.

B : Close Strings

A is the square matrix of size N. Every entry in the matrix will contain a lower case alphabet. Given Z strings, for each string your task is to find out the closest square-matrix starting from (1, 1). Closeness between the sub-matrix and the string is defined as, number of similar characters between the two. The input is guaranteed to have at least one character that is same in string Zi and matrix A, where Zi is the ith string. The same cell in the sub-matrix of A cannot be counted more than once to match with the character in the S.

Input Format:
First line of input will consist of T Test cases. Each test case will consist of four parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N) where elements are delimited by space.
3. Number of Strings (Z)
4. Z lines each comprising of string S

Output Format:
For every string print the bottom right co-ordinates of the square-matrix closest from (1, 1).

Constraints:
1<=T<=5
1<=N<=500
'a'<=Aij<='z', where Aij is the entry of the matrix at ith row and jth column(1-index based).
1<=Z<=25000
1<=|S|<=500

Sample Input and Output
SNo.InputOutput
1
1
4
a e r d
p l x l
l p x z
c q x o
3
apple
alex
aero

3 3
3 3
4 4

Explanation:
String S: "apple"
Sub matrix: (1, 1) to (3, 3)
a e r
p l x
l p x
Sub matrix contains all characters of string S, hence the output

C : Employee DB Design

Given a list of unique employee ids and their age information, your task is to design a program to support the certain operations. Broadly speaking there are two operations, Insert and Query. You can assume that insert operations are unique on the employee_id column. Query operations are of 3 types. Both insert and query operations are described below.

1. A {employee_id} {age}
Operation will add the employee_id and age.
Syntax: A {employee_id} {age}
Returns: None

2. Q 1 {employee_id}
Query will search for the records whose employee id is {user_employee_id}.
Syntax: Q 1 {employee_id}
Returns: Boolean (True or False)

3. Q 2 {employee_id}
Query will search for the records whose employee id starts with {user_employee_id}.
Syntax: Q 2 {employee_id}
Returns: Total count of the records which matches the search criteria.

4. Q 3 {employee_id} {lb} {ub}
Query will search for the records for which employee id starts with {user_employee_id} and age is in the range (lb, ub).
Syntax: Q 3 {employee_id} {lb} {ub}
Returns: Total count of the records which matches the search criteria.

Input Format:
Every input line consists of either Add (A) or Query (Q) operation. -1 represents the end of input.

Output Format:
Print the total number of resultant records in case of Query operation.

Constraints:
1 <= Total digits in the Employee ID <= 50
20 <= Age <= 100
10 <=lb<= 100, where lb is a multiple of 10.
lb <=ub<= 100, where ub is a multiple of 10.
Total number of add operations <= 40000
Total number of query operations <= 40000

Sample Input and Output
SNo.InputOutput
1
A 112345 20
A 112346 40
A 212347 40
A 212348 23
A 112449 30
Q 1 112345
Q 2 1123
Q 3 112 10 50
-1

True
2
3

NOTE:
Minimum time required for evaluating this program is 24 seconds. Please be patient. Request you to wait up to a few minutes before resubmitting the same solution.

D : Employee Hierarchy

Adam is working in a large hierarchical organization, where every employee, except the CEO, is either a supervisor or a sub-ordinate or both. Every employee will be identified by Employee ID. CEO of the company has 1 as his employee id. Every employee will get the salary (S) in accordance with their level and their performance. Given the data about the all the above mentioned details, your task is to find out the number of sub-ordinates with salary less than Adam.

Note:
1. If an employee appears at two different levels, then consider the level which is near (best) to the CEO.
2. An employee is considered as sub-ordinate, if Adam is a direct or indirect supervisor to the said employee.

Input Format:
Input consists of three parts, viz.
1. First Line contains, number of employees (N), number of supervisor-subordinate relationships (M) and Adam's employee id (A)
2. Next M lines follow, each line contains two employee Ids I, J which represents that I is a supervisor of J.
3. Salaries of N employees, delimited by space

Output Format:
Print the total number of employees who are sub-ordinates to Adam and have salary less than Adam's.

Constraints:
1<=N<=50
1<=M<=1000
1<=I, J<=N
1<=S<=50000

Sample Input and Output
SNo.InputOutput
1
5 5 2
1 2
2 3
2 4
4 5
5 3
1500 2000 1500 2500 1800

2

E : Close Friends

K friends came to the city and they are in search of some nice flats to stay. At most they can spend T Rupees for the accommodation. Finally they came to know about Marika Apartments, where there exist N empty flats, and every flat owner is allowing at-most Pj persons to stay in a jth flat. So finally they have decided to stay in the multiple flats, at the same time they would like to stay in such flats in which every person from the K person group should able to reach the other K-1 friends who are actually staying either in the same flat or in the other flat.

Let Rj define the rent of the jth flat. One of the major problem in the apartment is only few flats are reachable from the other and one can take the advantage of one flat to reach the other in an indirect way. Now having all the above information your task is to help K friends to know how many options are available for them to stay.

Input Format:
Input consists of four parts, viz.
First Line contains, number of friends (K), number of flats (N) and total paying capacity (T)
Next N lines contain N spaced integers, which represents the connectivity between the N flats. The connectivity between the flats are directional in nature and if Jth flat is reachable from Ith flat then it will be marked as 1
Next line contains N spaced integers Pj, each represents the maximum allowed persons in the jth flat.
Last line contains N spaced integers Rj, each represents the rent of the jth flat.

Output Format:
Print the number of options i.e. number of different ways in which K friends can have their space and stay connected too.

Constraints:
1<=K<=100
1<=N<=500
1<=T<=10 ^ 6
1<=Rj<=5000
1<=Pj<=100

Sample Input and Output
SNo.InputOutput
1
5 4 10000
0 0 1 1
1 0 0 0
0 1 0 0
0 0 0 1
5 4 3 2
2000 1500 1500 2500

1

2
13 4 10000
0 0 1 1
1 0 0 0
0 1 0 0
0 0 0 1
5 4 3 2
2000 1500 1500 2500

0

F : Cyclic Palindrome

A string is said to be palindrome, if it reads the same from both the ends. Given a string S, you are allowed to perform cyclic shifts. More formally, you can pick any one character from any end (head or tail) and you can append that character at the other end. For example, if the string is "abc", then if we do a shift using the character at head position then the string becomes "bca". Similarly, if we do the shift using the character at the tail then the input string becomes "cab". Your task is to find out the minimum number of shifts needed to make the given string, a palindrome. In case, we can't convert the string to palindrome then print -1.

Input Format:
First line starts with T i.e. number of test cases, and then T lines will follow each containing a string "S".

Output Format:
Print the minimum number of cyclic shifts for each string if it can be made a palindrome, else -1.

Constraints:
1<=T<=100
1<=|S|<=300, S will contains only lower case alphabets ('a'-'z').

Sample Input and Output
SNo.InputOutput
1
4
abbb
aaabb
aabb
abc

-1
1
1
-1

Explanation:
For Test Case 2 (aaabb):
Shift the character at the tail to the head and the result will be "baaab", which is a palindrome. This is an operation which requires minimum number of shifts to make the given string a palindrome.

For Test Case 3 (aabb):
One way to convert the given string to palindrome is, shift the character at the head to the tail, and the result will be "abba", which is a palindrome. Another way is to shift the character at the tail to the head, and the result will be "baab", which is also a palindrome. Both require only one shift.

Important Note:
Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not exist in gcc

Happy Coding!

#### Leave a Reply

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.