Accenture Coding Questions and Solution 2023

Want to crack the Accenture coding round but don’t know where to start from? Look no further because we’re here with Accenture coding questions with Solutions asked in  interviews which will help you crack accenture coding tests. 

Accenture is an international services company which includes a coding round for their hiring process.  In the coding round you need to solve the problems in order to get recruited. There are multiple elements included in this coding round like data structures, algorithms, and sometimes coding exercises related to real-world scenarios. 

The coding rounds are conducted to test the ability of the candidate and how effectively he can write logical code. Candidates are expected to write the right answer and solve the problem and it’s pretty crucial that you solve the problem to get hired at the company.  

If you’re preparing for coding rounds you can check our recent post on IBM coding  round. To give it to you directly the coding questions asked in the Accenture aren’t that tough if you are prepared. There are multiple elements that you need to focus on in order to prepare for the test. Today we’ve covered Accenture Coding Questions with Solutions. There are Accenture Coding Questions in Python, Accenture Coding Questions in C and many other languages. you can download the accenture coding questions and answers pdf at the bottom of this post.

Accenture Coding  Questions Round Duration 

Writing the code from scratch the candidate will be given 20 questions and 45 minutes of time to solve the problem. The languages to write the codes are:

  • C
  • C++
  • Java
  • Python
  • Dot Net

Rules for Accenture Coding Questions for Freshers Round Section:

Number of questions 

20 Questions 

Type of Test 

Non- Adaptive

Time Duration 

45 minutes

Negative Marking 

No

 

Latest Accenture Coding Questions And Answers 2024 for placement

 

  1. Chocolate Distribution 

(Asked in Accenture On Campus 10 Aug 2022, Slot 2)

Problem Description:

The function accepts an integer array ‘arr’ of size ‘n’ as its argument. Each element of ‘arr’ represents the number of chocolates distributed to a person. The function needs to return the minimum number of chocolates that need to be distributed to each person so that the difference between the chocolates of any two people is minimized.

Example:

Input:

  

Output:

n: 5

  

3

arr: 10 4 12 3 1

   

Solution : 

#include <stdio.h>

int min_chocolates(int arr[], int n) {
    // Sort the array
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // swap
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    // Find the minimum difference
    int min_diff = arr[n - 1] - arr[0];
    for (int i = 1; i <= n - 5; i++) {
        if (arr[i + 4] - arr[i] < min_diff) {
            min_diff = arr[i + 4] - arr[i];
        }
    }

    return min_diff;
}

int main() {
    int n = 5;
    int arr[] = {10, 4, 12, 3, 1};
    int result = min_chocolates(arr, n);
    printf("%d\n", result);

    return 0;
}
#include <iostream>
#include <algorithm>

using namespace std;

int min_chocolates(int arr[], int n) {
    sort(arr, arr + n);

    int min_diff = arr[n - 1] - arr[0];
    for (int i = 1; i <= n - 5; i++) {
        min_diff = min(min_diff, arr[i + 4] - arr[i]);
    }

    return min_diff;
}

int main() {
    int n = 5;
    int arr[] = {10, 4, 12, 3, 1};
    int result = min_chocolates(arr, n);
    cout << result << endl;

    return 0;
}
import java.util.Arrays;

public class ChocolateDistribution {
    public static int minChocolates(int[] arr) {
        Arrays.sort(arr);

        int minDiff = arr[arr.length - 1] - arr[0];
        for (int i = 0; i <= arr.length - 5; i++) {
            minDiff = Math.min(minDiff, arr[i + 4] - arr[i]);
        }

        return minDiff;
    }

    public static void main(String[] args) {
        int[] arr = {10, 4, 12, 3, 1};
        int result = minChocolates(arr);
        System.out.println(result);
    }
}
def min_chocolates(arr):
    arr.sort()

    min_diff = arr[-1] - arr[0]
    for i in range(len(arr) - 4):
        min_diff = min(min_diff, arr[i + 4] - arr[i])

    return min_diff

arr = [10, 4, 12, 3, 1]
result = min_chocolates(arr)
print(result)

 

  1. Parking Lot 

( Asked in Accenture, On Campus
10 Aug 2022, Slot 3)

Problem Description:

The function accepts a character array ‘arr’ of size ‘n’ as its argument. Each element of ‘arr’ represents the status of a parking slot, where ‘S’ represents an empty slot and ‘X’ represents an occupied slot. The function needs to return the maximum number of cars that can be parked in the parking lot. It is assumed that two cars cannot occupy the same slot and cars can only park in consecutive empty slots.


Example:

Input:

  

Output:

n: 16

  

7

arr: XXXSXXSXXSSXXSXX

   

 

Solution :

#include <stdio.h>

int max_cars_parked(int n, char arr[]) {
    int max_cars = 0;
    int current_cars = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] == 'S') {
            current_cars++;
        } else {
            max_cars = (max_cars > current_cars) ? max_cars : current_cars;
            current_cars = 0;
        }
    }

    max_cars = (max_cars > current_cars) ? max_cars : current_cars;
    return max_cars;
}

int main() {
    int n = 16;
    char arr[] = "XXXSXXSXXSSXXSXX";
    int result = max_cars_parked(n, arr);
    printf("%d\n", result);

    return 0;
}
#include <iostream>

using namespace std;

int max_cars_parked(int n, char arr[]) {
    int max_cars = 0;
    int current_cars = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] == 'S') {
            current_cars++;
        } else {
            max_cars = max(max_cars, current_cars);
            current_cars = 0;
        }
    }

    max_cars = max(max_cars, current_cars);
    return max_cars;
}

int main() {
    int n = 16;
    char arr[] = "XXXSXXSXXSSXXSXX";
    int result = max_cars_parked(n, arr);
    cout << result << endl;

    return 0;
}
public class ParkingLot {
    public static int maxCarsParked(int n, char[] arr) {
        int maxCars = 0;
        int currentCars = 0;

        for (int i = 0; i < n; i++) {
            if (arr[i] == 'S') {
                currentCars++;
            } else {
                maxCars = Math.max(maxCars, currentCars);
                currentCars = 0;
            }
        }

        maxCars = Math.max(maxCars, currentCars);
        return maxCars;
    }

    public static void main(String[] args) {
        int n = 16;
        char[] arr = "XXXSXXSXXSSXXSXX".toCharArray();
        int result = maxCarsParked(n, arr);
        System.out.println(result);
    }
}
def max_cars_parked(n, arr):
    max_cars = 0
    current_cars = 0

    for status in arr:
        if status == 'S':
            current_cars += 1
        else:
            max_cars = max(max_cars, current_cars)
            current_cars = 0

    max_cars = max(max_cars, current_cars)
    return max_cars

n = 16
arr = "XXXSXXSXXSSXXSXX"
result = max_cars_parked(n, arr)
print(result)

 

  1. String Transformation 

(Asked in Accenture OnCampus 10 Aug 2022, Slot 4)

 

Problem Description:

The function accepts a string ‘str’ as its argument. The function needs to return the transformed string by replacing all occurrences of the character ‘a’ with the character ‘b’ and vice versa.

Example:

Input:

  

Output:

str: abaabbcc

  

bbbbaaac

 

   

 

Solution : 

#include <stdio.h>
#include <string.h>

char* transformString(char* str) {
    int length = strlen(str);
    for (int i = 0; i < length; i++) {
        if (str[i] == 'a')
            str[i] = 'b';
        else if (str[i] == 'b')
            str[i] = 'a';
    }
    return str;
}

int main() {
    char str[] = "abaabbcc";
    printf("%s\n", transformString(str));
    return 0;
}
#include <iostream>
#include <string>

std::string transformString(std::string str) {
    for (char &c : str) {
        if (c == 'a')
            c = 'b';
        else if (c == 'b')
            c = 'a';
    }
    return str;
}

int main() {
    std::string str = "abaabbcc";
    std::cout << transformString(str) << std::endl;
    return 0;
}
public class StringTransformation {
    public static String transformString(String str) {
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == 'a')
                charArray[i] = 'b';
            else if (charArray[i] == 'b')
                charArray[i] = 'a';
        }
        return new String(charArray);
    }

    public static void main(String[] args) {
        String str = "abaabbcc";
        System.out.println(transformString(str));
    }
}
def transform_string(str):

    transformed_string = ""

    for i in range(len(str)):
        if str[i] == 'a':
            transformed_string += 'b'
        elif str[i] == 'b':
            transformed_string += 'a'
        else:
            transformed_string += str[i]


    return transformed_string


str = "abaabbcc"
transformed_string = transform_string(str)
print(transformed_string)

 

  1. Array Equilibrium 

(Asked in Accenture OnCampus 10 Aug 2022, Slot 5)

 

Problem Description:

The function accepts an integer array ‘arr’ of size ‘n’ as its argument. The function needs to return the index of an equilibrium point in the array, where the sum of elements on the left of the index is equal to the sum of elements on the right of the index. If no equilibrium point exists, the function should return -1.

Example:

Input:

  

Output:

n: 5

  

3

arr: 1 3 5 7 3

   

 

Solution : 

#include <stdio.h>

int findEquilibrium(int arr[], int n) {
    int totalSum = 0, leftSum = 0;
    
    for (int i = 0; i < n; i++)
        totalSum += arr[i];

    for (int i = 0; i < n; i++) {
        totalSum -= arr[i];
        if (leftSum == totalSum)
            return i;
        leftSum += arr[i];
    }

    return -1;
}

int main() {
    int n = 5;
    int arr[] = {1, 3, 5, 7, 3};
    printf("%d\n", findEquilibrium(arr, n));
    return 0;
}
#include <iostream>

int findEquilibrium(int arr[], int n) {
    int totalSum = 0, leftSum = 0;
    
    for (int i = 0; i < n; i++)
        totalSum += arr[i];

    for (int i = 0; i < n; i++) {
        totalSum -= arr[i];
        if (leftSum == totalSum)
            return i;
        leftSum += arr[i];
    }

    return -1;
}

int main() {
    int n = 5;
    int arr[] = {1, 3, 5, 7, 3};
    std::cout << findEquilibrium(arr, n) << std::endl;
    return 0;
}
public class EquilibriumPoint {

    static int findEquilibrium(int arr[], int n) {
        int totalSum = 0, leftSum = 0;

        for (int i = 0; i < n; i++)
            totalSum += arr[i];

        for (int i = 0; i < n; i++) {
            totalSum -= arr[i];
            if (leftSum == totalSum)
                return i;
            leftSum += arr[i];
        }

        return -1;
    }

    public static void main(String[] args) {
        int n = 5;
        int[] arr = {1, 3, 5, 7, 3};
        System.out.println(findEquilibrium(arr, n));
    }
}
def find_equilibrium(arr):
    total_sum = sum(arr)
    left_sum = 0

    for i in range(len(arr)):
        total_sum -= arr[i]
        if left_sum == total_sum:
            return i
        left_sum += arr[i]

    return -1

arr = [1, 3, 5, 7, 3]
print(find_equilibrium(arr))

 

  1. Array Rotation 

(Asked in Accenture OnCampus 10 Aug 2022, Slot 6)

Problem Description:

The function accepts an integer array ‘arr’ of size ‘n’ and an integer ‘d’ as its argument. The function needs to rotate the array ‘arr’ by ‘d’ positions to the right. The rotation should be done in place, without using any additional memory.

 

Example:

Input:

  

Output:

n: 5

  

3 4 5 1 2

arr: 1 2 3 4 5

d: 3

   

 

Solution : 

#include <stdio.h>

void rotateArray(int arr[], int n, int d) {
    int temp[d];
    for (int i = 0; i < d; i++) {
        temp[i] = arr[n - d + i];
    }
    for (int i = n - 1; i >= d; i--) {
        arr[i] = arr[i - d];
    }
    for (int i = 0; i < d; i++) {
        arr[i] = temp[i];
    }
}

int main() {
    int n = 5;
    int arr[] = {1, 2, 3, 4, 5};
    int d = 3;
    
    rotateArray(arr, n, d);

    printf("Output: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}
#include <iostream>

void rotateArray(int arr[], int n, int d) {
    int temp[d];
    for (int i = 0; i < d; i++) {
        temp[i] = arr[n - d + i];
    }
    for (int i = n - 1; i >= d; i--) {
        arr[i] = arr[i - d];
    }
    for (int i = 0; i < d; i++) {
        arr[i] = temp[i];
    }
}

int main() {
    int n = 5;
    int arr[] = {1, 2, 3, 4, 5};
    int d = 3;
    
    rotateArray(arr, n, d);

    std::cout << "Output: ";
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }

    return 0;
}
public class ArrayRotation {
    public static void rotateArray(int[] arr, int n, int d) {
        int[] temp = new int[d];
        for (int i = 0; i < d; i++) {
            temp[i] = arr[n - d + i];
        }
        for (int i = n - 1; i >= d; i--) {
            arr[i] = arr[i - d];
        }
        for (int i = 0; i < d; i++) {
            arr[i] = temp[i];
        }
    }

    public static void main(String[] args) {
        int n = 5;
        int[] arr = {1, 2, 3, 4, 5};
        int d = 3;

        rotateArray(arr, n, d);

        System.out.print("Output: ");
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
def rotate_array(arr, n, d):
    temp = arr[-d:]
    for i in range(n - 1, d - 1, -1):
        arr[i] = arr[i - d]
    arr[:d] = temp

n = 5
arr = [1, 2, 3, 4, 5]
d = 3

rotate_array(arr, n, d)

print("Output:", *arr)

 

Also Read :

  1. Substring Search 

(Asked in Accenture On Campus 10 Aug 2022, Slot 7)


Problem Description:

The function accepts two strings ‘str1’ and ‘str2’ as its argument. The function needs to return the index of the first occurrence of substring ‘str2’ in string ‘str1’ or -1 if the substring is not found.

 

Example:

Input:

  

Output:

str1: “Hello, World!”

  

7

str2: “World”

   

 

Solution : 

#include <stdio.h>
#include <string.h>

int substringSearch(char str1[], char str2[]) {
    char *ptr = strstr(str1, str2);
    if (ptr != NULL) {
        return ptr - str1;
    } else {
        return -1;
    }
}

int main() {
    char str1[] = "Hello, World!";
    char str2[] = "World";
    printf("%d\n", substringSearch(str1, str2));
    return 0;
}
#include <iostream>
#include <cstring>

int substringSearch(const char* str1, const char* str2) {
    const char* ptr = std::strstr(str1, str2);
    if (ptr != nullptr) {
        return ptr - str1;
    } else {
        return -1;
    }
}

int main() {
    const char* str1 = "Hello, World!";
    const char* str2 = "World";
    std::cout << substringSearch(str1, str2) << std::endl;
    return 0;
}
public class SubstringSearch {
    public static int substringSearch(String str1, String str2) {
        int index = str1.indexOf(str2);
        return index != -1 ? index : -1;
    }

    public static void main(String[] args) {
        String str1 = "Hello, World!";
        String str2 = "World";
        System.out.println(substringSearch(str1, str2));
    }
}
def substring_search(str1, str2):
    index = str1.find(str2)
    return index if index != -1 else -1

str1 = "Hello, World!"
str2 = "World"
print(substring_search(str1, str2))

 

  1. Palindrome Check

(Asked in Accenture OnCampus 10 Aug 2022, Slot 8)

Problem Description:

The function accepts a string ‘str’ as its argument. The function needs to determine whether the string is a palindrome or not. A palindrome is a word or phrase that reads the same backward as forward.

 

Example:

Input:

  

Output:

str: “madam”

  

1

 

   

 

Solution:

#include <stdio.h>
#include <string.h>

int isPalindrome(char *str) {
    int length = strlen(str);
    for (int i = 0; i < length / 2; i++) {
        if (str[i] != str[length - i - 1]) {
            return 0; // Not a palindrome
        }
    }
    return 1; // Palindrome
}

int main() {
    char str[] = "madam";
    if (isPalindrome(str)) {
        printf("1\n");
    } else {
        printf("0\n");
    }
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(string str) {
    int length = str.length();
    for (int i = 0; i < length / 2; i++) {
        if (str[i] != str[length - i - 1]) {
            return false; // Not a palindrome
        }
    }
    return true; // Palindrome
}

int main() {
    string str = "madam";
    if (isPalindrome(str)) {
        cout << "1" << endl;
    } else {
        cout << "0" << endl;
    }
    return 0;
}
public class PalindromeCheck {
    static boolean isPalindrome(String str) {
        int length = str.length();
        for (int i = 0; i < length / 2; i++) {
            if (str.charAt(i) != str.charAt(length - i - 1)) {
                return false; // Not a palindrome
            }
        }
        return true; // Palindrome
    }

    public static void main(String[] args) {
        String str = "madam";
        if (isPalindrome(str)) {
            System.out.println("1");
        } else {
            System.out.println("0");
        }
    }
}
def isPalindrome(s):
    return s == s[::-1]

str_input = "madam"
if isPalindrome(str_input):
    print("1")
else:
    print("0")

 

  1. Reverse Words 

(Asked in Accenture OnCampus 10 Aug 2022, Slot 9)

Problem Description:

The function accepts a string ‘str’ as its argument. The function needs to reverse the order of the words in the string.

 

Example:

Input:

  

Output:

str: “Hello, World!”

  

!dlroW ,olleH

 

   

 

Solution: 

#include <stdio.h>
#include <string.h>

void reverseWords(char* str) {
    char* word = strtok(str, " ");
    if(word != NULL) {
        reverseWords(word + strlen(word) + 1);
        printf("%s ", word);
    }
}

int main() {
    char str[] = "Hello, World!";
    reverseWords(str);
    return 0;
}
#include <iostream>
#include <sstream>
#include <algorithm>

void reverseWords(std::string& str) {
    std::istringstream iss(str);
    std::string word;
    if (iss >> word) {
        reverseWords(iss.str());
        std::cout << word << " ";
    }
}

int main() {
    std::string str = "Hello, World!";
    reverseWords(str);
    return 0;
}
public class ReverseWords {
    public static void reverseWords(String str) {
        String[] words = str.split(" ");
        for (int i = words.length - 1; i >= 0; i--) {
            System.out.print(words[i] + " ");
        }
    }

    public static void main(String[] args) {
        String str = "Hello, World!";
        reverseWords(str);
    }
}
def reverse_words(s):
    words = s.split()
    reversed_words = " ".join(reversed(words))
    print(reversed_words)

str_input = "Hello, World!"
reverse_words(str_input)

 

  1. Find Two Numbers with Sum N

 

Problem Description:

Given an array of integers and an integer sum, find a pair of numbers (a, b) in the array where a + b = sum.

 

Example:

Input:

  

Output:

An array of integers

  

An array of two integers representing the pair (a, b) or -1 if no such pair exists

An integer sum

   


Explanation:

Given an array of integers, such as [5, 2, 4, 1, 3], and an integer sum, such as 9, the algorithm should determine that the pair (a, b) = (2, 7) or (4, 5) satisfies the condition a + b = sum. If no such pair exists, the algorithm should return -1.

 

Solution : 

#include <stdio.h>

void findPair(int arr[], int size, int sum) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] + arr[j] == sum) {
                printf("Pair found: %d, %d\n", arr[i], arr[j]);
                return;
            }
        }
    }
    printf("Pair not found\n");
}

int main() {
    int arr[] = {1, 4, 7, 8, 3, 9};
    int sum = 10;
    int size = sizeof(arr) / sizeof(arr[0]);
    findPair(arr, size, sum);
    return 0;
}
#include <iostream>

void findPair(int arr[], int size, int sum) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] + arr[j] == sum) {
                std::cout << "Pair found: " << arr[i] << ", " << arr[j] << std::endl;
                return;
            }
        }
    }
    std::cout << "Pair not found" << std::endl;
}

int main() {
    int arr[] = {1, 4, 7, 8, 3, 9};
    int sum = 10;
    int size = sizeof(arr) / sizeof(arr[0]);
    findPair(arr, size, sum);
    return 0;
}
public class FindPair {
    static void findPair(int arr[], int sum) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] + arr[j] == sum) {
                    System.out.println("Pair found: " + arr[i] + ", " + arr[j]);
                    return;
                }
            }
        }
        System.out.println("Pair not found");
    }

    public static void main(String[] args) {
        int arr[] = {1, 4, 7, 8, 3, 9};
        int sum = 10;
        findPair(arr, sum);
    }
}
def find_pair(arr, sum):
    for i in range(len(arr) - 1):
        for j in range(i + 1, len(arr)):
            if arr[i] + arr[j] == sum:
                print(f"Pair found: {arr[i]}, {arr[j]}")
                return
    print("Pair not found")

arr = [1, 4, 7, 8, 3, 9]
sum_val = 10
find_pair(arr, sum_val)

 

  1. Maximum Subarray Sum

 

Problem Description:

Given an array of integers, find the maximum subarray sum. A subarray is a contiguous subsequence of the array.

Eample:

Input:

  

Output:

An array of integers

  

An integer representing the maximum subarray sum

 

   

 

Explanation:

Given an array of integers, such as [-2, 1, -3, 4, -1, 2, 1, -5, 4], the algorithm should determine that the maximum subarray sum is 6 ([4, -1, 2, 1]).


Solution :

#include <stdio.h>

int maxSubArraySum(int arr[], int size) {
    int max_so_far = arr[0];
    int max_ending_here = arr[0];

    for (int i = 1; i < size; i++) {
        max_ending_here = (max_ending_here + arr[i] > arr[i]) ? max_ending_here + arr[i] : arr[i];
        max_so_far = (max_so_far > max_ending_here) ? max_so_far : max_ending_here;
    }

    return max_so_far;
}

int main() {
    int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
    int size = sizeof(arr) / sizeof(arr[0]);
    printf("Maximum Subarray Sum is %d\n", maxSubArraySum(arr, size));
    return 0;
}
#include <iostream>
using namespace std;

int maxSubArraySum(int arr[], int size) {
    int max_so_far = arr[0];
    int max_ending_here = arr[0];

    for (int i = 1; i < size; i++) {
        max_ending_here = (max_ending_here + arr[i] > arr[i]) ? max_ending_here + arr[i] : arr[i];
        max_so_far = (max_so_far > max_ending_here) ? max_so_far : max_ending_here;
    }

    return max_so_far;
}

int main() {
    int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
    int size = sizeof(arr) / sizeof(arr[0]);
    cout << "Maximum Subarray Sum is " << maxSubArraySum(arr, size) << endl;
    return 0;
}
public class MaxSubArraySum {
    public static int maxSubArraySum(int[] arr) {
        int max_so_far = arr[0];
        int max_ending_here = arr[0];

        for (int i = 1; i < arr.length; i++) {
            max_ending_here = Math.max(max_ending_here + arr[i], arr[i]);
            max_so_far = Math.max(max_so_far, max_ending_here);
        }

        return max_so_far;
    }

    public static void main(String[] args) {
        int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};
        System.out.println("Maximum Subarray Sum is " + maxSubArraySum(arr));
    }
}
def max_sub_array_sum(arr):
    max_so_far = max_ending_here = arr[0]

    for num in arr[1:]:
        max_ending_here = max(num, max_ending_here + num)
        max_so_far = max(max_so_far, max_ending_here)

    return max_so_far

arr = [-2, -3, 4, -1, -2, 1, 5, -3]
print("Maximum Subarray Sum is", max_sub_array_sum(arr))

 

  1. Character Replacement

 

Problem Description:

Given a string str, a character ch1, and a character ch2, replace all occurrences of ch1 in str with ch2 and vice versa.

 

Input:

  

Output:

A string str

  

The modified string str where all occurrences of ch1 are replaced with ch2 and vice versa

A character ch1

A character ch2

   

 

Example:

Input:

  

Output:

str = “apples”, ch1 = ‘a’, ch2 = ‘p’

  

str = “paales”

 

   

 

Solution : 

#include <stdio.h>
#include <string.h>

void replaceCharacters(char str[], char ch1, char ch2) {
    int len = strlen(str);
    for (int i = 0; i < len; ++i) {
        if (str[i] == ch1)
            str[i] = ch2;
        else if (str[i] == ch2)
            str[i] = ch1;
    }
}

int main() {
    char str[] = "apples";
    char ch1 = 'a';
    char ch2 = 'p';
    replaceCharacters(str, ch1, ch2);
    printf("Modified string: %s\n", str);
    return 0;
}
#include <iostream>
#include <string>

void replaceCharacters(std::string &str, char ch1, char ch2) {
    for (char &c : str) {
        if (c == ch1)
            c = ch2;
        else if (c == ch2)
            c = ch1;
    }
}

int main() {
    std::string str = "apples";
    char ch1 = 'a';
    char ch2 = 'p';
    replaceCharacters(str, ch1, ch2);
    std::cout << "Modified string: " << str << std::endl;
    return 0;
}
public class CharacterReplacement {
    public static String replaceCharacters(String str, char ch1, char ch2) {
        char[] charArray = str.toCharArray();
        for (int i = 0; i < charArray.length; ++i) {
            if (charArray[i] == ch1)
                charArray[i] = ch2;
            else if (charArray[i] == ch2)
                charArray[i] = ch1;
        }
        return new String(charArray);
    }

    public static void main(String[] args) {
        String str = "apples";
        char ch1 = 'a';
        char ch2 = 'p';
        String modifiedStr = replaceCharacters(str, ch1, ch2);
        System.out.println("Modified string: " + modifiedStr);
    }
}
def replace_characters(input_str, ch1, ch2):
    return input_str.replace(ch1, 'temp').replace(ch2, ch1).replace('temp', ch2)

str_value = "apples"
ch1_value = 'a'
ch2_value = 'p'
modified_str = replace_characters(str_value, ch1_value, ch2_value)
print("Modified string:", modified_str)

 

  1. Find the Minimum Value and Its Index in the Array

 

Problem Description:

Given an integer array, find the minimum value and its index in the array.

 

Input:

  

Output:

An integer array

  

The minimum value and its index, separated by a newline character

 

   

 

 

Example:

Input:

  

Output:

[5, 2, 4, 1, 3]

  

1 3

 

   

 

Solution : 

#include <stdio.h>

int main() {
    int arr[] = {5, 2, 4, 1, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    int min = arr[0], index = 0;
    for (int i = 1; i < n; ++i) {
        if (arr[i] < min) {
            min = arr[i];
            index = i;
        }
    }

    printf("%d %d\n", min, index);

    return 0;
}
#include <iostream>
using namespace std;

int main() {
    int arr[] = {5, 2, 4, 1, 3};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    int min = arr[0], index = 0;
    for (int i = 1; i < n; ++i) {
        if (arr[i] < min) {
            min = arr[i];
            index = i;
        }
    }

    cout << min << " " << index << endl;

    return 0;
}
public class MinValueAndIndex {
    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 1, 3};
        int min = arr[0], index = 0;

        for (int i = 1; i < arr.length; ++i) {
            if (arr[i] < min) {
                min = arr[i];
                index = i;
            }
        }

        System.out.println(min + " " + index);
    }
}

 

def find_min_and_index(arr):
    min_value = arr[0]
    min_index = 0

    for i in range(1, len(arr)):
        if arr[i] < min_value:
            min_value = arr[i]
            min_index = i

    return f"{min_value} {min_index}”

 

  1. Find the Average of All Positive Numbers in an Array


Problem Description:

Given an array of integers, find the average of all positive numbers in the array.

 

Input:

  

Output:

An integer array

  

The average of all positive numbers, or -1 if there are no positive numbers

 

   

 

Example:

Input:

  

Output:

[5, 2, -4, 1, 3]

  

3

 

   

 

Solution : 

#include <stdio.h>

float averagePositive(int arr[], int size) {
    int sum = 0, count = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            sum += arr[i];
            count++;
        }
    }
    return (count > 0) ? (float)sum / count : -1;
}

int main() {
    int arr[] = {5, 2, -4, 1, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    float result = averagePositive(arr, size);

    printf("Output: %.2f\n", result);

    return 0;
}
#include <iostream>

float averagePositive(int arr[], int size) {
    int sum = 0, count = 0;
    for (int i = 0; i < size; i++) {
        if (arr[i] > 0) {
            sum += arr[i];
            count++;
        }
    }
    return (count > 0) ? (float)sum / count : -1;
}

int main() {
    int arr[] = {5, 2, -4, 1, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    float result = averagePositive(arr, size);

    std::cout << "Output: " << result << std::endl;

    return 0;
}
public class AveragePositive {

    public static float averagePositive(int[] arr) {
        int sum = 0, count = 0;
        for (int num : arr) {
            if (num > 0) {
                sum += num;
                count++;
            }
        }
        return (count > 0) ? (float) sum / count : -1;
    }

    public static void main(String[] args) {
        int[] arr = {5, 2, -4, 1, 3};
        float result = averagePositive(arr);

        System.out.println("Output: " + result);
    }
}
def average_positive(arr):
    total = count = 0
    for num in arr:
        if num > 0:
            total += num
            count += 1
    return total / count if count > 0 else -1

arr = [5, 2, -4, 1, 3]
result = average_positive(arr)

print("Output:", result)

 

  1. Count the Occurrences of a Given Element in an Array

Problem Description:

Given an integer array and an integer element, count the number of occurrences of the element in the array.

 

Input:

  

Output:

An integer array

  

The number of occurrences of the element

An integer element

   

 

 

Example:

Input:

  

Output:

[5, 2, 4, 1, 2], 2

  

2

 

   

 

Solution : 

#include <stdio.h>

int count_occurrences(int arr[], int n, int element) {
    int count = 0;
    
    for (int i = 0; i < n; i++) {
        if (arr[i] == element) {
            count++;
        }
    }

    return count;
}

int main() {
    int arr[] = {5, 2, 4, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int element = 2;

    int result = count_occurrences(arr, n, element);
    printf("%d\n", result);

    return 0;
}
#include <iostream>

using namespace std;

int count_occurrences(int arr[], int n, int element) {
    int count = 0;

    for (int i = 0; i < n; i++) {
        if (arr[i] == element) {
            count++;
        }
    }

    return count;
}

int main() {
    int arr[] = {5, 2, 4, 1, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    int element = 2;

    int result = count_occurrences(arr, n, element);
    cout << result << endl;

    return 0;
}
public class OccurrenceCounter {
    public static int countOccurrences(int[] arr, int element) {
        int count = 0;

        for (int num : arr) {
            if (num == element) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 1, 2};
        int element = 2;

        int result = countOccurrences(arr, element);
        System.out.println(result);
    }
}
def count_occurrences(arr, element):
    count = 0

    for num in arr:
        if num == element:
            count += 1

    return count

arr = [5, 2, 4, 1, 2]
element = 2
result = count_occurrences(arr, element)
print(result)

 

  1. Check if an Array Contains a Given Element

Problem Description:

Given an integer array and an integer element, check if the array contains the element.

Input:

  

Output:

An integer array

  

True if the element is found, False otherwise

An integer element

   

 

Example:

Input:

  

Output:

[5, 2, 4, 1, 3], 2

  

True

 

   

 

Solution : 

#include <stdio.h>

int containsElement(int arr[], int size, int element) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == element) {
            return 1; // True
        }
    }
    return 0; // False
}

int main() {
    int arr[] = {5, 2, 4, 1, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    int element = 2;
    
    int result = containsElement(arr, size, element);
    printf(result ? "True\n" : "False\n");

    return 0;
}
#include <iostream>

using namespace std;

bool containsElement(int arr[], int size, int element) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == element) {
            return true;
        }
    }
    return false;
}

int main() {
    int arr[] = {5, 2, 4, 1, 3};
    int size = sizeof(arr) / sizeof(arr[0]);
    int element = 2;
    
    bool result = containsElement(arr, size, element);
    cout << (result ? "True" : "False") << endl;

    return 0;
}
public class ArrayContainsElement {
    public static boolean containsElement(int[] arr, int element) {
        for (int num : arr) {
            if (num == element) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 1, 3};
        int element = 2;
        
        boolean result = containsElement(arr, element);
        System.out.println(result ? "True" : "False");
    }
}
def contains_element(arr, element):
    return element in arr

arr = [5, 2, 4, 1, 3]
element = 2

result = contains_element(arr, element)
print("True" if result else "False")

 

  1. Problem Statement: Calculate Prime Sum

Implement the function:

Int CalculatePrimeSum(int m, int n);

Calculate and return the sum of prime numbers between ‘m’ and ‘n’ (inclusive).

Note: 0 < m <= n

 

Example:

Input:

  

Output:

m : 10

  

158

n : 50

   

 

Solution : 

#include <stdio.h>
#include <stdbool.h>

bool is_prime(int num) {
    if (num <= 1) {
        return false;
    }
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int calculate_prime_sum(int m, int n) {
    int sum = 0;
    for (int i = m; i <= n; i++) {
        if (is_prime(i)) {
            sum += i;
        }
    }
    return sum;
}

int main() {
    int m = 10, n = 50;
    int result = calculate_prime_sum(m, n);
    printf("%d\n", result);

    return 0;
}
#include <iostream>
#include <cmath>

using namespace std;

bool is_prime(int num) {
    if (num <= 1) {
        return false;
    }
    for (int i = 2; i * i <= num; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

int calculate_prime_sum(int m, int n) {
    int sum = 0;
    for (int i = m; i <= n; i++) {
        if (is_prime(i)) {
            sum += i;
        }
    }
    return sum;
}

int main() {
    int m = 10, n = 50;
    int result = calculate_prime_sum(m, n);
    cout << result << endl;

    return 0;
}
public class CalculatePrimeSum {
    static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    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) {
        int m = 10, n = 50;
        int result = calculatePrimeSum(m, n);
        System.out.println(result);
    }
}
def is_prime(num):
    if num <= 1:
        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):
    prime_sum = sum(i for i in range(m, n + 1) if is_prime(i))
    return prime_sum

m, n = 10, 50
result = calculate_prime_sum(m, n)
print(result)

 

  1. Problem Statement: Digit Sum Difference

Implement the function:

Int DigitSumDifference(int m, int n);

Calculate and return the absolute difference between the sum of digits of numbers divisible by 4 and the sum of digits of numbers divisible by 7, in the range from ‘m’ to ‘n’ (inclusive).

Note: 0 < m <= n

 

Example:

Input:

  

Output:

m : 50

  

2

n : 120

   

 

Solution : 

#include <stdio.h>

int digit_sum_difference(int m, int n) {
    int sum_divisible_by_4 = 0;
    int sum_divisible_by_7 = 0;

    for (int i = m; i <= n; i++) {
        if (i % 4 == 0) {
            int num = i;
            while (num > 0) {
                sum_divisible_by_4 += num % 10;
                num /= 10;
            }
        } else if (i % 7 == 0) {
            int num = i;
            while (num > 0) {
                sum_divisible_by_7 += num % 10;
                num /= 10;
            }
        }
    }

    return abs(sum_divisible_by_4 - sum_divisible_by_7);
}

int main() {
    int m = 50;
    int n = 120;
    int result = digit_sum_difference(m, n);
    printf("%d\n", result);

    return 0;
}
#include <iostream>

using namespace std;

int digit_sum_difference(int m, int n) {
    int sum_divisible_by_4 = 0;
    int sum_divisible_by_7 = 0;

    for (int i = m; i <= n; i++) {
        if (i % 4 == 0) {
            int num = i;
            while (num > 0) {
                sum_divisible_by_4 += num % 10;
                num /= 10;
            }
        } else if (i % 7 == 0) {
            int num = i;
            while (num > 0) {
                sum_divisible_by_7 += num % 10;
                num /= 10;
            }
        }
    }

    return abs(sum_divisible_by_4 - sum_divisible_by_7);
}

int main() {
    int m = 50;
    int n = 120;
    int result = digit_sum_difference(m, n);
    cout << result << endl;

    return 0;
}
public class DigitSumDifference {
    public static int digitSumDifference(int m, int n) {
        int sumDivisibleBy4 = 0;
        int sumDivisibleBy7 = 0;

        for (int i = m; i <= n; i++) {
            if (i % 4 == 0) {
                int num = i;
                while (num > 0) {
                    sumDivisibleBy4 += num % 10;
                    num /= 10;
                }
            } else if (i % 7 == 0) {
                int num = i;
                while (num > 0) {
                    sumDivisibleBy7 += num % 10;
                    num /= 10;
                }
            }
        }

        return Math.abs(sumDivisibleBy4 - sumDivisibleBy7);
    }

    public static void main(String[] args) {
        int m = 50;
        int n = 120;
        int result = digitSumDifference(m, n);
        System.out.println(result);
    }
}
def digit_sum_difference(m, n):
    sum_divisible_by_4 = 0
    sum_divisible_by_7 = 0

    for i in range(m, n + 1):
        if i % 4 == 0:
            num = i
            while num > 0:
                sum_divisible_by_4 += num % 10
                num //= 10
        elif i % 7 == 0:
            num = i
            while num > 0:
                sum_divisible_by_7 += num % 10
                num //= 10

    return abs(sum_divisible_by_4 - sum_divisible_by_7)

m = 50
n = 120
result = digit_sum_difference(m, n)
print(result)

 

  1. Problem Statement: Fibonacci Sum

Implement the function:

Int FibonacciSum(int m, int n);

Calculate and return the sum of Fibonacci numbers in the range from ‘m’ to ‘n’ (inclusive).

Note: 0 < m <= n

Example:

Input:

  

Output:

m : 5

  

52

n : 20

   

 

Solution :

#include <stdio.h>

int fibonacciSum(int m, int n) {
    int a = 0, b = 1, temp, sum = 0;

    while (b <= n) {
        if (b >= m) {
            sum += b;
        }
        temp = a + b;
        a = b;
        b = temp;
    }

    return sum;
}

int main() {
    int m = 5, n = 20;
    int result = fibonacciSum(m, n);
    printf("%d\n", result);

    return 0;
}
#include <iostream>

using namespace std;

int fibonacciSum(int m, int n) {
    int a = 0, b = 1, temp, sum = 0;

    while (b <= n) {
        if (b >= m) {
            sum += b;
        }
        temp = a + b;
        a = b;
        b = temp;
    }

    return sum;
}

int main() {
    int m = 5, n = 20;
    int result = fibonacciSum(m, n);
    cout << result << endl;

    return 0;
}
public class FibonacciSum {
    public static int fibonacciSum(int m, int n) {
        int a = 0, b = 1, temp, sum = 0;

        while (b <= n) {
            if (b >= m) {
                sum += b;
            }
            temp = a + b;
            a = b;
            b = temp;
        }

        return sum;
    }

    public static void main(String[] args) {
        int m = 5, n = 20;
        int result = fibonacciSum(m, n);
        System.out.println(result);
    }
}
def fibonacci_sum(m, n):
    a, b, total = 0, 1, 0

    while b <= n:
        if b >= m:
            total += b
        a, b = b, a + b

    return total

m, n = 5, 20
result = fibonacci_sum(m, n)
print(result)

 

  1. Problem Statement: Reverse and Add

Implement the function:

Int ReverseAndAdd(int m, int n);

Calculate and return the sum of numbers obtained by reversing the digits of each number in the range from ‘m’ to ‘n’ (inclusive).

Note: 0 < m <= n

Example:

Input:

  

Output:

m : 21

  

288

n : 35

   

 

Solution : 

#include <stdio.h>

int reverseAndAdd(int m, int n) {
    int sum = 0;
    for (int i = m; i <= n; i++) {
        int reversedNum = 0;
        int temp = i;
        while (temp > 0) {
            reversedNum = reversedNum * 10 + temp % 10;
            temp /= 10;
        }
        sum += reversedNum + i;
    }
    return sum;
}

int main() {
    int m = 21;
    int n = 35;
    int result = reverseAndAdd(m, n);
    printf("%d\n", result);

    return 0;
}
#include <iostream>

using namespace std;

int reverseAndAdd(int m, int n) {
    int sum = 0;
    for (int i = m; i <= n; i++) {
        int reversedNum = 0;
        int temp = i;
        while (temp > 0) {
            reversedNum = reversedNum * 10 + temp % 10;
            temp /= 10;
        }
        sum += reversedNum + i;
    }
    return sum;
}

int main() {
    int m = 21;
    int n = 35;
    int result = reverseAndAdd(m, n);
    cout << result << endl;

    return 0;
}
public class ReverseAndAdd {
    public static int reverseAndAdd(int m, int n) {
        int sum = 0;
        for (int i = m; i <= n; i++) {
            int reversedNum = 0;
            int temp = i;
            while (temp > 0) {
                reversedNum = reversedNum * 10 + temp % 10;
                temp /= 10;
            }
            sum += reversedNum + i;
        }
        return sum;
    }

    public static void main(String[] args) {
        int m = 21;
        int n = 35;
        int result = reverseAndAdd(m, n);
        System.out.println(result);
    }
}
def reverse_and_add(m, n):
    total_sum = 0
    for i in range(m, n + 1):
        reversed_num = int(str(i)[::-1])
        total_sum += reversed_num + i
    return total_sum

m = 21
n = 35
result = reverse_and_add(m, n)
print(result)

 

 

  1. Problem Statement: Square Root Difference

Implement the function:

Int SquareRootDifference(int m, int n);

Calculate and return the difference between the sum of square roots of even numbers and the sum of square roots of odd numbers in the range from ‘m’ to ‘n’ (inclusive).

Note: 0 < m <= n


Example:

Input:

  

Output:

m : 1

  

2.29416

n : 10

   

 

Solution : 

#include <stdio.h>
#include <math.h>

double squareRootDifference(int m, int n) {
    double evenSum = 0, oddSum = 0;

    for (int i = m; i <= n; ++i) {
        double squareRoot = sqrt(i);
        if (i % 2 == 0) {
            evenSum += squareRoot;
        } else {
            oddSum += squareRoot;
        }
    }

    return evenSum - oddSum;
}

int main() {
    int m = 1, n = 10;
    double result = squareRootDifference(m, n);
    printf("%.5lf\n", result);

    return 0;
}
#include <iostream>
#include <cmath>

using namespace std;

double squareRootDifference(int m, int n) {
    double evenSum = 0, oddSum = 0;

    for (int i = m; i <= n; ++i) {
        double squareRoot = sqrt(i);
        (i % 2 == 0) ? evenSum += squareRoot : oddSum += squareRoot;
    }

    return evenSum - oddSum;
}

int main() {
    int m = 1, n = 10;
    double result = squareRootDifference(m, n);
    cout << fixed << result << endl;

    return 0;
}
public class SquareRootDifference {
    public static double squareRootDifference(int m, int n) {
        double evenSum = 0, oddSum = 0;

        for (int i = m; i <= n; ++i) {
            double squareRoot = Math.sqrt(i);
            if (i % 2 == 0) {
                evenSum += squareRoot;
            } else {
                oddSum += squareRoot;
            }
        }

        return evenSum - oddSum;
    }

    public static void main(String[] args) {
        int m = 1, n = 10;
        double result = squareRootDifference(m, n);
        System.out.printf("%.5f\n", result);
    }
}
import math

def square_root_difference(m, n):
    even_sum = sum(math.sqrt(i) for i in range(m, n + 1) if i % 2 == 0)
    odd_sum = sum(math.sqrt(i) for i in range(m, n + 1) if i % 2 != 0)

    return even_sum - odd_sum

m, n = 1, 10
result = square_root_difference(m, n)
print(f"{result:.5f}")

 

Follow on Fb for more updates

Accenture Coding Questions with solutions (Faq)

Does Accenture ask coding questions?

Accenture does ask coding questions during the interview process for the specific roles. They ask the questions to assess the candidate’s problem-solving skills, coding proficiency, and ability to write clean and efficient code.

Are Accenture coding questions repeated?

Accenture does not repeat the coding questions from previous interviews as they keep updating the questions to ensure the problems are relevant with today’s trends and challenging for the candidates. Although similar questions could’ve been included for the Accenture coding round.

How many coding questions are asked in an accenture interview?

There are 2 questions asked in the Accenture coding round. All Accenture Coding Questions should be solved and the candidate is required to use algorithms, data structures, and design patterns.

Conclusion

We hope you liked this post and grab free Accenture Coding Questions pdf download and receive valuable information from this. Let us know what you think is the hardest part of an Accenture interview process. 


Accenture coding questions and answers pdf

Download for FREE