0% found this document useful (0 votes)
44 views8 pages

Coding Challenges: Functions and Outputs

The document outlines seven coding questions, each requiring the implementation of specific functions to solve various problems involving integer and array manipulations. The problems include calculating differences of sums based on divisibility, finding second largest elements in arrays, determining food sufficiency for rats, evaluating binary string operations, validating passwords, counting elements based on absolute differences, and finding the greatest difference between two arrays. Each question provides example inputs and outputs to clarify the expected functionality.

Uploaded by

bittu gurrala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views8 pages

Coding Challenges: Functions and Outputs

The document outlines seven coding questions, each requiring the implementation of specific functions to solve various problems involving integer and array manipulations. The problems include calculating differences of sums based on divisibility, finding second largest elements in arrays, determining food sufficiency for rats, evaluating binary string operations, validating passwords, counting elements based on absolute differences, and finding the greatest difference between two arrays. Each question provides example inputs and outputs to clarify the expected functionality.

Uploaded by

bittu gurrala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Coding Question 1

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

Explanation

 Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60

 Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 +


18 + 19 = 150

 Difference 150 – 60 = 90

Sample Input
n:3
m:10
Sample Output
19
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 largest from the odd
position of given ‘arr’.

Assumption:

 All array elements are unique

 Treat the 0th position as even

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

Explanation

 Second largest among even position elements(1 3 5) is 3

 Second largest among odd position element is 4

 Thus output is 3+4 = 7

Sample Input

arr:1 8 0 2 3 5 6

Sample Output

8
Coding Question 3

Problem Description :
The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as
its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each
rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’
house number, where 0 <= i

Note:

 Return -1 if the array is null

 Return 0 if the total amount of food from all houses is not sufficient for all the rats.

 Computed values lie within the integer range.

Example:

Input:

 r: 7

 unit: 2 (each rat need 2 units)

 n: 8

 arr: 2 8 3 5 7 4 1 2 (each element is number of food the house has)

Output:

Explanation:
Total amount of food required for all rats = r * unit

= 7 * 2 = 14. (requried food)

The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4 houses is sufficient
for all the rats. Thus, output is 4.

(4 because, you can have excess food but not least food that required after 2,8,3 count is 3 where as
food requried is 2 + 8 + 3 = 13 still I need on more 1 unit so I consider next so that I can have extra
food but not less that needed that is 14)
Coding Question 4

Problem Description :
The Binary number system only uses two digits, 0 and 1 and number system can be called binary
string. You are required to implement the following function:

int OperationsBinaryString(char* str);

The function accepts a string str as its argument. The string str consists of binary digits eparated with
an alphabet as follows:

 – A denotes AND operation

 – B denotes OR operation

 – C denotes XOR Operation

You are required to calculate the result of the string str, scanning the string to right taking one
opearation at a time, and return the same.

Note:

 No order of priorities of operations is required

 Length of str is odd

 If str is NULL or None (in case of Python), return -1

Input:
str: 1C0C1C1A0B1

Output:
1

Explanation:
The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1 AND 0 OR 1”, result of the
expression becomes 1, hence 1 is returned.

Sample Input:
0C1A1B1C1C1B0A0

Output:
0
Question 5: Password Checker

You are given a function.


int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument. Implement the function which returns 1 if
given string str is valid password else 0.
str is a valid password if it satisfies the below conditions.

 – At least 4 characters

 – At least one numeric digit

 – At Least one Capital Letter

 – Must not have space or slash (/)

 – Starting character must not be a number

Assumption:
Input string will not be empty.

Example:

Input 1: Output 1:
aA1_67 1
Input 2: Output 2:
a987 abC012 0
Question 6:

You are given a function,


int findCount(int arr[], int length, int num, int diff);

The function accepts an integer array ‘arr’, its length and two integer variables ‘num’ and ‘diff’.
Implement this function to find and return the number of elements of ‘arr’ having an absolute
difference of less than or equal to ‘diff’ with ‘num’.
Note: In case there is no element in ‘arr’ whose absolute difference with ‘num’ is less than or equal
to ‘diff’, return -1.

Example:
Input:

 arr: 12 3 14 56 77 13

 num: 13

 diff: 2

Output:
3

Explanation:
Elements of ‘arr’ having absolute difference of less than or equal to ‘diff’ i.e. 2 with ‘num’ i.e. 13 are
12, 13 and 14.
Question 7:

1. A Fine Number

Problem statement
You are given a function
Int FineNumber(int* a, int*b, int n, int m)
Example:
Input: Output:
56 88
12345
10 12 34 2 4 89
Explanation:
Here the greatest difference is between a 1 from array ‘a’ and 89 from array ‘b’
The custom input format for the above case:
Sample input
N:4
M:3
A : 6 7 8 11
B:312
Sample output:
10
The custom input format for the above case :
4 3
6 7 8 11
3 1 2
(the first line represents ‘n’ and ‘m’ , the second line represents the elements of the array ‘a’ and the
third line represents the elements of the array ‘b’.)

You might also like