Accenture Coding Questions
Accenture Coding Questions
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.
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.
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:
The function accepts a string str as its argument. The string str consists of
binary digits separated with an alphabet as follows:
– B denotes OR 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.
Output: 1
Output: 0
Question 3: Password Checker (Asked in Accenture OnCampus 10 Aug 2021,
Slot 3)
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
– Starting character must not be a number Assumption: Input string will not
be empty.
Example:
Input: aA1_67
Output: 1
Output: 0
Question 4: (Asked in Accenture OnCampus 11 Aug 2021, Slot 1)
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’.
Example:
Output: 3
def differenceofSum(n. m)
The function 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
Sample Input
n:3
m:10
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 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 as even
NOTE
Example
Input: a
rr:3 2 1 7 5 4
Output: 7
Sample Input
arr:1 8 0 2 3 5 6
Sample Output: 8
Question:7 (Asked in Accenture OnCampus 12 Aug 2021, Slot 1)
The function 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: 2
Sample Input
sum:4
Arr:9 8 3 -7 3 9
The function accepts positive integer n and num Implement the function to
calculate the n-base equivalent of num and return the same as a string
Steps:
1.Divide the decimal number by n, Treat the division as the integer division.
Example: -
Input: str.Move-Hyphens-to-Front
Output: -MoveHyphenstoFront
Sample Input
Output- -StringCompare
Question:10 (Asked in Accenture Offcampus 1 Aug 2021, Slot 2)
Problem Statement:
The functions accept 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’.
Example:
Num 2: 349
Output 2
Sample Input
Num 1: 23
Num 2: 563
Output 0
Question:11 (Asked in Accenture Offcampus 1 Aug 2021, Slot 3)
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’.
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:
ch1:a ch2:p
Output: Paales
Explanation: ‘A’ in original string is replaced with ‘p’ and ‘p’ in original string
is replaced with ‘a’, thus output is paales.
(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:
Sample Input c: 2 a: 16 b: 20
Sample Output: -4
Question:13 (Asked in Accenture Offcampus 2 Aug 2021, Slot 2)
Problem Statement: You are given a function, Int MaxExponents (int a, int b);
You have to find and return the number between ‘a’ and ‘b’ (range inclusive
on both ends) which has the maximum exponent of 2.
The algorithm to find the number with maximum exponent of 2 between the
given range is Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’. Find
the exponent (power) of 2 for each ‘i’ and store the number with maximum
exponent of 2 so far in a variable, let say ‘max’. Set ‘max’ to ‘i’ only if ‘i’ has
more exponent of 2 than ‘max’. Return ‘max’. Assumption: a <b Note: If two
or more numbers in the range have the same exponents of 2, return the
small number.
Example
Input: 7 12
Output: 8
Explanation: Exponents of 2 in: 7-0 8-3 9-0 10-1 11-0 12-2. Hence
maximum exponent of two is of 8.
14. Question:(Asked in Accenture Offcampus 2 Aug 2021, Slot 3)
Example:
Input: m: 12 n: 50
Output :90
Example:
Test Cases:
Test Case 1:
Input: 5
Expected Result Value: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 275
Test Case 2:
Input: 12
Expected Result Value: 12, 24, 36, 48, 60, 72, 84, 96, 108, 120 660
17. 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
Test Case 2:
Input: 100,200
Expected Result: 101, 111, 121, 131, 141, 151, 161, 171, 181, 191.
18. 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
19. Find the maximum value and its index in the array
Problem Statement: You are given a function, void MaxInArray(int arr[], int
length); The function accepts an integer array ‘arr’ of size ‘length’ as its
argument. Implement the function to find the maximum element of the array
and print the maximum element and its index to the standard output
(STDOUT). The maximum element and its index should be printed in separate
lines.
Note: Array index starts with 0 Maximum element and its index should be
separated by a line in the output. Assume there is only 1 maximum element
in the array. Print exactly what is asked, do not print any additional greeting
messages
Example:
Input: 23 45 82 27 66 12 78 13 71 86
Output: 86 9
You are given a function, def FindAutoCount(n): The function accepts string
“n” which is a number and checks whether the number is an
autobiographical number or not.
If it is, an integer is returned, i.e. the count of distinct numbers in ‘n’. If not, it
returns 0.
Assumption:
Example: