Our Feeds

Thursday 17 September 2015

AJITH KP

Some Campus Interview Questions and Solutions

Hi GuyZ,
     Here I'm sharing some campus interview questions and their answers. These questions are asked to my friend studying in College of Engineering, Thalassery.

Q. No. 1

     Given an array of numbers and sliding window size, write a program to print maximum value in each sliding window.

      Eg. Input:
            Array of Numbers: 2 3 4 2 6 2 5 1
            Size of sliding window: 3
      Output:
            4 4 6 6 6 5

Solution

#include <stdio.h>
/*

[*] Coded By Ajith Kp
[*] http://www.terminalcoders.blogspot.com

*/
int main()
{
 int arr[256], w, n, i, j;
 printf("Enter size of array: ");
 scanf("%d", &n);
 printf("Enter %d items: ", n);
 for(i=0;i<n;i++)
 {
  scanf("%d", &arr[i]);
 }
 printf("Window size: ");
 scanf("%d", &w);
 
 for(i=0;i<n-w+1;i++)
 {
  int max=arr[i];
  for(j=i;j<i+w;j++)
  {
   if(arr[j]>max)
   {
    max = arr[j];
   }
  }
  printf("%d ", max);
 }
 printf("\n");
 return 0;
}


Q. No. 2

     Given a positive value S, print all sequences with continuous numbers whose sum is S.
   
     Eg. Input:
          15
     Output:
          1 2 3 4 5
          4 5 6
          7 8
          15

Solution

#include <stdio.h>
/*

[*] Coded By Ajith Kp
[*] http://www.terminalcoders.blogspot.com

*/
#include <math.h>
int main()
{
 int n, i, j, k, x, max;
 printf("Enter number: ");
 scanf("%d", &n);
 for(i=1;i<=n;i++)
 {
  max = j = i;
  while(max<n)
  {
   max += (++j);
   //printf("%d %d\n", max, j);
  }
  if(max == n)
  {
   for(k=i;k<=j;k++)
   {
    printf("%d ", k);
   }
   printf("\n");
  }
 }
 return 0;
}

Q. No. 3

     Write a program to print all subsets of given set with sum equal to given value.

     Eg. Input:
          Set: 3 34 4 12 5 2
          Sum: 9
     Output:
          {4, 5}, {3, 4, 2}

Soultion

#include <stdio.h>
/*

[*] Coded By Ajith Kp
[*] http://www.terminalcoders.blogspot.com

*/
int main()
{
 int n, arr[256], sarr[256], x, i, j, k, ind, sum;
 printf("Enter size: ");
 scanf("%d", &n);
 printf("Enter n items: ");
 for(i=0;i<n;i++)
 {
  scanf("%d", &arr[i]);
 }
 printf("Enter sum: ");
 scanf("%d", &x);
 i=1<<n;
 while(i>0)
 {
  sum = 0;
  ind = 0;
  for(j=n-1, k=0; j>=0; j--)
  {
   if(1<<j & i)
   {
    sum+=arr[k];
    sarr[ind] = arr[k];
    ind++;
   }
   k++;
  }
  i--;
  if(sum==x)
  {
   printf("{ ");
   for(k=0;k<ind-1;k++)
   {
    printf("%d ", sarr[k]);
   }
   printf("%d }\n", sarr[ind-1]);
  }
 }
 printf("\n");
 return 0;
}

Q. No. 4

     Write a program to implement a function to verify whether two words are pair of anagrams.(If two words have the same characters and the occurrence number of each character is also identical respectively, they are anagrams)

     Eg. Input:
          silent listen
     Output:
          The given strings are Anagrams

Solution

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

[*] Coded By Ajith Kp
[*] http://www.terminalcoders.blogspot.com

*/
int main()
{
 char str1[256], str2[256], ch;
 int i, j, n;
 printf("1st word: ");
 scanf("%s", str1);
 printf("2nd word: ");
 scanf("%s", str2);
 if(strlen(str1) != strlen(str2))
 {
  printf("The given words are not Anagrams");
 }
 else
 {
  //Sort 2 words
  for(i=0;i<strlen(str1);i++)
  {
   for(j=i;j<strlen(str1);j++)
   {
    if(str1[i]>str1[j])
    {
     ch = str1[i];
     str1[i] = str1[j];
     str1[j] = ch;
    }
   }
  }
  for(i=0;i<strlen(str2);i++)
  {
   for(j=i;j<strlen(str2);j++)
   {
    if(str2[i]>str2[j])
    {
     ch = str2[i];
     str2[i] = str2[j];
     str2[j] = ch;
    }
   }
  }
  if(strcmp(str1, str2)==0)
  {
   printf("The given words are Anagrams\n");
  }
  else
  {
   printf("The given words are not Anagrams\n");
  }
 }
}