0% found this document useful (1 vote)
34 views

Coding Questions 09-11-2024

Uploaded by

rakeen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
34 views

Coding Questions 09-11-2024

Uploaded by

rakeen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Coding Questions

Question 1: Rat Count House


(Asked in Accenture OnCampus 10 Aug 2022, Slot 1)

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
 n: 8
 arr: 2 8 3 5 7 4 1 2

Output:
4

Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
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.

#code
def calculate (r, unit, arr, n):
if n == 0:
return -1

totalFoodRequired = r * unit
foodTillNow = 0
house = 0

for house in range (n):


foodTillNow += arr[house]
if foodTillNow>=totalFoodRequired:
break
if totalFoodRequired > foodTillNow:
return 0
return house + 1

r = int (input ())


unit = int (input ())
n = int (input ())

arr = list (map (int, input ().split ()))


print (calculate (r, unit, arr, n))

Question 2:

(Asked in Accenture OnCampus 10 Aug 2022, Slot 2)

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

#code
def OperationsBinaryString(str):
a=int(str[0])
i=1
while i< len(str):
if str[i]=='A':
a&=int(str[i+1])
elif str[i]=='B':
a|=int(str[i+1])
else:
a^=int(str[i+1])
i+=2
return a
str=input()
print(OperationsBinaryString(str))
Question 3:
Password Checker

(Asked in Accenture OnCampus 10 Aug 2022, Slot 3)

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:
aA1_67

Input 2:
a987 abC012

Output 1:
1
Output 2:
0
#code

def CheckPassword(s,n):
if n<4:
return 0
if s[0].isdigit():
return 0
cap=0
nu=0
for i in range(n):
if s[i]==' ' or s[i]=='/':
return 0
if s[i]>='A' and s[i]<='Z':
cap+=1
elif s[i].isdigit():
nu+=1
if cap>0 and nu>0:
return 1
else:
return 0

s=input()
a=len(s)
print(CheckPassword(s,a))

You might also like