0% found this document useful (0 votes)
15 views24 pages

20 Coding Q Ans A

The document contains a series of programming problems and their solutions, primarily focused on algorithms and data structures. Each problem includes a description, example inputs and outputs, and a Java implementation. Topics covered include chocolate distribution, parking lot management, string transformations, equilibrium points in arrays, and more.

Uploaded by

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

20 Coding Q Ans A

The document contains a series of programming problems and their solutions, primarily focused on algorithms and data structures. Each problem includes a description, example inputs and outputs, and a Java implementation. Topics covered include chocolate distribution, parking lot management, string transformations, equilibrium points in arrays, and more.

Uploaded by

jeet biswas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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

Input-(n=5),(arr=10,4,12,3,1)

Output-3

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);

}
2. 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

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);

3. 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.

Input: Output:

str: abaabbcc bbbbaaac

Example:

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));

4. 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

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));

5. 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 34512

arr: 1 2 3 4 5

d: 3

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] + " ");

6. 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”

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));

7. 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:
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");

8. 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:

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);

9. 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 An array of two integers representing the pair (a, b) or -1 if


integers 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 :

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);

10. 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 :

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));

}
11. 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:

The modified string str where all occurrences of ch1 are


A string str
replaced with ch2 and vice versa

A character
ch1

A character
ch2

Example:

Input: Output:

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

Solution :

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);

12. 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 The minimum value and its index, separated by a newline


array character

Example:

Input: Output:

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

Solution :
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);

13. 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 The average of all positive numbers, or -1 if there are no


array positive numbers

Example:

Input: Output:

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

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);

}
14. 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 :

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);

15. 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 :

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");

16. 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 :

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);

}
17. 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 :

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);

18. 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 :

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);

19. 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 :

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);

20. 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 :

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);

You might also like