Wednesday, 27 September 2017

To reverse the order of two words ..... if I enter "hello world" , I should get "world hello "

#include<stdio.h>
void reverse(char *begin, char *end);

void reverseWords(char *s)
{
  char *word_begin = s;
  char *temp = s;

  while( *temp )
  {
    temp++;
    if (*temp == '\0')
    {
      reverse(word_begin, temp-1);
    }
    else if(*temp == ' ')
    {
      reverse(word_begin, temp-1);
      word_begin = temp+1;
    }
  }
  reverse(s, temp-1);
}

void reverse(char *begin, char *end)
{
  char temp;
  while (begin < end)
  {
    temp = *begin;
    *begin++ = *end;
    *end-- = temp;
  }
}

int main()
{
  char *s;
  gets(s);
  char *temp = s;
  reverseWords(s);
  printf("%s", s);
  getchar();
  return 0;
}

Write a Program to Read two numbers and check whether they are Co-Primes or not Co-Prime: In number theory, two integers a and b are said to be relatively prime, mutually prime, or coprime (also spelled co-prime)[1] if the only positive integer that divides both of them is 1 Input: 2 3 Output: Co-Prime

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

int prime(int number){
    if(number==1)
    return 0;
    int dummy = (int) sqrt(number), i;
for (i = 2; i <= dummy; i++) {
if (number % i == 0) {
return 0;
}
}
return 1;
}
int main(){
    int num1,num2;
    scanf("%d %d",&num1,&num2);

    //Any even number have 2 as common divisor
    if(num1%2==0 && num2%2==0){
        printf("Not a Co-prime");
    }

    //1 is co-prime with every number
    else if(num1==1 || num2==1){
        printf("Co-prime Number");
    }

    //Any two successive numbers/ integers are always co-prime
    else if(abs(num1-num2)==1){
        printf("Co-prime Number");
    }

    //Every prime number is co-prime to each other
    else if(prime(num1)&&prime(num2)){
        printf("Co-prime Number");
    }

    else{
        printf("Not a Co-prime");
    }
}

Tuesday, 26 September 2017

Given an array with both positive and negative numbers. Find two elements such that their sum is closest to zero

#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,*arr,len,total=99999,sum,num1,num2;
scanf("%d",&len);
//Dynamically allocating memory for array
arr = (int*) malloc(len * sizeof(int));
for(i=0;i<len;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<len;i++){
for(j=0;j<len;j++){
if(i==j)
break;
if(abs(arr[i]+arr[j])<abs(total)){
total = arr[i]+arr[j];
num1=arr[i];
num2=arr[j];
}
}
}
printf("sum = %d\nNumbers are %d %d",total,num1,num2);
}

Given an array with both positive and negative numbers. Find two elements such that their sum is closest to zero

#include<stdio.h>
#include<stdlib.h>
int main(){
int i,j,*arr,len,total=99999,sum,num1,num2;
scanf("%d",&len);
//Dynamically allocating memory for array
arr = (int*) malloc(len * sizeof(int));
for(i=0;i<len;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<len;i++){
for(j=0;j<len;j++){
if(i==j)
break;
if(abs(arr[i]+arr[j])<abs(total)){
total = arr[i]+arr[j];
num1=arr[i];
num2=arr[j];
}
}
}
printf("sum = %d\nNumbers are %d %d",total,num1,num2);
}

Given 2 arrays of integers a1,a2 check if a1 is a subset of a2

#include<stdio.h>
#include<stdlib.h>

int main(){
    int i,j,len1,len2;
   
    scanf("%d",&len1);
    int arr1[len1];
    for(i=0;i<len1;i++){
        scanf("%d",&arr1[i]);
    }
   
    scanf("%d",&len2);
    int arr2[len2];
    for(i=0;i<len2;i++){
        scanf("%d",&arr2[i]);
    }
    int flag=0;
    for(i=0;i<len1;i++){
        flag=0;
        for(j=0;j<len2;j++){
            if(arr1[i]==arr2[j]){
                flag=1;
                break;
            }
        }
        if(flag==0){
            printf("arr1 is not a subset of arr2");
            break;
        }
    }
    if(flag==1)
        printf("arr1 is subset of arr2");
}

Monday, 25 September 2017

Given an array of n numbers, give an algorithm to find the first element in the array which is repeated

#include<stdio.h>
#include<stdlib.h>


int main(){
    int i,*arr,len,hash[10000]={0};
    scanf("%d",&len);
    
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
    
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
        hash[arr[i]]++;
    }
    
    for(i=0;i<len;i++){
        if(hash[arr[i]]>1){
            printf("%d is the first the element in the array which is repeated",arr[i]);
            break;
        }
    }

}

Given an array filled with integers that appear exactly twice, with the exception of one integer that appears once, find the unique integer. ex:findUnique([1, 2, 6, 9, 9, 1, 3, 6, 2]) returns: 3 ex: findUnique([12, 45, 32, 65, 32, 65, 45]) returns: 12

#include<stdio.h>
#include<stdlib.h>


int main(){
    int i,*arr,len,temp,j;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
    }
   
    //Sort the array
    for (i=0;i<len;i++)
    {
        for (j=0;j<len-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
        }
    }
     
    //Traversing the array, finding the unique integer as mentioned in the question the number must
    //appear exactly twice except one unique number
    for(i=0;i<len-1;i+=2){
        if(arr[i]!=arr[i+1]){
            printf("%d is the unique number\n",arr[i]);
            break;
        }
    }
}

Integers in an array are unique and increasingly sorted. Please write a function/method to find an integer from the array which equals to its index. For example, in the array {-3, -1, 1, 3, 5}, the number 3 equals its index 3.

#include<stdio.h>
#include<stdlib.h>

int main(){
    int i,*arr,len;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++)
        scanf("%d",&arr[i]);
   
    //Traversing the array & finding the element which is equal to its index
    for(i=0;i<len;i++){
        if(arr[i]==i){
            printf("The number %d is equal to its index %d\n",arr[i],i);
        }
    }
}

There are n registration numbers in an array, some of them are repeated, write an algorithm to find the repeated numbers in time complexity of O(nlogn) or O(n)

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    int i,*arr,len,max=-1,*hash;
    scanf("%d",&len);
   
    //Dynamically allocating memory for array
    arr = (int*) malloc(len * sizeof(int));
   
    for(i=0;i<len;i++){
        scanf("%d",&arr[i]);
        // To find the largest number in array
        if(max<arr[i])
            max=arr[i];
    }
   
    //Dynamically allocating memory for hash map
    hash = (int*) malloc(max * sizeof(int));
   
    //Initialize hash array with 0
    for(i=0;i<len;i++)
    hash[arr[i]]=0;
   
    //Traversing the array and using hash map, finding the duplicates in the array
    for(i=0;i<len;i++){
        if(hash[arr[i]]>=0)
        hash[arr[i]]++;
        if(hash[arr[i]]>1){
            printf("%d is duplicated\n",arr[i]);
            hash[arr[i]]=-1;
        }
    }

Monday, 26 December 2016

Automation Software Testing

The objective of automated testing is to simplify as much of the testing effort as possible with a minimum set of scripts. If unit testing consumes a large percentage of a quality assurance (QA) team's resources, for example, then this process might be a good candidate for automation. Automated testing tools are capable of executing tests, reporting outcomes and comparing results with earlier test runs. Tests carried out with these tools can be run repeatedly, at any time of day.
The method or process being used to implement automation is called a test automation framework. Several frameworks have been implemented over the years by commercial vendors and testing organizations. Automating tests with commercial off-the-shelf (COTS) or open source software can be complicated, however, because they almost always require customization. In many organizations, automation is only implemented when it has been determined that the manual testing program is not meeting expectations and it is not possible to bring in more human testers.

What is automated software testing?

Automated software testing is an alternative to manual testing, where software tools, not human testers, execute pre-scripted tests on a software application before it is released into production.
Using automated testing tools is a bad idea if you're not yet expert at testing.
Automation tools enable testing organizations to run tests quickly and repeatedly. The tools manage test execution, report outcomes and compare results with earlier test runs.
BEST EXPLANATION

Tuesday, 19 July 2016

ZOHO INTERVIEW QUESTION rotate the matrix

#include<stdio.h>
#include<conio.h>
int N,move,n,a[100][100];
void rotate(int index){
int i,temp,temp1;
temp=a[index][index];
for(i=index+1;i<N-index;i++){
     temp1=a[index][i];
     a[index][i]=temp;
     temp=temp1;
}
for(i=index+1;i<N-index;i++){
     temp1=a[i][N-1-n];
     a[i][N-1-n]=temp;
     temp=temp1;
}
for(i=N-1-index-1;i>index;i--){
     temp1=a[N-1-n][i];
     a[N-1-n][i]=temp;
     temp=temp1;
}
for(i=N-1-index;i>index;i--){
     temp1=a[i][index];
     a[i][index]=temp;
     temp=temp1;
     printf("temp=%d",temp);
}
printf("temp=%d\n",temp);
a[index][index]=temp;
}
void main(){
int i,j,num=1;
clrscr();
scanf("%d",&N);
for(i=0;i<N;i++){
for(j=0;j<N;j++){
a[i][j]=num++;
}
}
for(i=0;i<N;i++){
for(j=0;j<N;j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}

move=N/2;
n=0;
printf("move=%d\n",move);
while(move){
for(i=0;i<move;i++){
rotate(n);
}
n++;
move--;
}
for(i=0;i<N;i++){
for(j=0;j<N;j++) {
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}


Friday, 8 July 2016

print the pattern 3 4 33 34 43 44 333 334..

#include<stdio.h>
int main(){
int n,k=1,a[10000],i;
a[0]=3,a[1]=4;
scanf("%d",&n);
for(i=0;i<n;i++){
a[++k]=(a[i]*10)+3;
a[++k]=(a[i]*10)+4;
}
printf("%d",a[n-1]);
return 0;
}

Print a given matrix in spiral form

#include <stdio.h>
#define R 3
#define C 6
void spiralPrint(int m, int n, int a[R][C])
{
    int i, k = 0, l = 0;
    /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator
    */
    while (k < m && l < n)
    {
        /* Print the first row from the remaining rows */
        for (i = l; i < n; ++i)
        {
            printf("%d ", a[k][i]);
        }
        k++;
        /* Print the last column from the remaining columns */
        for (i = k; i < m; ++i)
        {
            printf("%d ", a[i][n-1]);
        }
        n--;
        /* Print the last row from the remaining rows */
        if ( k < m)
        {
            for (i = n-1; i >= l; --i)
            {
                printf("%d ", a[m-1][i]);
            }
            m--;
        }
        /* Print the first column from the remaining columns */
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
            {
                printf("%d ", a[i][l]);
            }
            l++;   
        }       
    }
}
/* Driver program to test above functions */
int main()
{
    int a[R][C] = { {1,  2,  3,  4,  5,  6},
        {7,  8,  9,  10, 11, 12},
        {13, 14, 15, 16, 17, 18}
    };
    spiralPrint(R, C, a);
    return 0;
}