Learn and Grow

On December 22, 2021 by Flare Study   No comments

 Deloitte important coding questions asked in Assessment Test on 18 Dec 2021

Program 1:
The YETI (Yet searching for Extra Terrestrial Intelligence) is receiving a signal from a remote solar system. The message M is a string that contains alphabets integers and some special characters. The signal has to be segregated into two sequences, one with characters and another with sum of all integers present in the message. Write a program to segregate M into two sequences and print it.

Constraints: 

(1) The input does not contain any space 
(II) All the continuous digits make a single integer. 
(III) M contains at least one alphabet or special character. 

Input Format: 

The input contains the message, M 

Output Format: 

The first line of output contains a string that has everything except integers present in M 
The second line of output contains the sum of all integers present in M 

Sample Input 1:

1234Hi!Hello678Everyone@GoodByeto#all1

Sample Output 1:

Hi!HeloEveryoneGoodByeto#all
1913

Sample Explanation:

M=1234Hi!Hello678Everyone@GoodByeto#all1
Sum of Integers=1234+678+1=1913
Except String= Hi!HeloEveryoneGoodByeto#all

C:

#include<stdio.h>
#include <math.h>
#include<string.h>
int main()
{
char str[100];
int i, j,count,sum=0,exp,rev=0,l;
scanf("%[^\n]s",str);//Input string with numbers
l=strlen(str);//Find the Length of String
exp = 0;
for(i = l-1; i >= 0; i--) //Calculate the Sum of all the numbers present in the String
{
      if (isdigit(str[i])) {
           rev = str[i]-48;  // there are better ways to parse characters to int 
           rev = (int) pow(10, exp) * rev;
           sum += rev;  // only add when you see a digit 
      } else { exp = -1; } // reset back to 10^0 = 1 on next loop 

    exp++;
}
for(i = 0; str[i] != '\0'; ++i)//For Removing the Numbers from string
{
   while (!( (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '\0') )
      {
           for(j = i; str[j] != '\0'; ++j){
                str[j] = str[j+1];
            }
        str[j] = ‘\0’;
      }
}
puts(str);//print number free string
printf("%d",sum);//print sum on console screen
return 0;
}

Program 2:
Given a numerical array with n values from 0 to 9, find the smallest possible sum S (represented as a string) of two numbers created using the digits of the array elements. The two numbers together should include all the digits of the array

Constraints: 

Array size n should be greater than one 
Array elements should be greater than or equal to zero

Input Format:

I) The first line of input contains the array size n. 
II) The second line of input contains n elements separated by a single white space

Sample Input 1:

6
685423

Sample Output 1:

604

Sample Explanation:

Two numbers of sizes (n/2) and (n-n/2) should be created using digits of the array such that their addition should form the smallest possible number So the few numbers formed are 358 and 246 to generate the smallest sum of 604 in the output

C:

#include<stdio.h>
// C program to sort the array in an
// ascending order using selection sort

#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// Function to perform Selection Sort
void selectionSort(int arr[], int n)
{
int i, j, min_idx;

// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {

// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element
// with the first element
swap(&arr[min_idx], &arr[i]);
}
}

// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main()
{
int arr[] = { 6, 8, 5, 4, 2, 3};
int sum=0,num1=0,num2=0,i;
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);//Sort Array
    for(i=0;i<n;i++){//Actual Logic for this problem
      if(i%2!=0){
        num1=num1*10+arr[i];
      }else{
        num2=num2*10+arr[i];
     }
    }
     sum=num1+num2;
     printf("%d",sum);
return 0;
}