Accenture Coding Final1
Accenture Coding Final1
Accenture Coding Final1
Topic/Course
Sub-Topic (Example: name of college)
Question 1 [Acc -2021]
Array element which has least difference with a given number
Problem statement
Implement a given function,
Int leastdifference (int arr[], int length, int n) ;
The function accepts an integer array ‘arr’ of size ‘length’ and an integer ‘n’ as its
arguments. Implement the function to find and return the
element of the array having at least absolute difference with the given number ‘n’.
Note: N > = 0
Question 1 [Acc – 2021]
Absolute difference means you have to ignore the sign of the difference.
Example
Input
12
1 2 12 13 15 17 26 30 38 45 64 72
27
Output
26
Explanation
Question 1
Element Absolute difference
1 26
2 25
12 15
13 14
15 12
17 10
26 1
30 3
38 11
45 18
64 37
72 45
Question 2
Problem statement
You are given a string s. You can convert s to a palindrome by adding characters in
front of it.
Return the shortest palindrome you can find by performing this transformation.
Constraints
0 <= s.length <= 5 * 104
s consists of lowercase English letters only.
Question 2
Assumption:
1. String will contain only lowercase English alphabets.
2. Length of the string is greater than equal to 1.
Note:
You have to find the minimum characters required to append at the begin of string
to make it palindrome
Question 2
Example 1:
Input
s = "aacecaaa"
Output
"aaacecaaa"
Example 2:
Input
s = "abcd"
Output
"dcbabcd"
Question 3[Acc – 2021]
Problem statement
Given a string containing just the characters '(' and ')’, return the length of the
longest valid (well-formed) parentheses substring
Constraints:
0 <= s.length <= 3 * 104
s[i] is '(', or ')'
Question 3[Acc – 2021]
Example 1
Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
Example 2
Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
Question 3[Acc – 2021]
Example 3
Input: s = ""
Output: 0
Question 4
Problem statement:
You are given two positive integer arrays nums and numsDivide. You can delete
any number of elements from nums.Return the minimum number of deletions such
that the smallest element in nums divides all the elements
of numsDivide. If this is not possible, return -1.
Example 2:
Input: n = 1
Output: ["()"]
Constraints:
1 <= n <= 8
Question 8
Maximum element and its index
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.
Question 8
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
Question 8
Example
Input
10
23 45 82 27 66 12 78 13 71 86
Output
86
9
Explanation
86 is the maximum element of the array ai index 9.
Question 8
Sample Input:
8
1 9 11 144 6 7 112 95
Sample Output
144
3
Question 9 [Acc – 2021]
Maximum Product
Problem statement
Given two ranges(L1,R1) and(L2,R2).You need to find two numbers a and b,such
that l1≤a≤R1 and L2≤b≤R2 and the product of a and b is maximum.
Input format
The input consists of a single line:
The line contains four space-separated integers denoting L1,L2,R1,R2 respectively.
Question 9 [Acc – 2021]
The input will be read from the STDIN by the candidate.
Output format
Print the maximum product of a and b.
The output will be matched to the candidate’s output printed on the STDOUT.
Constraints:
•- 〖 10 〗 ^3 ≤L1≤R1≤ 〖 10 〗 ^3
•- 〖 10 〗 ^3 ≤L2≤R2≤ 〖 10 〗 ^3
Question 9 [Acc – 2021]
Example:
Input:
1 3 -2 6
Output
18
Explanation:
The ranges are (1,3) and (-2,6). So the maximum product of a,b such that 1<=a<=3
and -2<=b<=6 is (3*6)=18.
Question 10[Acc – 2021]
Number of cards
Problem statement
Arrangement of cards used for building
pyramids are shown in the following image
a pyramid return the number of cards
%1000007. The function accepts an integer
‘n’ as its argument.The integer ‘n’denotes
level of Pyramid. You are required
Question 10[Acc – 2021]
to calculate the number of cards,required to
build a pyramid return the number of cards
%1000007.
Question 10 [Acc – 2021]
Note:
•Return -1, if ‘n’ =0
Assumptions:
•The number of cards required to build a pyramid of level 1 are 2
Input:
n:2
Output:
7
Question 10 [Acc – 2021]
Explanation:
Cards required to build a pyramid of level 1 are 2, adding 1 more level to the
pyramid will require 5 more cards ,thus a total of 7 cards are needed to build a
pyramid of level 2.
The custom input format for the above case:
2
(The line represents ‘n’)
Question 10 [Acc – 2021]
Sample input:
n:3
Sample output:
15
The custom input format for the above case:
3
(The line represents ‘n’)
Question 10 [Acc – 2021]
Instructions:
•This is a template based questions ,Do not write the “main” function.
•Your code is judged by an automated system,do not write any additional
welcome/greeting messages.
•Additional score will be given for writing optimized code both in terms of memory
and execution time
Question 11
DigiOdd
Problem statement
You are provided with a number N. Count and print the number of integers x that
follow the following conditions
•1 ≤ x ≤ N
•X has odd number of digits
Input format
The input consists of a single line
The line contains a single integer denoting N.
Question 11
Input will be read from the STDIN by the candidate.
Output format
Print the count of integers fulfilling the given conditions.
The output will be matched to the candidates output printed on the STDOUT
Constraints:
•1≤ N ≤ 〖 10 〗 ^5
•N is an integer
Question 11
Example:
Input:
15
Output:
9
Explanation:
The integers to be considered for this input are 1 to 15. Among these 1 to 9 have an
odd number of digits
Question 12
Evaluate expression
Problem statement
You are given a function,
Int EvaluateExpression(char* expr);
The function accepts a mathematical expression ‘expr’ as a parameter. Implement
the function to evaluate the given expression.
Assumption:
•You can assume there is no space in between any of the characters of the
expression ‘expr’
•Expression ‘expr’ contains only digits and operators
Question 12
Note:
•Every operation should be integer based e.g: 5/2 should be given 2, not 2.5
•Consider the precedence of operators while evaluating the expressions, precedence
of(‘/’ or ‘*’ ) > precedence of(‘+’ or ‘-’)
•If ‘expr’ has multiple operators of the same precedence then evaluate them from
left to right.
Question 12
Example:
Input:
Expr: 2+3+5*4/2
Output:
15
Explanation:
2+3+5*4/2= 2+3+10=15, hence 15 is the evaluated value.
The custom input format for the above case:
9
2+3+5*4/2
Question 12
(The first line represents the length of the string, the second line represents the
string)
Sample Input
Expr: 22+15-2*7/3
Sample Output
33
The custom input format for the above case:
11
22+15-2*7/3
(The first line represents the length of the string, the second line represents the
string)
Question 13[Acc – 2021]
Factorial
Problem statement
You are given a function:
long long int Factorial(int n, int m);
The function accepts two integers ‘n’ and ‘m’ (where ‘m’ ≤ ‘n’ and ‘n’-’m’ < 10).
Implement the function to return the value of ‘n’/’m’.
Example:
Input:
n:7
Question 13[Acc – 2021]
m: 2
Output:
2520
Explanation:
Factorial of 7 and 2 will be 5040 and 2 respectively.So the result will be 2520.
The custom input format for the above case:
72
(The line represents ‘n’ and ‘m’ respectively)
Question 13[Acc – 2022]
Sample input:
n: 5
m: 2
Sample Output
60
The custom input format for the above case:
52
(The line represents ‘n’ and ‘m’ respectively).
Question 14
Problem statement
You are required to implement the following function:
char* FrequentCharacterReplaced(char*' str, char x);
The function accepts a string 'str' and a character 'x' as its arguments. You are
required to find the most frequent character in string 'str' and replace it with
character 'x' across the string, and return the same.
Note :
If the frequency of two characters are the same, we have to consider the character
with lower ascii value.
Question 14
Example:
Input:
bbadbbababb
t
Output:
ttadttatatt
Explanation:
The most frequent character in string 'str' is 'b', replacing 'b' with 't' will form string
'ttadttatatt', hence 'ttadttatatt' is returned.
Question 15 [Acc – 2021]
problem statement:
Implement of following function:
Int odd Even sum(int num);
The function accepts a non-negative integer ‘num’ as its argument. Implement the
function to find the sum of all odd digits and sum of all digits of ‘num’and return
sum without value is greater
Question 15 [Acc – 2021]
Example:
Input:
98631
Output:
14
Explanation:
•Sum of odd digits = 9+3+1+=13
•Sum of even digits =8+6=14
Since(13<14),thus output is 14
The custom input format for the above case:
Question 15 [Acc – 2021]
98631
(The line represent ‘num’)
Sample input:
274695
Sample output
21
The custom input format for the above case:
274695
Question 16[Acc 2022]
Divisibility check
Implement the following function:
Int NearestInteger(int num, int m);
The function accepts two positive integers ‘num’ and ‘m’ as its argument.
Implement the function to find and return a number which satisfies the following
the condition
1. Number is divisible by ‘m’
2.Number is nearest to ‘num’(have the least distance from num)
Example:
Input:
DCCBAA
Question 18[Acc – 2021]
The custom input format for the above case:
4
GBCE
(The first line represents the length of the string, the second line represents the
string )
Instructions :
This is a template based question, DO NOT write the “main” function.
Your code is judged by an automated system, do not write any additional
welcome/greeting messages.
“Save and Test” only checks for basic test cases, more rigorous cases will be used
to judge your code while scoring.
Question 18[Acc – 2021]
Additional score will be given for writing optimized code both in terms of memory
and execution time.
Question 19
Sum of leaders in an array
Problem Statement
The function accepts a positive integer array ‘arr’ of size ‘n’ as its argument.
Implement the function to find the leaders in the array and return their sum. An
element is a leader in the array if it is greater than all the elements to its right side.
The rightmost element is always a leader.
Question 19
If ‘arr’ is empty or none(in case of python), return -1
Output lies within the integer range.
Example
Input
7
52 66 64 36 45 24 32
Output
207
Explanation
Leaders of array are {66,64,45,32} sum = 66+64+45+32=207. Thus the output is
207
Question 20[Acc – 2022]
Problem Statement:
The binary number system only uses two digits, 0 and 1. Any string that represents
a number in the binary number system can be called a binary string. You are
required to implement the following function:
Note:
No order of priorities of operations is required.
If 'str' is NULL or None(in case of python), return -1
Question 20[Acc – 2022]
Example
Input:
ICOCICIAOBI
Output:
1
Explanation:
The alphabet in 'str' when expanded becomes "1 XOR 0 XOR 1 XOR 1 AND 0 OR
1", the result of the expression becomes 1, hence 1 is returned.
Question 21[Acc – 2022]
Nearest multiple of 10
Implement the following function
int RoundOff(int n);
The function accepts an integer ‘n’ as its argument. Implement the function to round
off the given number ‘n’ to multiple of 10 according to following rules:
If unit digit of ‘n’ is <= 4, then round off ‘n’ to smaller nearest number to ‘n’ which
is multiple of 10
If unit digit of ‘n’ is >= 5, then round off, ‘n’ to larger nearest number to ‘n’ which
is multiple of 10.
Question 21[Acc – 2022]
Return the round off value.
Nearest number: a number is nearest to another number if they have least distance
between them.
Distance between two numbers is the number of integers between them.
Assumption
N>0
Note
If n is a single digit number then return 10
If n is multiple of 10, then return n unchanged
Question 21[Acc – 2022]
Example
Input
n: 138
output
140
Explanation
Since, unit digit of 138 is 8 which is greater than 5. 138 is round off to 140 which is
larger nearest number of 138 and is multiple of 10. Thus, output is 140.
Question 22[Acc – 2022]
Min Max in two arrays
You are required to implement the following function:
int MinMaxInTwoArrays(int arr1[], int arr2[], int len1, int len2, int k);
The function accepts two integer arrays ‘arr1’ of length ‘len1’, ‘arr2’ of length
‘len2’ and an integer ‘k’ as its arguments.
Let the count of numbers greater than k in array ‘arr1’ be ‘x’ and the count of
numbers lesser than ‘k’ in array ‘arr2’ be ‘y’. You are required to return
he maximum of ‘x’ and ‘y’ from the function.
Assumption: ‘len1’, ‘len2’ > 0
THANK YOU