Accenture Coding Round :
In Accenture, there will be two coding questions that you need to solve within 45 minutes. In the Accenture Coding Round, you can write code using the following preferred languages:
- C
- C++
- Java
- Python
- .NET
The difficulty level of the questions is high. You need to practice extensively to achieve a good score in Accenture coding questions. [Accenture Coding Questions and Answers]

Accenture Coding Questions and Answers 2025
Question 1 : Rat Count House
Problem Statement:Â Given the number of rats r
, the amount of food each rat consumes unit
, and an array arr
representing the amount of food in each house, determine the minimum number of houses required to feed all the rats.
Input Format
- The first line contains an integer
r
(number of rats). - The second line contains an integer
unit
(amount of food per rat). - The third line contains an integer
n
(size of the array). - The fourth line contains
n
space-separated integers representing the arrayarr
.
Output
- An integer representing the minimum number of houses required to feed all the rats. If it is not possible to feed all the rats, output
0
.
Example
Input:
5
3
4
2 2 2 2
Output: 0
Explanation :Â
- Total food required =
5 * 3 = 15
. - Total food available =
2 + 2 + 2 + 2 = 8
(insufficient). - Therefore, the output is
0
.
[Accenture Coding Questions and Answers]
Solution :
public class RatCountHouse {
public static int minHouses(int r, int unit, int[] arr) {
if (arr == null) return -1;
int totalFoodRequired = r * unit;
int currentFood = 0;
for (int i = 0; i < arr.length; i++) {
currentFood += arr[i];
if (currentFood >= totalFoodRequired) {
return i + 1;
  }
 }
return 0;
  }
}
//Accenture Coding Questions and Answers
#include <vector>
int minHouses(int r, int unit, std::vector<int> arr) {
if (arr.empty()) return -1;
int totalFoodRequired = r * unit;
int currentFood = 0;
for (size_t i = 0; i < arr.size(); ++i) {
currentFood += arr[i];
if (currentFood >= totalFoodRequired) {
return i + 1;
   }
 }
return 0;
}
def min_houses(r, unit, arr):
if arr is None:
return -1
total_food_required = r * unit
current_food = 0
for i in range(len(arr)):
current_food += arr[i]
if current_food >= total_food_required:
return i + 1
return 0
Question 2 : Counting 1s in Binary: Alex and Carrie's Game
Problem Statement: Alex and Carrie are playing a binary game. In this game, Alex gives Carrie a number, and Carrie’s task is to start from 0 and incrementally go up to the given number N.
For each number in this range, Carrie must count the number of 1s in its binary representation.
Question Constraints: 1 <= N<= 5*10^6
Input Format
- The input consists of a single character representing the number N.
Output
- Print a vector where each element is the count of 1s in the binary representation of numbers from 0 up to N.
[Accenture Coding Questions and Answers]
Solution :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // Number of elements
int ans = 0; // Result
// Process each element
while (n– > 0) {
ans ^= sc.nextInt(); // Perform XOR directly
}
System.out.println(ans); // Output the result
}
}
#include <iostream>
using namespace std;
int main() {
int n, ans = 0;
cin >> n; // Number of elements
// Process each element
while (n–) {
int k;
cin >> k;
ans ^= k; // XOR the result
}
cout << ans << endl; // Output the result
return 0;
}
# Input the number of elements
n = int(input())
ans = 0 # Result
# Process each element
for _ in range(n):
k = int(input())
ans ^= k # Perform XOR
print(ans) # Output the result
#Accenture Coding Questions and Answers
Question 3 : Difference of Sums: Divisible vs. Non-Divisible Numbers
Problem Statement: The function def differenceofSum(n. m) 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
- Sum of numbers divisible by 4 are 4 + 8 + 12 + 16 + 20 = 60
- Sum of numbers not divisible by 4 are 1 +2 + 3 + 5 + 6 + 7 + 9 + 10 + 11 + 13 + 14 + 15 + 17 + 18 + 19 = 150
- Difference 150 – 60 = 90
Sample Input :Â
n:3
m:10
Sample Output :Â
19
[Accenture Coding Questions and Answers]
Solution :
import java.util.Scanner;
class Solution {
public static int differenceOfSum(int m, int n) {
int sumAll = m * (m + 1) / 2; // Sum of all numbers from 1 to m
int countDivisible = m / n; // Count of numbers divisible by n
int sumDivisible = n * (countDivisible * (countDivisible + 1)) / 2; // Sum of divisible numbers
int sumNotDivisible = sumAll – sumDivisible; // Sum of numbers not divisible by n
return Math.abs(sumNotDivisible – sumDivisible); // Absolute difference
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
System.out.println(differenceOfSum(m, n));
}
}
#include <iostream>
using namespace std;
int differenceOfSum(int m, int n) {
int sumAll = m * (m + 1) / 2; // Sum of all numbers from 1 to m
int countDivisible = m / n; // Count of numbers divisible by n
int sumDivisible = n * (countDivisible * (countDivisible + 1)) / 2; // Sum of divisible numbers
int sumNotDivisible = sumAll – sumDivisible; // Sum of numbers not divisible by n
return abs(sumNotDivisible – sumDivisible); // Absolute difference
}
int main() {
int n, m;
cin >> n >> m;
cout << differenceOfSum(m, n) << endl;
return 0;
}
def difference_of_sum(m, n):
sum_all = m * (m + 1) // 2 # Sum of all numbers from 1 to m
count_divisible = m // n # Count of numbers divisible by n
sum_divisible = n * (count_divisible * (count_divisible + 1)) // 2 # Sum of divisible numbers
sum_not_divisible = sum_all – sum_divisible # Sum of numbers not divisible by n
return abs(sum_not_divisible – sum_divisible) # Absolute difference
# Input
n = int(input())
m = int(input())
print(difference_of_sum(m, n))
Question 4 : Second Largest and Second Smallest: The Position-Based Sum
Problem Statement: You are required to implement the following Function 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 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 a seven
NOTE
- Return 0 if array is empty
- Return 0, if array length is 3 or less than 3
Example:-
Input:
arr:3 2 1 7 5 4
Output:
7
Explanation
- Second largest among even position elements(1 3 5) is 3
- Second largest among odd position element is 4
- Thus output is 3+4 = 7
Sample Input:
arr:1 8 0 2 3 5 6
Sample Output: 8
[Accenture Coding Questions and Answers]
Solution :
import java.util.Scanner;
class Solution {
public static int largeSmallSum(int[] arr) {
int largestEven = Integer.MIN_VALUE, secondLargestEven = Integer.MIN_VALUE;
int smallestOdd = Integer.MAX_VALUE, secondSmallestOdd = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) { // Even-indexed elements
if (arr[i] > largestEven) {
secondLargestEven = largestEven;
largestEven = arr[i];
} else if (arr[i] > secondLargestEven) {
secondLargestEven = arr[i];
}
} else { // Odd-indexed elements
if (arr[i] < smallestOdd) {
secondSmallestOdd = smallestOdd;
smallestOdd = arr[i];
} else if (arr[i] < secondSmallestOdd) {
secondSmallestOdd = arr[i];
}
}
}
return secondLargestEven + secondSmallestOdd;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(largeSmallSum(arr));
}
}
#include <iostream>
#include <climits>
using namespace std;
int largeSmallSum(int arr[], int n) {
int largestEven = INT_MIN, secondLargestEven = INT_MIN;
int smallestOdd = INT_MAX, secondSmallestOdd = INT_MAX;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) { // Even-indexed elements
if (arr[i] > largestEven) {
secondLargestEven = largestEven;
largestEven = arr[i];
} else if (arr[i] > secondLargestEven) {
secondLargestEven = arr[i];
}
} else { // Odd-indexed elements
if (arr[i] < smallestOdd) {
secondSmallestOdd = smallestOdd;
smallestOdd = arr[i];
} else if (arr[i] < secondSmallestOdd) {
secondSmallestOdd = arr[i];
}
}
}
return secondLargestEven + secondSmallestOdd;
}
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << largeSmallSum(arr, n) << endl;
return 0;
}
def large_small_sum(arr):
 largest_even = float(‘-inf’)
 second_largest_even = float(‘-inf’)
 smallest_odd = float(‘inf’)
 second_smallest_odd = float(‘inf’)
for i in range(len(arr)):
 if i % 2 == 0: # Even-indexed elements
  if arr[i] > largest_even:
   second_largest_even = largest_even
   largest_even = arr[i]
  elif arr[i] > second_largest_even:
   second_largest_even = arr[i]
 else: # Odd-indexed elements
  if arr[i] < smallest_odd:
   second_smallest_odd = smallest_odd
   smallest_odd = arr[i]
 elif arr[i] < second_smallest_odd:
   second_smallest_odd = arr[i]
return second_largest_even + second_smallest_odd
# Input
 n = int(input())
 arr = list(map(int, input().split()))
 print(large_small_sum(arr))
Solution :
import java.util.HashSet;
public class SmallestMissingPositive {
public static int findSmallestMissing(int[] arr) {
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
if (num > 0) {
set.add(num);
}
}
int i = 1;
while (set.contains(i)) {
i++;
}
return i;
}
}
#include <vector>
#include <unordered_set>
int findSmallestMissing(const std::vector<int>& arr) {
std::unordered_set<int> set;
for (int num : arr) {
if (num > 0) {
set.insert(num);
}
}
int i = 1;
while (set.find(i) != set.end()) {
i++;
}
return i;
}
def find_smallest_missing(arr):
s = set(x for x in arr if x > 0)
i = 1
while i in s:
i += 1
return i
Question 6 : Longest Substring Without Repeating Characters
Problem Statement:
Given a string s
, find the length of the longest substring that does not contain any repeating characters.
Input Format:
- A single string
s
consisting of lowercase and uppercase letters, digits, and symbols.
Output Format:
- An integer representing the length of the longest substring without repeating characters.
Example Input and Output:
Input: abcabcbb
Output: 3
Explanation:
The longest substring without repeating characters is "abc"
, which has length 3
.
Â
[Accenture Coding Questions and Answers]
Solution :
import java.util.HashSet;
public class LongestUniqueSubstring {
public static int lengthOfLongestSubstring(String s) {
int maxLength = 0, left = 0;
HashSet<Character> set = new HashSet<>();
for (int right = 0; right < s.length(); right++) {
while (set.contains(s.charAt(right))) {
set.remove(s.charAt(left));
left++;
}
set.add(s.charAt(right));
maxLength = Math.max(maxLength, right – left + 1);
}
return maxLength;
}
public static void main(String[] args) {
System.out.println(lengthOfLongestSubstring(“abcabcbb”));Â
}
}
#include <iostream>
#include <unordered_set>
using namespace std;
int lengthOfLongestSubstring(string s) {
unordered_set<char> charSet;
int left = 0, maxLength = 0;
for (int right = 0; right < s.length(); right++) {
while (charSet.find(s[right]) != charSet.end()) {
charSet.erase(s[left]);
left++;
}
charSet.insert(s[right]);
maxLength = max(maxLength, right – left + 1);
}
return maxLength;
}
int main() {
cout << lengthOfLongestSubstring(“abcabcbb”) << endl;Â
return 0;
}
def length_of_longest_substring(s):
char_set = set()
left = 0
max_length = 0
for right in range(len(s)):
while s[right] in char_set:
char_set.remove(s[left])
left += 1
char_set.add(s[right])
max_length = max(max_length, right – left + 1)
return max_length
# Example usage
print(length_of_longest_substring(“abcabcbb”))Â
Question 7 : Shorten Word with Middle Character Count
Problem Statement:
Given a word, return a string that consists of:
- The first letter.
- The count of middle characters.
- The last letter.
If the word length is 2 or less, return the word as it is.
Input Format:
  A single string s
(length ≥ 1).
Output Format:
  A shortened string in the specified format.
Example Input and Output:
Input: examination
Output: e9n
Explanation:
- First letter:
e
- Middle characters count:
9
(“xaminatio”) - Last letter:
n
- Final output:
"e9n"
[Accenture Coding Questions and Answers]
Solution :
public class ShortenWord {
public static String shorten(String word) {
int len = word.length();
if (len <= 2) return word;
return word.charAt(0) + String.valueOf(len – 2) + word.charAt(len – 1);
}
public static void main(String[] args) {
System.out.println(shorten(“examination”));
}
}
#include <iostream>
using namespace std;
string shorten(string word) {
int len = word.length();
if (len <= 2) return word;
return word[0] + to_string(len – 2) + word[len – 1];
}
int main() {
cout << shorten(“examination”) << endl;Â
return 0;
}
def shorten(word):
if len(word) <= 2:
return word
return word[0] + str(len(word) – 2) + word[-1]
# Example usage
print(shorten(“examination”))Â
Question 8 : Parking Lot Problem
Problem Statement:
You are given a character array arr
of size n
, where:
'S'
represents an empty parking slot.'X'
represents an occupied slot.
You need to return the maximum number of cars that can be parked in the parking lot, assuming that:
- A car can only park in consecutive empty slots (
'S'
). - Two cars cannot occupy the same slot.
Input Format:
- An integer
n
representing the number of slots. - A string of length
n
consisting of'S'
(empty) and'X'
(occupied).
Output Format:
- Â An integer representing the maximum number of cars that can be parked.
Example Input and Output:
Input:Â n= 16Â Â Â arr= XXXSXXSXXSSXXSXX
Output: 7
Explanation:
- The empty slots (
S
) are at positions:[3, 6, 9, 10, 13]
. - Consecutive empty slots are
SS
at indices[9, 10]
, so 2 cars can be parked there. - Single empty slots can hold 1 car each.
- Total cars that can be parked =
7
.
[Accenture Coding Questions and Answers]
Solution :
public class ParkingLot {
public static int maxCarsCanBeParked(String arr) {
int count = 0;
int n = arr.length();
for (int i = 0; i < n; i++) {
if (arr.charAt(i) == ‘S’) {
count++; // Count each empty slot
if (i > 0 && arr.charAt(i – 1) == ‘S’) {
count++; // Additional car in consecutive empty slots
}
}
}
return count;
}
public static void main(String[] args) {
String arr = “XXXSXXSXXSSXXSXX”;
System.out.println(maxCarsCanBeParked(arr)); // Output: 7
}
}
#include <iostream>
using namespace std;
int maxCarsCanBeParked(string arr) {
int count = 0;
int n = arr.length();
for (int i = 0; i < n; i++) {
if (arr[i] == ‘S’) {
count++; // Count each empty slot
if (i > 0 && arr[i – 1] == ‘S’) {
count++; // Extra car for consecutive empty slots
}
}
}
return count;
}
int main() {
string arr = “XXXSXXSXXSSXXSXX”;
cout << maxCarsCanBeParked(arr) << endl; // Output: 7
return 0;
}
def max_cars_can_be_parked(arr):
count = 0
n = len(arr)
for i in range(n):
if arr[i] == ‘S’:
count += 1 # Count each empty slot
if i > 0 and arr[i – 1] == ‘S’:
count += 1 # Extra car for consecutive empty slots
return count
# Example usage
arr = “XXXSXXSXXSSXXSXX”
print(max_cars_can_be_parked(arr))Â
Question 9 : Calculate Prime Sum
Problem Statement:Â Â Int CalculatePrimeSum(int m, int n);
Calculate and return the sum of prime numbers between ‘m’ and ‘n’ (inclusive).
Note: 0 < m <= n
Input Format:
- Two integers
m
andn
such that0 < m <= n
.
Output Format:
- A single integer representing the sum of all prime numbers in the given range.
Example Input and Output:
Input: Â m= 10Â n= 50
Output: 158
Explanation:
- The prime numbers between
10
and50
are:
11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
Sum: 11 + 13 + 17 + 19 + 23 + 29 + 31 + 37 + 41 + 43 + 47 = 158
[Accenture Coding Questions and Answers]
Solution :
public class PrimeSum {
public static boolean isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
public static int CalculatePrimeSum(int m, int n) {
int sum = 0;
for (int i = m; i <= n; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
public static void main(String[] args) {
System.out.println(CalculatePrimeSum(10, 50)); // Output: 158
}
}
#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
int CalculatePrimeSum(int m, int n) {
int sum = 0;
for (int i = m; i <= n; i++) {
if (isPrime(i)) {
sum += i;
}
}
return sum;
}
int main() {
cout << CalculatePrimeSum(10, 50) << endl; // Output: 158
return 0;
}
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
def calculate_prime_sum(m, n):
return sum(i for i in range(m, n + 1) if is_prime(i))
# Example usage
print(calculate_prime_sum(10, 50)) # Output: 158