Coding Questions
-----------------
Programming Languages: C, C++, Java, Python
Question 1: Algebraic Equation
Description:
Today in class Ms.Mary started teaching the concepts of
simple Algebra. To test the student’s understandability
of the day’s session, she planned to give them some
assignment.
She gave algebraic equations with addition and subtraction
and asking the students to find the X value.
Our task is to take the equation given by Ms.Mary as a
string and find the X value and print it.
Constraints:
There are spaces between every number and symbol in the
string.
x may be a negative number.
Sample Input: (Entire value taken as a String)
20 + x = 40
Sample Output:
20
Sample Input: (Entire value taken as a String)
10 - x = -40
Sample Output:
50
----------------------------------------------------------------
Question 2: Alphabet Numbering
Description:
Write a Python program that takes a string and replaces
each letter with its appropriate position in the alphabet.
a=1, b=2, c=3, …. y=25,z=26
Constraint:
• If any of the character given in the string is not a
letter, ignore it.
• Treat the string in lowercase.
Sample Input:
code
Sample Output:
3 15 4 5
Explanation:
Alphabet positions:
a=1, b=2, c=3, …. y=25,z=26
Our String is Code
Alphabet’s positions –
c=3
o=15
d=4
e=5
Hence the output: 3 15 4 5
Input:
code hash
Output:
3 15 4 5 8 1 19 8
Input:
code
Output:
3 15 4 5
----------------------------------------------------------------
Question 3: Count Vowels
Description:
Write a Python program to read a string from the user
and print how many times each vowel has appeared within
the string.
Sample Input:
Python is a very easy language
Sample Output:
{'a': 4, 'e': 3, 'i': 1, 'o': 1, 'u': 1}
Explanation:
String is – Python is a very easy language
Total vowels present is 10
'a': 4
'e': 3
'i': 1
'o': 1
'u': 1
Input: Instacks
Output: {'a': 1, 'e': 0, 'i': 1, 'o': 0, 'u': 0}
Input: crypt
Output: {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
----------------------------------------------------------------
Question 4: Next Alphabet
Description:
Rita is a 1st Grade teacher who has just taught her
students about the English alphabets.
And now she wants to conduct a test to see how many of
her students remember what was taught to them.
So she gave a task – she will give a word and the
students have to every letter in the string to the next
letter.
a will become b
b will be c
c will be d
and so on…
Constraints:
• There will be no z used in the string.
And if the string contains ‘z’ then print -1
• The string must contain only alphabets if any other
character is present then return -1
Sample Input:
hello
Sample Output:
ifmmp
Explanation:
alphabet after h -> i
alphabet after e -> f
alphabet after l -> m
alphabet after l -> m
alphabet after o -> p
Input: HELLO
Output: IFMMP
Input: zoo
Output: -1
Input: Python
Output: Qzuipo
Input: Python9
Output: -1
Input: This is a Sample String
Output: -1
Input: 123
Output: -1
Input: @#$
Output: -1
----------------------------------------------------------------
Question 5: Anagram Words
Description:
Ms.Rita is teaching her students about anagrams.
An anagram is a word or phrase formed by rearranging the
letters of a different word or phrase.
She has given two words to the students and asked
whether it is an anagram or not. Our task is to help the
students identify an anagram.
Constraints: Perform a case insensitive comparison.
Sample Input:
listen
silent
Sample Output:
1
Explanation:
string1 is listen
string2 is silent
both the strings are made of up of the same characters, hence they are called as
anagrams.
Input:
dad
bad
Output:
0
Input:
read
dear
Output:
1
Input:
race
care
Output:
1
Input:
knee
keen
Output:
1
Input:
star
rats
Output:
1
Input:
cosmic
comics
Output:
1
Input:
rotates
toaster
Output:
1
Input:
space
paces
Output:
1
Input:
listen
silent
Output:
1
Input:
dad
bad
Output:
0
----------------------------------------------------------------
Question 6: Josephus Problem
Description:
The Josephus Problem is a mathematical problem in which a circle is made, its
circumference formed of n people. Starting from the person in the 0th position,
each person eliminates the person to their left (the next person in the circle).
The next living person then does the same, and the process is repeated until there
is only one person left alive.
Find the position (index - starting from 0) of the last man standing in a circle of
n people.
Constraint:
If the number of people in the circle is less than 1, return False.
Sample Input: 8
Sample Output: 0
Test Cases:
Input: 12
Output: 8
Input: 0
Output: False
Input: 3
Output: 2
Input: 6
Output: 4
----------------------------------------------------------------
Question 7: Repeating Characters
Description:
Create a function that returns the number of times a character appears in each word
in a sentence.
Constraints:
• Treat upper-case and lower-case characters of the same letter as being identical
• For Example - ‘a’ exists in Anna twice, not once.
Sample Input:
code hash
h
Sample Output:
[0, 2]
Explanation:
Code hash is the string and the character that we have to search for in each word
is ‘h’
The string is comprised of 2 words –
In the 1st word: code – ‘h’ appears 0 times
In the 2nd word: hash – ‘h’ appears 1 time
Hence the output [0, 2]
Input:
HELLO dear
l
Output:
[2, 0]
Private Test Cases:
Input:
code hash and instacks
z
Output:
[0, 0, 0, 0]
Input:
She sells sea shells by the seashore
s
Output:
[1, 2, 1, 2, 0, 0, 2]
Input:
Happy BirthDay Enjoy!
Y
Output:
[1, 1, 1]
Input:
Good
o
Output:
[2]
----------------------------------------------------------------
Question 8: Checking "ss"
Description:
Write a Python program that takes a string as input and check each word within the
string, whether it satisfies the following condition. If yes print 1 or else print
0
Condition: each word within the string must contain
• atleast 2 instances of the letter "s" and it must be together (ss)
or
• Zero instances of the letter "s".
Constraint:
• Treat the whole string as lowercase letters
• Ignore any other characters that are not alphabets
Sample Input:
Good Morning
Sample Output:
1
Explanation:
String Good Morning consist of two words –
1st word: Good – which has no ‘s’
2nd word: Morning – which has no ‘s’
Hence our condition is satisfied (Zero instance of ‘s’)
The output is given as 1 which represents True
Input:
Good MorningSS
Output:
1
Input:
Goods Morning
Output:
0
Input:
Sshe ssselects to eat that apple
Output:
True
Input:
She ssselects to eat that apple
Output:
False
Input:
SSHE SIS A COOL GIRL
Output:
0
Input:
SSHESS IS Good
Output:
0
Input:
SSHESS SSIS Good
Output:
1
----------------------------------------------------------------
Question 9: Shared & Unique
Program Description:
Write a python program that takes two string values as input and returns a list of
three elements, in the following order:
a)Shared letters between two words.
b)Letters unique to word 1
c)Letters unique to word 2
Constraints:
• String values will be considered in lowercase.
• If an element contains no alphabets, return an empty string
• Even with multiple matching character there should be only 1 exist in the unique
group
• Each element should be alphabetically sorted and placed within the list
Sample Input:
sharp
soap
Sample Output:
['aps', 'hr', 'o']
Explanation:
String1 = sharp
String2 = soap
common letters = 'aps'
unique letters in string1 = 'hr'
unique letters in string2 = 'o'
Hence the output = ['aps', 'hr', 'o']
Input:
Happy
Birthday
Output:
['ay', 'Hp', 'Bdhirt']
Input:
board
bored
Output:
['bdor', 'a', 'e']
Input:
match
ham
Output:
['ahm', 'ct', '']
Input:
married
single
Output:
['ei', 'admr', 'glns']
----------------------------------------------------------------
Question 10: Split String
Description:
Write a Python program that takes a string as input and splits a string into an
array of identical clusters.
• Each cluster should only have one unique character
• The resulting array should be in the same order as the input string
• Logic should work with letters, numbers, and characters
Sample Input:
5556677555
Sample Output:
['555', '66', '77', '555']
Explanation:
1st Cluster – 555
2nd Cluster – 66
3rd Cluster – 77
4th Cluster – 555
Input:
56789
Output:
['5', '6', '7', '8', '9']
Input:
555
Output:
['555']
Input:
5556667788
Output:
['555', '666', '77', '88']
Input:
aaabbbaabbab
Output:
['aaa', 'bbb', 'aa', 'bb', 'a', 'b']
Input:
abbbcc88999&&!!!_
Output:
['a', 'bbb', 'cc', '88', '999', '&&', '!!!', '_']
Input:
@#$%!!&&
Output:
['@', '#', '$', '%', '!!', '&&']
----------------------------------------------------------------
Problem 11:
Write a python function, check_double(number) which accepts a whole number and
returns True if it satisfies the given conditions.
The number and its double should have the same number of digits.
Both the numbers should have the same digits ,but in different order.
Otherwise, it should return False.
Example: If the number is 125874 and its double, 251748, contain the same digits,
but in a different order.
Partial Code: Write Your Logic within this. Do not delete any lines
def check_double(number):
pass
#Remove pass and write your logic here
#Provide different values for number and test your program
print(check_double(number))
Test Cases Output
number = 125874 True
number = 123587 False
number = 141131 False
number = 142857 True
number = 1025874 True
number = 1428570 True
----------------------------------------------------------------
Problem 12:
The road transport corporation (RTC) of a city wants to know whether a particular
bus-route is running on profit or loss.
Assume that the following information are given:
Price per litre of fuel = 70
Mileage of the bus in km/litre of fuel = 10
Price(Rs) per ticket = 80
The bus runs on multiple routes having different distance in kms and number of
passengers.
Write a function to calculate and return the profit earned (Rs) in each route.
Return -1 in case of loss.
Test Cases Output:
distance-100, no_of_passengers-10 100.0
distance-100,no_of_passengers-1 -1
distance-25,no_of_passengers-25 1825.0
distance-20,no_of_passengers-50 3860.0
Partial Code: Write Your Logic within this. Do not delete any lines
def calculate(distance,no_of_passengers):
pass
#Remove pass and write your logic here
#Provide different values for distance, no_of_passenger and test your program
distance=20
no_of_passengers=50
print(calculate(distance,no_of_passengers))
----------------------------------------------------------------
Problem 13:
A teacher is in the process of generating few reports based on the marks scored by
the students of her class in a project-based assessment.
Assume that the marks of her 10 students are available in a tuple. The marks are
out of 25.
Write a python program to implement the following functions:
1. find_more_than_average(): Find and return the percentage of students who have
scored more than the average mark of the class.
2. generate_frequency(): Find how many students have scored the same marks. For
example, how many have scored 0, how many have scored 1, how many have scored
3….how many have scored 25. The result should be populated in a list and returned.
3. sort_marks(): Sort the marks in the increasing order from 0 to 25. The sorted
values should be populated in a list and returned.
Sample Input:
list_of_marks = (12,18,25,24,2,5,18,20,20,21)
Expected Output:
Output:
70.0
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2, 1, 0, 0, 1, 1]
[2, 5, 12, 18, 18, 20, 20, 21, 24, 25]
Input: list_of_marks = (25, 25, 3, 0, 0, 0, 25, 25, 0, 1)
Output:
40.0
[4, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4]
[0, 0, 0, 0, 1, 3, 25, 25, 25, 25]
Input: list_of_marks = (24, 24, 24, 24, 24, 24, 24, 25, 24, 24)
Output:
10.0
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1]
[24, 24, 24, 24, 24, 24, 24, 24, 24, 25]
Input: list_of_marks- (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Output:
0.0
[10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Input: list_of_marks-(25, 25, 25, 25, 25, 25, 25, 25, 25, 25)
Output:
0.0
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
[25, 25, 25, 25, 25, 25, 25, 25, 25, 25]
Partial Code: Write Your Logic within this. Do not delete any lines
#Global variable
list_of_marks=(12,18,25,24,2,5,18,20,20,21)
def find_more_than_average():
pass
#Remove pass and write your logic here
def sort_marks():
pass
#Remove pass and write your logic here
def generate_frequency():
pass
#Remove pass and write your logic here
print(find_more_than_average())
print(generate_frequency())
print(sort_marks())
----------------------------------------------------------------
Problem 14:
Consider sample data as follows:
sample_data=range(1,11)
Create two functions: odd() and even()
• he function odd() returns a list of all the odd numbers from sample_data
• The function even() returns a list of all the even numbers from sample_data
Create a function sum_of_numbers() which will accept the sample data and/or a
function.
If a function is not passed, the sum_of_numbers() should return the sum of all the
numbers from sample_data
If a function is passed, the sum_of_numbers() should return the sum of numbers
returned from the function passed.
Test Cases:
Input:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] , even
Output:
136
Explanation:
Sum of [10,12,14,16,18,20,22,24]
Input:
[10, 11, 12, 13, 14, 15, 16, 17], even
Output:
52
Explanation:
Sum of [10,12,14,16]
Input:
[1], even
Output:
0
Explanation:
Sum of []
Input:
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25], odd
Output:
144
Explanation:
Sum of [11, 13, 15, 17, 19, 21, 23, 25]
Partial Code: Write Your Logic within this. Do not delete any lines
#This verification is based on string match.
def sum_of_numbers(list_of_num, filter_func=None):
pass #Remove pass and write the logic here
def even(data):
pass #Remove pass and write the logic here
def odd(data):
pass #Remove pass and write the logic here
sample_data = range(1,11)
print(sum_of_numbers(sample_data,None))
print(sum_of_numbers(sample_data,even))
print(sum_of_numbers(sample_data,odd))
----------------------------------------------------------------