0% found this document useful (0 votes)
22 views29 pages

Programming Challenges and Solutions

The document contains a series of coding questions and tasks for an Accenture drive, focusing on various programming concepts such as array manipulation, string operations, mathematical calculations, and algorithm implementation. Each question specifies input parameters, expected outputs, and sometimes includes examples to illustrate the requirements. The tasks are designed to test problem-solving skills and knowledge of programming languages, particularly Python and C.

Uploaded by

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

Programming Challenges and Solutions

The document contains a series of coding questions and tasks for an Accenture drive, focusing on various programming concepts such as array manipulation, string operations, mathematical calculations, and algorithm implementation. Each question specifies input parameters, expected outputs, and sometimes includes examples to illustrate the requirements. The tasks are designed to test problem-solving skills and knowledge of programming languages, particularly Python and C.

Uploaded by

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

Important Question For Accenture Drive

Q.1 ] 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

Input: Output: 4

 r: 7
 unit: 2
 n: 8
 arr: 2 8 3 5 7 4 1 2

Q.2 ] 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 separated 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.
Q.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 Output : 1


Input 2: a987 abC012 Output : 0

Q.4] 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 Output : 3
Q.4] Implement the following Function

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.

Input Output :90


n:4
m:20

Q.5] Implement the following Function

def ProductSmallestPair(sum, arr)

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

Input :sum:9 size of Arr = 7 Arr:5 2 4 3 9 7 1

Output : 2
Q.6] N-base notation is a system for writing numbers that uses only n different
symbols, This symbols are the first n symbols from the given notation list(Including
the symbol for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7,
8:8, 9:9, 10:A,11:B and so on upto 35:Z) Implement the following function
Char* DectoNBase(int n, int num):

The function accept 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
2. Write the the remainder (in n-base notation)
3. Divide the quotient again by n, Treat the division as integer division
4. Repeat step 2 and 3 until the quotient is 0
5. The n-base value is the sequence of the remainders from last to first

Assumption:
1 < n < = 36

Example

Input
n: 12
num: 718

Output
4BA

Q.7] Implement the following functions.a char*MoveHyphen(char str[],int n);The


function accepts a string “str” of length ‘n’, that contains alphabets and hyphens (-).
Implement the function to move all hyphens(-) in the string to the front of the given
string. NOTE:- Return null if str is null.

 Input:
o [Link]-Hyphens-to-Front
 Output:
o —MoveHyphenstoFront
Q.8] A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding
two numbers from right-to-left one digit at a time

You are required to implement the following function.

Int NumberOfCarries(int num1 , int num2);

The functions accepts 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’.

Assumption: num1, num2>=0

Example:

 Input
oNum 1: 451
oNum 2: 349
 Output
o 2

Q.9] given a function,

Void *ReplaceCharacter(Char str[], int n, char ch1, char ch2);

The function accepts a string ‘ str’ of length n and two characters ‘ch1’ and ‘ch2’ as
its arguments . 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’.

Assumption: String Contains only lower-case alphabetical letters.

 Input:
oStr: apples
och1:a
och2:p
 Output:
o paales
Q.10]You are required to implement the following function.

Int OperationChoices(int c, int n, int a , int b )

The function accepts 3 positive integers ‘a’ , ‘b’ and ‘c ‘ as its arguments. Implement
the function to return.

 ( 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:

 Input
oc :1
oa:12
ob:16
 Output:
o Since ‘c’=1 , (12+16) is performed which is equal to 28 , hence 28 is
returned.

Q.11] 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

1. Loop between ‘a’ and ‘b’. Let the looping variable be ‘i’.
2. Find the exponent (power) of 2 for each ‘i’ and store the number with
maximum exponent of 2 so faqrd in a variable , let say ‘max’. Set ‘max’ to ‘i’
only if ‘i’ has more exponent of 2 than ‘max’.
3. Return ‘max’. Assumption: a <b.

 Input: output : 8
o 7
o 12
Q.11] You are required to implement the following function:

Int Calculate(int m, int n);

The function accepts 2 positive integer ‘m’ and ‘n’ as its [Link] are required
to calculate the sum of numbers divisible both by 3 and 5, between ‘m’ and ‘n’ both
inclusive and return the same.

Input:

m : 12

n : 50

Output

90

Q.11] You are required to input the size of the matrix then the elements of matrix,
then you have to divide the main matrix in two sub matrices (even and odd) in such a
way that element at 0 index will be considered as even and element at 1st index will
be considered as odd and so on. then you have sort the even and odd matrices in
ascending order then print the sum of second largest number from both the matrices

Example

 enter the size of array : 5


 enter element at 0 index : 3
 enter element at 1 index : 4
 enter element at 2 index : 1
 enter element at 3 index : 7
 enter element at 4 index : 9

Sorted even array : 1 3 9


Sorted odd array : 4 7

7
Q.12] Write a program in C to display the table of a number and print the sum of all
the multiples in it.

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

Q.13] Write a program in C such that it takes a lower limit and upper limit as inputs
and print all the intermediate palindrome numbers.

Test Cases:

TestCase 1:
Input :
10 , 80
Expected Result:
11 , 22 , 33 , 44 , 55 , 66 , 77.

Test Case 2:
Input:
100,200
Expected Result:
101 , 111 , 121 , 131 , 141 , 151 , 161 , 171 , 181 , 191.
Q.13] The program is supposed to calculate the sum of distance between three
points from each other.

For
x1 = 1 y1 = 1
x2 = 2 y2 = 4
x3 = 3 y3 = 6

Distance is calculated as : sqrt(x2-x1)2 + (y2-y1)2

Q.14]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

Input:

23 45 82 27 66 12 78 13 71 86

Output:

86

9
Q.15] An Autobiographical Number is a number N such that the first digit of N
represents the count of how many zeroes are there in N, the second digit represents
the count of how many ones are there in N and so on.

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:

 The input string will not be longer than 10 characters.


 Input string will consist of numeric characters.

Input:

n: “1210”

Output:

Q.16.] Execute the given function.

def differenceofSum(n.m)

The function takes two integrals m and n as arguments. You are required to obtain
the total of all integers ranging between 1 to n (both inclusive) which are not divisible
by m. You must also return the distinction between the sum of integers not divisible
by m with the sum of integers divisible by m.

Assumption

m > 0 and n > 0


Important Question Language Wise

Q.1 Sum lies within the integral range

Input:
m=6
n = 30

Output:
285

Integers divisible by 6 are 6, 12, 18, 24, and 30. Their sum is 90.

Integers that are not divisible by 6 are 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17,
19, 20, 21, 22, 23, 25, 26, 27, 28, and 29. Their sum is 375.

The difference between them is 285 (375 – 90).

Q.2. Execute the given function.

def LargeSmallSum(arr)

The function takes an integral arr which is of the size or length of its arguments.
Return the sum of the second smallest element at odd position ‘arr’ and the second
largest element at the even position.

Input:
Arr: 3 2 1 7 5 4

Output:
7
Q.3 The second smallest element at the odd position is 4.

The output is 7 (3 + 4).

Q.4. Write a function to validate if the provided two strings are anagrams or not. If
the two strings are anagrams, then return ‘yes’. Otherwise, return ‘no’.

Input:

Input 1: 1st string


Input 2: 2nd string

Output:
(If they are anagrams, the function will return ‘yes’. Otherwise, it will return ‘no’.)

Example

Input 1: Listen
Input 2: Silent

Output:
Yes

Accenture Coding Questions in Python

Q.4. Perform the following function.

def Productsmallpair(sum,arr)

This function accepts the integers sum and arr. It is used to find the arr(j) and arr(k),
where k ! = j. arr(j) and arr(k) should be the smallest elements in the array. If these
pairs are not found, then return to zero.
Make sure all the values are within the integer range.
Input:
Sum: 9
Arr: 5 4 2 3 9 1 7

Output:
2

Q.5. Perform the function for the given purpose.

For writing numbers, there is a system called N-base notation. This system uses only
N-based symbols. It uses symbols that are listed as the first n symbols. Decimal and
n-based notations are 0:0, 1:1, 2:2, …, 10:A, 11:B, …, 35:Z.

Perform the function: Chats DectoNBase(int n, int num)

This function only uses positive integers. Use a positive integer n and num to find out
the n-base that is equal to num.

Steps

Select a decimal number and divide it by n. Consider this as an integer division.

Denote the remainder as n-based notation.

Again divide the quotient by n.

Repeat the above steps until you get a 0 remainder.

The remainders from last to first are the n-base values.

Assumption
1 < n < = 36
Example

Input:
N: 12
Num: 718

Output:
4BA

Explanation

num Divisor Quotient Remainder

718 12 59 10(A)

59 2 4 11(B)

4 12 0 4(4)

Sample input:
N: 21
Num: 5678

Sample output:
CI8

Q.6. Execute the function for the given purpose.

When the sum of the digits exceeds a total of 9, a carry digit is added to the right-left
of the digit. Execute the function: Int Numberofcarry(Integer num 1, Integer num 2)

Assumption

num1, num2 > = 0


Example

Input:
num1: 451
num2: 349

Output:
2

Explanation

When we add these two numbers from the right to the left, we get two carries. The
value of each of the carries is 1. Hence, the output is the total of these two carries,
i.e., 2.

Sample input:
num1: 23
num2: 563

Sample output:
0

Q.7. The given function has a string (str) and two characters, ch1 and ch2. Execute
the function in such a way that str returns to its original string, and all the events in
ch1 are replaced by ch2, and vice versa.

Replacecharacter(Char str1, Char ch1, Int 1, Char ch2)

Assumption

This string has only alphabets that are in lower case.

Example
Input:
str: apples
ch1: a
ch2: p

Output:
paales

Accenture Coding Questions In Java

Q.8. Perform the function: Int operationchoices(int c, int n, int a, int b). This function
considers three positive inputs of a, b and c.

Execute the function to get:


(a + b), if c = 1
(a / b), if c = 4
(a – b), if c = 2
(a x b), if c = 3

Input:
a: 12
b: 16
c: 1

Output:
28

Explanation
C = 1, hence the function is (a + b). Hence, the output is 28.
Q9. Perform the function Int calculate(int m, int n). This function needs two positive
integers. Calculate the sum of numbers between these two numbers that are
divisible by 3 and 5.

Assumption
m>n>=0

Example

Input:
m: 12
n: 50

Output:
90

Q.10. Execute the function for the given purpose.

Create a matrix and mention the elements in it. Now, divide the main matrix into two
halves in such a way that the elements in index 0 are even, the elements in index 1
are odd, and so on.

Then arrange the values in ascending order for even and odd. After this, calculate
the sum of the second largest numbers from both even and odd matrices.

Example
The size of the array is 5.
Element at 0 index: 3
Element at 1 index: 4
Element at 2 index: 1
Element at 3 index: 7
Element at 4 index: 9
Even array: 1,3,9
Odd array: 4,7

Accenture Coding Questions in C

Q11. The binary number system only uses two digits 1 and 0.

Perform the function: Int OperationsBinarystring(char* str)

Assumptions

Return to –1 if str is null.

The str is odd.


Input:
Str: ICOCICIAOBI

Output:
1

Q.12. Perform the function Checkpassword (char str[], int n)

Execute the function which happens to be 1 if the str is a valid password or it


remains 0.

Conditions for a valid password:

The password should have at least 4 characters.

It should have at least 1 digit.

It should have one capital letter.

It should not have any spaces or obliques (/).


The first character should not be a number.

Assumption
The input is not empty.

Example

Input:
aA1_67

Output:
1

Q.13. Execute this function Void MaxInArray(int arr[], int length)

This function helps in finding the maximum element in the array. Execute this
function to print the maximum element in the array with its index.

Assumptions

The index in the array for all the elements starts at 0.

The maximum element is in a different line in the output.

There has to be only one maximum element.

The function prints only what is required.

Example

Input:
23 45 82 27 66 12 78 13 71 86
Output:
86
9

Explanation
The maximum element is 86 and the array is 9.

Q.14. Change frequent character

The function to execute is


ChatFrequentcharacter(Char str, char x)

This function has a string and a character. This function requires replacing the most
used character in the str with the ‘x’ character.

Note: If two characters have the same frequency, then we will have to consider the
frequency which has the lower ASCII value.

Example

Input:
str: bbadbbababb
char x: t

Output:
ttadttatatt
Q.15. Execute the function Def Autocount(n).

The function accepts the string n. It checks whether the number is an


autobiographical number or not. If an integer returns, then it is an autobiographical
number. If 0 returns, then it is not an autobiographical number.

Assumption

The input value should not be more than 10 characters.

The input string will have numeric characters.

Example

Input:
N: “1210”

Output:
3

Q.16. In a given list of integers, find the number that has the highest frequency. If
there are one or more such numbers, give the smaller one as output.

Input:
3
7
2452324
6
121121
10
1111111111
Output:
2
1
1

Q17. Execute the function for the given purpose.

Write a function mergeArrays which merges two sorted arrays to create one single
sorted array. Complete the function int* mergeArrays(int a[], int b[], int asize, int
bsize) below which takes the pointers to the first element of the two sorted arrays
and the size of the arrays, and returns the base address of the final sorted array.

Input:
4 // Size of 1st array
1 2 3 6 // Elements of 1st array
3 // Size of 2nd array
4 5 7 // Elements of 2nd array

Output:
1
2
3
4
5
6
7
Q.18. Create web access management to the kth largest number. It will accept an
integer k and an array arr as its conditions and has to return the greatest element
based on the value of k. That is, if k = 0, return the greatest element. If k = 1, return
the second greatest element, and so on.

Example

Suppose the array contains values like {74, 85, 102, 99, 101, 56, 84} and the integer
k is 2. The method will return 99, the third greatest element, as there are only two
(according to the value of k) values greater than 99 (101 and 102).

Q.19. We have mentioned a list of integers that have no duplicates. Find how many
swaps it will take to sort the list in ascending order using Bubble sort.

Input:
3
5
21463
10
123 21 34 45 25 675 23 44 55 900
1
23

Output:
3
16
0
Q.20. Write a program to count the number of swaps required to sort a given list of
integers in ascending order using the selection sort algorithm.

Input:
2
3
425
5
10 11 8 7 1

Output:
1
3

Q.21. Form an array of 1000 integers and find out the second-largest number. If
there is no second largest number, return the value to –1.

Example
Input 1:
3
Input 2:
{2,1,2}
Output:
1
Explanation
The integer 1 is the second largest in the array.
Input 1:
5
Input 2:
{4,7,9,8,0} Output:8
Q22. Adam decides to do some charity work. From day 1 till day n, he will give i^2
coins to charity. On day ‘i’ (1 < = i < = n), find the number of coins he gives to charity.

Example 1
Input:
2
Output:
5
Explanation:
There are 2 days.
Example 2

Input:
3

Output:
14

Q23. Perform a function to reverse a string word-wise. The input here will be the
string. In the output, the last word mentioned should come as the first word and vice
versa.

Example
Input:
Welcome to code
Output:
code to Welcome

Explanation
The Reversed string word wise function is applied.
Example
Input:
Code to Crack Puzzle
Output:
Puzzle Crack to Code

Q.24. Find the sum of the divisors for the N integer number.

Example 1
Input:
6
Output:
12

Explanation
Divisors of 6 are 1, 2, 3, and 6. The sum of these numbers is 12.

Example 2
Input:
36
Output:
91

Q.25. Execute a function that accepts the integer array of length ‘size’ and finds out
the maximum number that can be formed by permutation.

Note: You will have to rearrange the numbers to make the maximum number.

Example
Input:
34 79 58 64
Output:
98765443
Explanation
All digits of the array are 3, 4, 7, 9, 5, 8, 6, and 4. The maximum number found after
rearranging all the digits is 98765443.

Q26. Find a string of a length of 1000 for a large number. Output is the modulo of 11.
The output specification is to return the remainder modulo 11 of the input.

Input:
121
Output:
0

Explanation
121 mod 11 = 0

Q27. Find out if the given set of points are on a straight line or not. If the points are
on a straight line, then return the equation. If not, then return 0.

Example
Input:
3
112233
Output:
1x – 1y = 0

Explanation
The three points here are [1,1], [2,2], and [3,3]. These lie on a line, so the function
returned the equation.
Q28. Write a function to find roots of a quadratic equation ax^2 + bx + c = 0.

Note: The formula to find the roots of a quadratic equation is given below:

Example
Input 1: 1
Input 2: –2
Input 3: 3
Output:
{3.0,–1.0}

Explanation:
On substituting the values of a, b, and c in the formula, the roots will be as follows:
+X = 3.0
-X = –1.0

Q29. Write a function to find if the given string is a palindrome or not. Return 1 if the
input string is a palindrome, else return 0.

Input:
level
Output:
1

Explanation:
The reverse of the string ‘level’ is ‘level’. As they are the same, the string is a
palindrome.
Q30. Write a function to check if the given two strings are anagrams or not. Return
‘Yes’ if they are anagrams, otherwise, return ‘No’.

Example
Input 1: build
Input 2: dubli
Output:
Yes

You might also like