Coding Questions
Coding Questions
You are given an integer N denoting farmers of a village. Each person has few farms. It was
found that the number of farms owned by the ith farmers are the bitwise XOR of the farms
owned by i−1th farmer and i. Your task is to find the sum of the farms owned by all the
farmers in the village, 1-based indexed.
Input format:
● The input consists of a single line.
● The line contains an integer N denoting the number of farmers.
Output format:
Print N space-separated numbers, denoting the sum of farms owned by all the farmers. The
output will be the candidate's output printed on the STDOUT.
Example:
Input:
N: 5
Output:
9
Code:
Question 2: Who is the Winner
Elections are conducted in a town that has a population N. All the people of the town cast
votes on the various candidates in town. The people of the town later realized that the votes
of only the candidates 18 and above years of age should be considered. Find out the
candidate who is the winner if only the votes of age group 18 and above are considered. If
the winner cannot be decided or there is a tie, print -1.
You are given an array vote which represents the candidate to whom the vote is given by the
i-th and another array of age which represents the age of that particular voter.
Input Format:
The input consists of three lines:
● The first line will contain one value N, the size of the arrays.
● The second line will contain N space-separated positive integers denoting the array
of the vote.
● The third line will contain N space-separated positive integers denoting the array of
age.
The input will be read from the STDIN by the candidate.
Example:
Input:
N:5
Vote: 1 2 3 1 2 4
Age: 16 18 25 27 22 35
Output:
2 is the Winner as 2 has the maximum votes
Input:
N:6
Vote: 1 2 3 1 2 3
Age: 19 32 25 27 22 35
Output:
-1 as three people have same votes
Code:
Question 3: Count Equal String
Alice gave you a binary string containing only 0 and 1 and asked you to count the unique
subsequences which are Equal String. Alice defines an Equal String as a string which
contains an equal number of 1 and 0. Your task is to print the total number of Equal String
that can be formed using the given binary string.
Input Format:
The input consists of a single line:
● The line contains a string str representing the string to be checked.
Output Format:
Print the count of the unique subsequences which are Equal String.
Constraints:
● 1 ≤ N ≤ 105
● str = {0,1}
Example:
Input:
101011
Output:
6
There is a total of 6 subsequences that are equal strings:
10
01
1010
0101
0011
1001
Code:
Question 4: Adam's Favourite Number
Adam is a big fan of GCD. He is developing a software that takes two integers l and r. The
function of the software is to count the number of pairs of integers (a, b) such that the
following conditions are satisfied:
● GCD(a, b) = a is Adam's favorite number where, 1 ≤ a ≤ b ≤ l ≤ r
● Your task is to implement the above logic for Adam.
Input Format
● The input consists of a single line:
● The first and only line contains two integers l and r.
● The input will be read from the STDIN by the candidate.
Output Format
● Print the number of pairs satisfying the given condition.
● The output will be matched to the candidate's output printed on the STDOUT.
Constraints:
● 1 ≤ l, r ≤ 10^4
Example
Input:
53
Output:
1
Explanation:
There is only a single pair satisfying the condition i.e GCD(3, 3) = 1.
Input:
42
Output:
2
Code:
Question 5: Minimum Cost String
You are given a string str. The string consists of lower case Latin letters (a-z). You have to
convert all the characters of the string such that all the characters become equal and they
would be a vowel. The following characters are vowels: a, e, i, o, u. The cost of changing a
consonant into any vowel is 10 dollars. Let X and Y are two vowels then the cost of
converting X into Y will be |X-Y| dollars. Your task is to find and print the minimum cost to
transform the string into the desired string.
If the string is already equal to the desired string print -1.
Input Format
● The input consists of a single line:
● The line contains a string str.
Output Format
● Print a number that represents the minimum cost to transform the string into the
desired string. If the string is already equal to the desired string then print -1.
Constraints
● 1 ≤ length of the string str ≤ 250
Example
Input:
great
Output:
34
Input:
aaaa
Output:
-1
Code: