The coding section of Accenture contains  2-3 questions with a time limit of 45 minutes, this is the third section of the test and mostly says that it has a medium difficulty level.

This is the list of coding questions which we are going to cover in this article.

#1 Coding Question (SmallLargeSum)

Write a function SmallLargeSum(array) which accepts the array as a parameter/argument,
which performs the addition of the second largest element from even location with
second largest element from odd location.

Rules:

  • All array elements are unique.
  • If the array is empty, then return 0.
  • If the length of the array is 3 or less than 3, then return 0.

Sample Test Cases #1:

INPUT:

6

3 2 1 7 5 4

OUTPUT:

7

Explanation:

  • Second largest element among the even locations (1 3 5) is 3.
  • Second largest element among the odd locations (2 4 7) is 4.
  • Hence, 3 + 4 = 7

Sample Test Cases #2:

INPUT:

7

1 8 0 2 3 5 6

OUTPUT:

6

Solution:

LOGIC:

  • Firstly, we have to take input from the user in the main function.
  • Next steps is create two different arrays, where  
    • One is for even location elements.
     
    • Another one is for odd location elements.
       
  • Then, we have to sort both arrays, so that we can get the second largest element.
  • At last, we have to add them and return and then print it from the main function.

Program:

#include <bits/stdc++.h>

using namespace std;

int smallLargeSum(int *arr, int n) {

   if(n <= 3) {

      return 0;

   }

   //Here we use vector because we don’t know
the
array size, 

   //we can use array also but vector gives us more
functionality than array

   vector<int> arrEven, arrOdd;

   //Break array into two different arrays even and
odd

   for(int i = 0; i < n; i++) {

      //If Number is even then add it into even
array

      if(i % 2 == 0) {

         arrEven.push_back(arr[i]);

      }

      else {

         arrOdd.push_back(arr[i]);

     }

   }

   //Sort the even array

   sort(arrEven.begin(), arrEven.end());

   //We use sort function from C++ STL library

   //Sort the odd array

   sort(arrOdd.begin(), arrOdd.end());

   //Taking second largest element from both arrays
and
add them

   return arrEven[1] + arrOdd[1];

}

// Driver code

int main()

{

   int n;

   cin>>n;

   int arr[n];

   //Get input from user

   for(int i = 0; i < n; i++) {

      cin>>arr[i];

   }

   cout<<smallLargeSum(arr, n);

   return 0;

}

#2 Coding Question (CheckPassword)

Write a function CheckPassword(str) which accepts the string as a parameter/argument,
which validates the password and returns 1 if satisfied all given conditions else 0.

Password is valid if it satisfies below conditions:

  1. At least 4 characters in it.
  1. At least one numeric digit in it. 
  1. At least one Capital letter in it.
  1. Must not have space or slash (/).
  1. Starting character must not be a number.

Sample Test Cases #1:

INPUT:

bB1_89

OUTPUT:

1

Program:

#include<iostream>

#include<string.h>

using namespace std;

int CheckPassword(char str[]) {

int len = strlen(str);

bool isDigit = false, isCap = false,
isSlashSpace=false,isNumStart=false;

//RULE 1: At least 4 characters in it

if (len < 4)

return 0;

for(int i=0; i<len; i++) {

//RULE 2: At least one numeric digit in it

if(str[i]>=’0′ && str[i]<=’9′)
{

isDigit = true;

}

//RULE 3: At least one Capital
letter 

else
if(str[i]>=’A’&&str[i]<=’Z’){

isCap=true;

}

//RULE 4: Must not have space or slash

if(str[i]==’ ‘|| str[i]==’/’)

isSlashSpace=true;

}

//RULE 5: Starting character must not be a
number

isNumStart = (str[0]>=’0′ &&
str[0]<=’9′);

//FYI: In C++, if int data type function returns
the true then it prints 1 and if false then it prints 0

return isDigit && isCap && !isSlashSpace
&& !isNumStart;

}

int main() {

char password[100];

cin>>password;

cout<<CheckPassword(password);

}

#3 Coding Question (CalculateBinaryOperations)

Write a function CalculateBinaryOperations(str) which accepts the string as a
parameter/argument, the string contains the binary numbers with their operators(AND, OR &
XOR).

  • ‘A’ means AND operation 
  • ‘B’ means OR operation 
  • ‘C’ means XOR operation 

You need to calculate the string, by scanning it from left to right and taking one operator at a
time and returning output.

Rules:

  • Operator priority is not required.
  • The string length is always odd.
  • If string is null then return -1.

Sample Test Cases #1:

INPUT:

ICOCICIAOBI 

OUTPUT:

1

Explanation:

The input string is 1 XOR 0 XOR 1 XOR 1 AND 0 OR 1.

Calculate this string without any operator priority, simply scan char from left to right,
calculate result and return output.

Solution:

Note: This will convert the character into the number (char – ‘0’) in c++.

Program:

#include <bits/stdc++.h>

using namespace std;

int CalculateBinaryOperations(char* str)

{

   int len = strlen(str);

   //Let’s consider the first element as a
answer
(because string can be a single char)

   int ans = str[0]-‘0’;

   for(int i=1; i<len-1; i+=2)

   {

      int j=i+1;

      //Performing operation for AND

      if(str[i]==’A’)

      {

       ans = ans & (str[j]-‘0’);

      }

      //Performing operation for OR

      else if(str[i]==’B’)

      {

         ans = ans | (str[j]-‘0’);

      }

      //Performing operation for XOR

      else if(str[i]==’C’)

      {

         ans = ans ^ (str[j]-‘0’);

      }

   }

   return ans;

}

int main()

{

   char str[100];

   cin>>str;

   cout<<CalculateBinaryOperations(str);

}

#4 Coding Question (FindMaxInArray)

Write a function FindMaxInArray, which finds the largest number in an array with its
index.

The largest number and its index should be printed in separate lines.

Sample Test Cases #1:

INPUT:

35 42 67 57 11 12 45 94 55 87 

OUTPUT:

94 

7

Program:

#include<iostream>

using namespace std;

void FindMaxInArray(int arr[],int length)

{

   int max=-1, maxIdx=-1;

   for(int i = 0;i < length; i++)

   {

      if(arr[i] > max)

      {

         max = arr[i];

         maxIdx = i;

      }

   }

   cout<<max<<endl<<maxIdx;

}

int main()

{

   int n;

   cin>>n;

   int a[n];

   for(int i=0;i<n;i++)

      cin>>a[i];

   FindMaxInArray(a,n);

}

#5 Coding Question (OperationChoices)

Write a function OperationChoices(c, a, b) which accepts the three integers as an
argument.

The function will returns:

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

Sample Test Cases #1:

INPUT:

2

15

20

OUTPUT:

-4

Here the value of c is 2, so it performs subtraction (15 – 20) and returns -4.

Program:

#include<iostream>

using namespace std;

int operationChoices(int c, int a , int b)

{

if(c==1)

{

return a + b;

}

else if(c==2)

{

return a – b;

}

else if(c==3)

{

return a * b;

}

else if(c==4)

{

return a / b;

}

}

int main()

{

int x, y, z;

int result;

cin>>x;

cin>>y;

cin>>z;

result = operationChoices(x, y, z);

printf(“%d”,result);

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here