Coding Question 1
write pyhton code : The function def differenceofSum(n. m) accepts two integers n, m as arguments
Find the sum of all numbers in range from 1 to m(both inclusive) that are not divisible by n. Return
difference between sum of integers not divisible by n with sum of numbers divisible by n.
Assumption:
n>0 and m>0
Sum lies between integral range
Example
Input n:4
m:20
Output
90
Coding Question 2
You are required to implement the following Function def LargeSmallSum(arr).
The function accepts an integers arr of size ’length’ as its arguments you are required to return the
sum of second largest largest element from the even positions and second smallest from the odd
position of given ‘arr’.
Assumption:
All array elements are unique
Treat the 0th position a seven
NOTE
Return 0 if array is empty
Return 0, if array length is 3 or less than 3
Example:-
Input
arr:3 2 1 7 5 4
Output
Coding Question 3
Implement the following Function
The function def ProductSmallestPair(sum, arr) accepts an integers sum and an integer array arr of
size n. Implement the function to find the pair, (arr[j], arr[k]) where j!=k, Such that arr[j] and arr[k]
are the least two elements of array (arr[j] + arr[k] <= sum) and return the product of element of this
pair
NOTE
Return -1 if array is empty or if n<2
Return 0, if no such pairs found
All computed values lie within integer range
Example
Input
sum:9
Arr:5 2 4 3 9 7 1
Output
Coding Question 4
N-base notation is a system for writing numbers which uses only n different symbols, This symbols
are the first n symbols from the given notation list(Including the symbol for o) Decimal to n base
notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:A,11:B and so on upto 35:Z)
Implement the following function, Char* DectoNBase(int n, int num):
The function accept positive integer n and num Implement the function to calculate the n-base
equivalent of num and return the same as a string
Steps:
Divide the decimal number by n,Treat the division as the integer division
Write the the remainder (in n-base notation)
Divide the quotient again by n, Treat the division as integer division
Repeat step 2 and 3 until the quotient is 0
The n-base value is the sequence of the remainders from last to first
Assumption:
1 < n < = 36
Example
Input
n: 12
num: 718
Output
4BA
Coding Question 5
Two government agencies Research and Analysis Wing(RAW) and Intelligence Bureau (IB) want to
encrypt their conversations so that they can save themselves from interception by any other
agency so they invent a new cipher.
Every message is encoded to its binary representation. Then it is written down k times, shifted by
0,1,….,k-1 bits. Each of the columns is XORed together to get the final encoded string.
If b = 1001011and k = 4 it looks like so:
1001011 shift 0
01001011 shift 1
001001011 shift 2
0001001011 shift 3
———-
1110101001 <- XORed/encoded string s
Now we have to decode the message. We know that k = 4 . The first digit in s = 1 so our output string
is going to start with 1 . The next two digits are also 1 , so they must have been XORed with 0. We
know the first digit of our 4th shifted string is a 1 as well. Since the 4th digit of s is 0, we XOR that
with our 1 and now know there is a 1 in the 4th position of the original string. Continue with that
logic until the end.
Then the encoded message s and the key k are sent to the Intelligence Bureau(IB).
RAW is using this encoding algorithm and asks IB to implement a decoding algorithm. Can you help
IB implement this?
Input Format
The first line contains two integers n and k , the length of the original decoded string and the
number of shifts.
The second line contains the encoded string s consisting n+k-1 of ones and zeros.
Output Format
Return the decoded message of length n, consisting of ones and zeros.
Sample Input
74
1110100110
Sample Output
1001010
Explanation
1001010
1001010
1001010
1001010
———-
1110100110
Coding Question 6
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two numbers from
right-to-left one digit at a time
You are required to implement the following function, Int NumberOfCarries(int num1 , int num2);
The functions accepts two numbers ‘num1’ and ‘num2’ as its arguments. You are required to
calculate and return the total number of carries generated while adding digits of two numbers
‘num1’ and ‘ num2’.
Assumption: num1, num2>=0
Example:
Input
o Num 1: 451
o Num 2: 349
Output
o 2
Coding Question 7
You are given a function, Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);
The function accepts a string ‘ str’ of length n and two characters ‘ch1’ and ‘ch2’ as its arguments .
Implement the function to modify and return the string ‘ str’ in such a way that all occurrences of
‘ch1’ in original string are replaced by ‘ch2’ and all occurrences of ‘ch2’ in original string are
replaced by ‘ch1’.
Assumption: String Contains only lower-case alphabetical letters.
Note:
Return null if string is null.
If both characters are not present in string or both of them are same , then return the string
unchanged.
Example:
Input:
o Str: apples
o ch1:a
o ch2:p
Output:
o Paales
Coding Question 8
You are required to implement the following function, Int OperationChoices(int c, int n, int a , int b )
The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its arguments. Implement the function to
return.
( a+ b ) , if c=1
( a + b ) , if c=2
( a * b ) , if c=3
(a / b) , if c =4
Assumption :
All operations will result in integer output.
Example:
Input
o c :1
o a:12
o b:16
Output:
o Since ‘c’=1 , (12+16) is performed which is equal to 28 , hence 28 is returned.
Coding Question 9
Problem Statement:
Vaibhav protected his confidential information by encrypting it using a cipher. This Cipher shifts
each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate
back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b
and c.
Original alphabet: abcdef-ghijkl-mnopqr-stuvwx-yz
Alphabet rotated +3: defghi-jklmno-pqrstu-vwxyza-bc
Note: The cipher only encrypts letters; symbols, such as -, remain [Link] Cipher is also
known as Caesar Cipher.
Input Format
The first line contains the integer, n , the length of the unencrypted string.
The second line contains the unencrypted string, s .
The third line contains k, the number of letters to rotate the alphabet by.
Output Format
For each test case, print the encoded string.
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Coding Question 10
You are required to implement the following function, Int Calculate(int m, int n);
The function accepts 2 positive integer ‘m’ and ‘n’ as its [Link] are required to calculate the
sum of numbers divisible both by 3 and 5, between ‘m’ and ‘n’ both inclusive and return the same.
Note:
0 < m <= n
Example
Input:
m : 12
n : 50
Output:
90
Coding Question 11
You are required to input the size of the matrix then the elements of matrix, then you have to divide
the main matrix in two sub matrices (even and odd) in such a way that element at 0 index will be
considered as even and element at 1st index will be considered as odd and so on. then you have sort
the even and odd matrices in ascending order then print the sum of second largest number from
both the matrices
Example:
enter the size of array : 5
enter element at 0 index : 3
enter element at 1 index : 4
enter element at 2 index : 1
enter element at 3 index : 7
enter element at 4 index : 9
Sorted even array : 1 3 9
Sorted odd array : 4 7 10
Coding Question 12
A string is said to be a substring of another string if it can be formed by deleting 0 or more
characters from the other string. Given two strings of equal length, what’s the longest string that
can be constructed such that it is a substring of both?
For example, ABCD and ABDC have two children with maximum length 3, ABC and ABD. They can
be formed by eliminating either the D or C from both strings. Note that we will not consider ABCD
as a common child because we can’t rearrange characters and ABCD ≠ ABDC.
Input Format
There is one line with two space-separated strings,s1 and s2 .
Output Format
Print the length of the longest string s, such that is a child of both s1 and s2.
Sample Input
HARRY
SALLY
Sample Output
Coding Question 13
Instructions: You are required to write the code. You can click on compile and run anytime to check
compilation/execution status. The code should be logically/syntactically correct.
Question: Write a program in C such that it takes a lower limit and upper limit as inputs and print all
the intermediate pallindrome numbers.
Test Cases:
TestCase 1:
Input :
10 , 80
Expected Result:
11 , 22 , 33 , 44 , 55 , 66 , 77.
Test Case 2:
Input:
100,200
Expected Result:
101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191.
Coding Question 14
Instructions: You are required to write the code. You can click on compile & run anytime to check
the compilation/ execution status of the program. The submitted code should be
logically/syntactically correct and pass all the test cases.
Ques: The program is supposed to calculate the distance between three points.
For,
x1 = 1, y1 = 1
x2 = 2 , y2 = 4
x3 = 3, y3 = 6
Distance is calculated as : sqrt(x2-x1)2 + (y2-y1)2
. In a race, each participant's finish time is recorded. You need to find out who finished first and
how long it took them. Additionally, you need to find the average finish time of all participants.
Input Format
A list of floating-point numbers representing finish times.
Output Format
Two floating-point numbers: the first-place time and the average time.
Example
Input: [12.5, 10.0, 11.2, 13.3]
Output: (10.0, 11.5)
2. In a magical forest, each animal has a unique identifier (ID). Some animals have decided to form
pairs based on their IDs. Your task is to find all the unique pairs of animal IDs that can be formed.
Input Format
A list of integers representing animal IDs.
Output Format
A list of unique pairs of animal IDs.
Input: [1, 2, 3]
Output: [(1, 2), (1, 3), (2, 3)]
3. In a small village, there are treasure chests scattered around. Each chest has a certain number of
coins, and some chests have been cursed, causing them to give negative coins. Your task is to find
the maximum sum of coins you can collect from a consecutive series of chests.
Input Format
A list of integers representing the coins in each chest.
Output Format
An integer representing the maximum sum of coins from consecutive chests.
4. The royal baker needs to prepare a special cake. Each ingredient has a specific weight. The baker
wants to know the total weight of the ingredients and the heaviest ingredient used.
Input Format
A list of floating-point numbers representing the weights of the ingredients.
Output Format
Two floating-point numbers: the total weight and the heaviest ingredient.
Example
Input: [1.2, 0.5, 2.3, 1.8]
Output: (5.8, 2.3)
5. In an enchanted garden, flowers bloom in various colors, and each color has a specific point
value. Your task is to calculate the total points based on the flowers picked by a gardener.
Input Format
A list of integers representing the points for each flower.
Output Format
An integer representing the total points earned from the flowers.
Example
Input: [5, 3, 8, 2]
Output: 18
6. A time traveler collects artifacts from different years. Each artifact has a year associated with it.
Your task is to find the range of years from the earliest to the latest artifact collected.
Input Format
A list of integers representing the years.
Output Format
An integer representing the range of years (latest year - earliest year).
Input: [1995, 2001, 1985, 2010]
Output: 25
7. In a mysterious library, each book has a unique ID, and some books are quite old. Your task is to
find the ID of the oldest book and the average ID of all the books.
Input Format
A list of integers representing book IDs.
Output Format
Two integers: the ID of the oldest book and the average ID (rounded down)
Example
Input: [1001, 1002, 999, 1003]
Output: (999, 1001)
8. A dragon hoards treasures in different forms, each with a specific value. Your task is to calculate
the total value of the treasures and find the highest value among them.
Input Format
A list of integers representing the values of the treasures.
Output Format
Two integers: the total value and the highest value.
Example
Input: [200, 500, 1000, 300]
Output: (2000, 1000)
9. A wizard brews a secret potion using various magical ingredients, each represented by a unique
integer ID. Your task is to identify if a specific ingredient ID is used in the potion.
Input Format
A list of integers representing the ingredient IDs and an integer representing the ID to check.
Output Format
A string indicating whether the ingredient ID is used ("Yes" or "No").
Example
Input: [101, 102, 103, 104], 102
Output: Yes
10. A timekeeper has a collection of clocks, each representing a specific hour. Your task is to
calculate the total hours represented by the clocks and find the clock showing the highest hour.
Input Format
A list of integers representing the hours of the clocks.
Output Format
Two integers: the total hours and the highest hour.
Example
Input: [3, 7, 5, 9]
Output: (24, 9)
1) Sum of Binary Digits
Problem Statement:
You are given a number N. Convert the number to its binary form and return the sum of its binary
digits (i.e., the number of 1’s in the binary representation).
Example:
Input: 15
Binary of 15 is 1111, so the sum is 4.
#CODE
def binary_sum(n):
return bin(n).count(‘1’)
n = 15
print(“Sum of binary digits:”, binary_sum(n))
2) Find the Second Smallest Element in an Array
Problem Statement:
Given an array, find the second smallest element in the array.
Example:
Input: [3, 1, 4, 1, 5]
Output: 3
Python Solution:
def second_smallest(arr):
unique_sorted = sorted(set(arr))
return unique_sorted[1] if len(unique_sorted) > 1 else None
arr = [3, 1, 4, 1, 5]
print(“Second smallest element:”, second_smallest(arr))
3) Shorten Word with Middle Character Count
Problem Statement:
Given a word, return a string that contains the first letter, the count of the middle characters, and
the last letter.
Example:
Input: examination
Output: e9n
Python Solution:
def shorten_word(word):
return word[0] + str(len(word[1:-1])) + word[-1]
word = “examination”
print(“Shortened word:”, shorten_word(word))
4) Prime Numbers Between 1 and N
Problem Statement:
Print all prime numbers between 1 and N.
Python Solution:
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def primes_upto_n(n):
return [i for i in range(2, n+1) if is_prime(i)]
n = 50
print(“Prime numbers up to”, n, “:”, primes_upto_n(n))
5) Print Floyd’s Triangle
Problem Statement:
Print Floyd’s triangle with N rows.
Example:
Input: N = 5
Python Solution:
def floyd_triangle(n):
num = 1
for i in range(1, n+1):
for j in range(i):
print(num, end=” “)
num += 1
print()
rows = 5
floyd_triangle(rows)
6) Check Leap Year
Problem Statement:
Write a program to check if a given year is a leap year or not.
Python Solution:
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
return False
year = 2024
print(year, “is a leap year?” , is_leap_year(year))
7) Sum of Even and Odd Elements in an Array
Problem Statement:
Given an array, calculate the sum of even and odd elements separately.
Python Solution:
def sum_even_odd(arr):
even_sum = sum(x for x in arr if x % 2 == 0)
odd_sum = sum(x for x in arr if x % 2 != 0)
return even_sum, odd_sum
arr = [1, 2, 3, 4, 5, 6]
even_sum, odd_sum = sum_even_odd(arr)
print(“Even sum:”, even_sum)
print(“Odd sum:”, odd_sum)
8) Vowel Permutation
Problem Statement:
Given a string, print all possible permutations of the vowels present in the string.
Python Solution:
from itertools import permutations
def vowel_permutations(s):
vowels = [char for char in s if char in ‘aeiou’]
return list(permutations(vowels))
word = “accenture”
print(“Vowel permutations:”, vowel_permutations(word))
9) Reverse the Array and Print Odd and Even Positions
Problem Statement:
Reverse the given array and then print the elements at even and odd positions in a string format.
Python Solution:
def reverse_and_positions(arr):
[Link]()
even_pos = arr[1::2]
odd_pos = arr[0::2]
return “Even positions: ” + str(even_pos) + “, Odd positions: ” + str(odd_pos)
arr = [10, 20, 30, 40, 50]
print(reverse_and_positions(arr))
10) Print the Winning Team of a Football Match
Problem Statement:
Given the number of teams and their goals, find the team with the maximum number of goals.
Python Solution:
def winning_team(teams, goals):
max_goals = max(goals)
winner_index = [Link](max_goals)
return teams[winner_index]
teams = [“Team A”, “Team B”, “Team C”, “Team D”, “Team E”]
goals = [5, 2, 3, 1, 4]
print(“Winning team:”, winning_team(teams, goals))
11) Maximum Chocolates
Problem Statement:
You are given an array of chocolate prices. If the price is divisible by 5, it’s free. Find the maximum
number of chocolates a person can buy.
Python Solution:
def max_chocolates(prices):
free_chocolates = sum(1 for price in prices if price % 5 == 0)
return free_chocolates
chocolate_prices = [10, 15, 20, 25, 7, 9]
print(“Maximum chocolates you can buy:”, max_chocolates(chocolate_prices))