Accenture Coding Questions
Accenture Coding Questions
The following is a compilation of the type of Accenture coding questions that you’d be
encountering.
1. 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
o m > 0 and n > 0
o Sum lies within the integral range
Example
Input:
m=6
n = 30
Output:
285
o Integers divisible by 6 are 6, 12, 18, 24, and 30. Their sum is 90.
o 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.
o The difference between them is 285 (375 – 90).
Sample input:
m=3
n = 10
Sample output:
19
Code
def difference_of_sum(m, n):
def sum_divisible_by_m(num):
# Calculate the sum of integers divisible by m using the formula for the sum of an
arithmetic series
num_of_terms = num // m
return m * (num_of_terms * (num_of_terms + 1)) // 2
def sum_not_divisible_by_m(num):
# Calculate the sum of integers not divisible by m using the formula for the sum of
an arithmetic series
num_of_terms = num // m
return (num * (num + 1)) // 2 - m * (num_of_terms * (num_of_terms + 1)) // 2
sum_divisible = sum_divisible_by_m(n)
sum_not_divisible = sum_not_divisible_by_m(n)
return sum_not_divisible - sum_divisible
# Test cases
print(difference_of_sum(6, 30)) # Output: 285
print(difference_of_sum(3, 10)) # Output: 19
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.
Assumption
o Every array element is unique.
o Array is 0 indexed.
Note:
o If the array is empty, return 0.
o If array length is 3 or <3, return 0.
Example
Input:
Arr: 3 2 1 7 5 4
Output:
7
Explanation
o The second largest element at the even position is 3.
o The second smallest element at the odd position is 4.
o The output is 7 (3 + 4).
Code :
def LargeSmallSum(arr):
if len(arr) < 3:
return 0
# Test case
print(LargeSmallSum([3, 2, 1, 7, 5, 4])) # Output: 7
Second version:
def LargeSmallSum(arr):
if len(arr) < 4:
return 0
for i in range(len(arr)):
if i % 2 == 0: # Even position
if arr[i] > largest_even:
second_largest_even = largest_even
largest_even = arr[i]
elif arr[i] > second_largest_even and arr[i] != largest_even:
second_largest_even = arr[i]
else: # Odd position
if arr[i] < smallest_odd:
second_smallest_odd = smallest_odd
smallest_odd = arr[i]
elif arr[i] < second_smallest_odd and arr[i] != smallest_odd:
second_smallest_odd = arr[i]
# Test case
arr = [3, 2, 1, 7, 5, 4]
print(LargeSmallSum(arr)) # Output: 7
3. 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
Explanation
Listen and Silent are anagrams (an anagram is a word formed by rearranging the letters
of the other word).
Code
# Test cases
print(are_anagrams("Listen", "Silent")) # Output: True (Yes)
print(are_anagrams("hello", "world")) # Output: False (No)
# Test case
input1 = "Listen"
input2 = "Silent"
print(are_anagrams(input1, input2)) # Output: Yes
Also Read About - Difference between argument and parameter
Accenture Coding Questions in Python
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.
Keep this in mind:
o If n<2 or empty, return –1.
o If these pairs are not found, then return to zero.
o Make sure all the values are within the integer range.
Example
Input:
Sum: 9
Arr: 5 4 2 3 9 1 7
Output:
2
Explanation
From the array of integers, we have to select the two smallest numbers, i.e 2 and 1.
Sum of these two (2 + 1) = 3. This is less than 9 (3 < 9). The product of these two is 2 (2
x 1 = 2) Hence the output is integer 2.
Sample input:
Sum: 4
Arr: 9 8 –7 3 9 3
Sample output:
–21
Code :
def Productsmallpair(sum, arr):
if len(arr) < 2:
return -1
# Iterate through the array and find the two smallest elements
for num in arr:
if num < smallest1:
smallest2 = smallest1
smallest1 = num
elif num < smallest2:
smallest2 = num
# Check if there are two distinct smallest elements whose sum equals the given sum
if smallest1 + smallest2 <= sum:
return smallest1 * smallest2
else:
return 0
# Test cases
print(Productsmallpair(9, [5, 4, 2, 3, 9, 1, 7])) # Output: 2
print(Productsmallpair(4, [9, 8, -7, 3, 9, 3])) # Output: -21
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
o Select a decimal number and divide it by n. Consider this as an integer
division.
o Denote the remainder as n-based notation.
o Again divide the quotient by n.
o Repeat the above steps until you get a 0 remainder.
o 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
Code :
def Chats_DectoNBase(n, num):
if n < 2 or n > 36:
raise ValueError("Base 'n' must be between 2 and 36.")
symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n_base = ""
while num > 0:
remainder = num % n
n_base = symbols[remainder] + n_base
num //= n
return n_base
# Test cases
print(Chats_DectoNBase(12, 718)) # Output: "4BA"
print(Chats_DectoNBase(21, 5678)) # Output: "CI8"
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
Code :
def Numberofcarry(num1, num2):
carry = carry_count = 0
return carry_count
# Test cases
print(Numberofcarry(451, 349))
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
Explanation
All the ‘a’ in the string is replaced with ‘p’. And all the ‘p’s are replaced with ‘a’.
def Replacecharacter(str1, ch1, ch2):
# Replace ch1 with a temporary character not present in the string
temp_char = chr(127) # Using a special character with ASCII code 127
return str1
# Test case
str_input = "apples"
ch1_input = "a"
ch2_input = "p"
output = Replacecharacter(str_input, ch1_input, ch2_input)
print(output) # Output: "paales"
Accenture Coding Questions In Java
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
Example:
Input:
a: 12
b: 16
c: 1
Output:
28
Explanation
C = 1, hence the function is (a + b). Hence, the output is 28.
Sample input:
a: 16
b: 20
c: 2
Sample output:
–4
# Find the first number divisible by 3 and 5 within the range [n, m]
first_divisible = ((n + 14) // 15) * 15
# Find the last number divisible by 3 and 5 within the range [n, m]
last_divisible = (m // 15) * 15
return sum_divisible
# Test case 1
m1 = 12
n1 = 50
output1 = calculate(m1, n1)
print(output1) # Output: 90
# Test case 2
m2 = 100
n2 = 160
output2 = calculate(m2, n2)
print(output2) # Output: 405
# Calculate the sum of the second largest elements from both matrices
sum_second_largest = even_matrix[-2] + odd_matrix[-2]
return even_matrix, odd_matrix, sum_second_largest
Example:
Input:
Str: ICOCICIAOBI
Output:
1
Explanation
The input when expanded is “1 XOR 0 XOR 1 XOR 1 XOR 1 AND 0 OR 1”. The result
becomes 1 and hence the output is 1.
Assumption
The input is not empty.
Example
Input:
aA1_67
Output:
1
Code:
def Checkpassword(password):
# Check if the password has at least 4 characters
if len(password) < 4:
return 0
has_digit = False
has_capital_letter = False
has_space_or_oblique = False
has_digit = False
has_capital_letter = False
has_space_or_oblique = False
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.
# Find the most frequent character (considering lower ASCII value if frequencies are
equal)
max_freq = 0
max_freq_char = None
for ch, freq in char_count.items():
if freq > max_freq or (freq == max_freq and ord(ch) < ord(max_freq_char)):
max_freq = freq
max_freq_char = ch
# Replace all occurrences of the most frequent character with the provided character
'x'
result = ''
for ch in s:
if ch == max_freq_char:
result += x
else:
result += ch
return result
Example
Input:
N: “1210”
Output:
3
Explanation
The 0th position has the number 1, the 1st position has the number 2, the 2nd position
has the number 1, and the 3rd position has number 0. Hence, it is an autobiographical
number.
The count of autobiographical numbers in the input is 3, hence 3 is returned.
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.
Example
Input 1:
5
Input 2:
{4,7,9,8,0}
Output:
8
22. 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
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
27. 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.
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
Problem Description
Given a range [A, B], find sum of integers divisible by 7 in this range.
Problem Constraints
1 <= A <= B <= 109
Input Format
First argument is an integer A.
Second argument is an integer B.
Output Format
Return an integer.
Example Input
Input 1:
A = 1
B = 7
Input 2:
A = 99
B = 115
Example Output
Output 1:
7
Output 2:
217
Example Explanation
Explanation 1:
Integers divisible by 7 in given range are {7}.
Explanation 2:
Integers divisible by 7 in given range are {105, 112}.
Problem Description
Problem Constraints
1 <= |A| <= 105
1 <= |B| <= 105
Input Format
First argument is a string A.
First argument is a string B.
Output Format
Return an integer.
Example Input
Input 1:
A = 2
B = 10
Input 2:
A = 11
B = 11
Example Output
Output 1:
4
Output 2:
1
Example Explanation
Explanation 1:
210 = 1024, hence last digit is 4.
Explanation 2:
1111 = 285311670611, hence last digit is 1.
Problem Description
Find the number of integers in range [A, B] with last digit C.
Problem Constraints
1 <= A <= B <= 109
0 <= C <= 9
Input Format
Given three integers A, B and C.
Output Format
Return an integer.
Example Input
Input 1:
A = 11, B = 111, C = 1
Input 2:
A = 1, B = 9, C = 0
Example Output
Output 1:
11
Output 2:
Example Explanation
Explanation 1:
The integers are 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111
Explanation 2:
Problem Constraints
1 <= |A|, |B| <= 105
1 <= Ai, Bi, C <=109
Input Format
First argument is an integer array A.
Second argument is an integer array B.
Third argument is an integer C.
Output Format
Return an integer.
Example Input
Input 1:
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
C = 4
Input 2:
Example Output
Output 1:
0
Output 2:
Example Explanation
Explanation 1:
There are no integers greater than C in A.
There are no integers less than C in B.
Explanation 2:
Integers greater than C in A are [100].
Integers less than C in A are [9, 9, 9].
Problem Description
Problem Constraints
1 <= A <= 105
1 <= B <= 20
Input Format
Given two integers A and B.
Output Format
Return an integer, the number of possible sequences modulo 109+7.
Example Input
Input 1:
A = 4, B = 2
Input 2:
A = 4, B = 3
Example Output
Output 1:
4
Output 2:
Example Explanation
Explanation 1:
The possible sequences are {1, 2}, {1, 3}, {1, 4}, {2, 4}.
Explanation 2:
Problem Constraints
1 <= |A| <= 105
Ai = {Lowercase latin alphabets}
Input Format
Given a string A.
Output Format
Return an integer array of length 26.
Array should contain frequency of characters in increasing order of characters.
Example Input
Input 1:
A = "abcdefghijklmnopqrstuvwxyz"
Input 2:
A = "interviewbit"
Example Output
Output 1:
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1}
Output 2:
{0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 1, 1, 0,
0, 0}
Example Explanation
Explanation 1:
Every charcater is present once in the string.
Explanation 2:
Problem Description
Problem Constraints
1 <= Ai <=105
1 <= B×C <= 105
Input Format
First argument is an integer array A.
Second argument is an integer B.
Third argument is an integer C.
Output Format
Return 2D integer array.
Example Input
Input 1:
A = [1, 2, 4, 8]
B = 2
C = 2
Input 2:
A = [1, 2, 3, 4, 5, 6, 7, 8, 9]
B = 3
C = 3
Example Output
Output 1:
[[1, 2],
[8, 4]]
Output 2:
[[1, 2, 3],
[8, 9, 4],
[7, 6, 5]]
Problem Description
Given two integers A and B, where A is the first element of the sequence then find Bth element of the
sequence.
If the kth element of the sequence is X then k+1th element calculated as:
Problem Constraints
1 <= A <= 109
1 <= B <= 105
Input Format
Given two integers A and B.
Output Format
Return an integer.
Example Input
Input 1:
A = 1
B = 3
Input 2:
A = 5
B = 6
Example Output
Output 1:
2
Output 2:
Example Explanation
Explanation 1:
The sequence is as follows 1 -> 4 -> 2
Explanation 2:
Given an integer A.
Find the digital root of A.
Digital root is the repeated sum of digits of untill there is only one digit left.
Problem Constraints
1 <= A <= 109
Input Format
Given an integer A.
Output Format
Return an integer.
Example Input
Input 1:
A = 99
Input 2:
A = 100
Example Output
Output 1:
9
Output 2:
Example Explanation
Explanation 1:
99 -> 9+9 = 18 -> 1+8 = 9
Explanation 2:
Problem Description
Given an integer array A.
Create an array B such that Bi is the product of all elements of A excluding A i.
Since the products can be too large take modulo 109 +7.
Problem Constraints
1 <= |A| <= 105
1 <= Ai <= 109
Input Format
Given an integer array A.
Output Format
Return an integer array.
Example Input
Input 1:
A = [1, 2, 3, 4]
Input 2:
A = [9, 9, 9]
Example Output
Output 1:
[24, 12, 8, 6]
Output 2:
Example Explanation
Explanation 1:
[2×3×4, 1×3×4, 1×2×4, 1×2×3]
Explanation 2: