Accenture Coding Final1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 75

Accenture_Coding

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.

Note that an integer x divides y if y % x == 0.


Question 4
Examplexample 1:
Input
nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
Output:
2
Explanation: The smallest element in [2,3,2,4,3] is 2, which does not divide all the
elements of numsDivide. We use 2 deletions to delete the elements in nums that are
equal to 2 which makes nums = [3,4,3]. The smallest element in [3,4,3] is 3, which
divides all the elements of numsDivide. It can be shown that 2 is the minimum
number of deletions needed.
Question 4
Examplexample
Input:
nums = [4,3,6], numsDivide = [8,2,6,10]
Output:
-1
Explanation: We want the smallest element in nums to divide all the elements of
numsDivide. There is no way to delete elements from nums to allow this.
Question 5[Acc – 2022]
Find Smallest Character
Problem Statement
You are given a function:
def SmallestCharacter(s):
The function accepts a string ‘s’. Implement the function to find the smallest
English character which is not present in the given string ‘s’ and return the same.
Question 5[Acc – 2022]
Example :
Input :
aidubudxd
Output :
C
Explanation :
Input string contains a and b. So now the smallest character that is not present in
the string is c.
The custom input format for the above case:
aidubndxd
Question 5 [Acc – 2022]
(The line represents a string ‘s’)
Sample Input
bbbb
Sample Output
a
Question 6
Find the word average
Problem Statement
Implement the following function:
Static float Average(String str){}
The function accepts a string ‘str’ of length ‘len’ as its arugment. Implement the
function to calculate the word average and return the same. Word Average is
calculated by finding the average of the ASCII values of all of the letters in a word.
Question 6
Note:
• ‘str’ is not null
• Input string will contain only lower case English alphabets
• The ASCII value of lower case ‘a’ is 97 while that of ‘z’ is 122
• Do not round off your results, it will be automatically rounded off up to 2 decimal
places and then displayed
Example :
Input :
Str: source
Question 6
Output :
109.50
Explanation :
Char value
S 115
o 111
u 117
r 114
c 99
e 101
Question 6
Word Average =(115+111+117+114+99+101)/6=657/6=109.50
Thus Output is 109.50
The custom input format for the above case :
source
(The first line represent the string ‘str’)
Question 6
Sample Input
Str: asp
Sample Output
108.00
The custom input format for the above case:
asp
(The first line represents the string ‘str’).
Question 7 [Acc – 2022]
Problem Statement

Given n pairs of parentheses, write a function to generate all combinations of well-


formed parentheses.
Question 7 [Acc – 2022]
Example 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]

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)

Distance between two numbers is the number of integers between them


Note:If there are two numbers with the least distance from ‘num’ which is divisible
by ‘m’ then return the larger number
Question 16[Acc 2022]
Example:
Input:
Num:67
m:8
Output:
64
Question 16[Acc 2022]
Explanation:
Nearest number greater than 67 which is divisible by 8 is 72.
Nearest number less than 67 which is divisible by 64
Since ,64 is nearest to 67.Thus,output is 64
The custom input format for the above case :
67
8
(The first line represent ‘num’,the second line represent ‘m’
Question 17
Maximum Index Product
Problem Statement
Implement the following function :Int MaximunProduct(int* arr , int n );The
function accepts an integer array ‘arr’ of size n. Implement the function to find and
return maximum index product. For every index ‘j’, index product Left(j) x
Right(j). Left(j) = an integer ‘k’ which is closest to index ‘j’, such that k < j and
arr[k] > arr[j], if no such index ‘k’ exists then Left(j) = 0. Right (j) an index ‘I’
which is closest to index ‘j’, such that i>j and arr[1]> arr[j], if no such index ‘I’
exists then Right(j)=0.
Question 17
Assumption :
Element at index 0 is the smallest.
Note:
Return =1 if the array is empty (or None in the case of python).
Indexing starts from 0.
Question 17
Example:
Input:
arr: -3 4 3 6 4 5 -2
Output:
15
Explanation:
Index product:
Question 17
• Index product of index 0 = 0x1 =0
• Index product of index 1 = 0x3=0
• Index product of index 2 =1x3=3
• Index product of index 3 =0x0=0
• Index product of index 4 = 3x5=15
• Index product of index 5 = 3x0=0
• Index product of index 6 = 5x0 =0
Question 17
The maximum index product in 15. Thus, the output is 15.
Sample Input
arr: 1 5 4 3 5 2
Sample Output
8
Question 18[Acc – 2021]
Lettered numbers
Problem Statement
You are required to implement the following function:
int LetteredNumberSum(char[] str, int len);

The function accepts string ‘str’(‘str1’ in case of Python) as its argument.


Implement the function which returns sum of number equivalents of each letter in
the given string ‘str’.
The number equivalents are as follows:
Question 18[Acc – 2021]
A=1
B = 10
C = 100
D = 1000
E = 10000
F = 100000
G = 1000000
Question 18[Acc – 2021]
Assumption: ‘str’ contains upper case letters only
Note:
Number equivalent for any letter other than (A,B,C,D,E,F,G) is 0
Computed value lies with in integer range
Return 0 if ‘str’ is null (None, in case of Python)

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:

int OperationsBinaryString(char *str);


The function accepts a string 'str' as its argument. The string 'str' consists of binary
digits separated with an alphabet as follows:
'A' denotes AND operation
'B' denotes OR operation
Question 20[Acc – 2022]
'C' denotes XOR operation
You are required to calculate the result of the string 'str', scanning the string left to
right, taking one operation at a time, and return the same.

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

You might also like