Friday 26 July 2019

Interview Coding Questions


Question:-  Given three integers x,y and z you need to find the all the possible combination of numbers having 4 at most x times, having 5 at most y times and having 6 at most z times as a digit.
 Example:-
input x=1,y=1,z=1
output

4
5
6
45
54
56
65
46
64
456
465
546
564
645
654


Solution :-  Please note code is in c.

/******************************************************************************
 Given three integers x,y and z you need to find the all the possible combination of numbers having 4 at most x times, having 5 at most y times and having 6 at most z times as a digit.
 Example:-
input x=1,y=1,z=1
output

4
5
6
45
54
56
65
46
64
456
465
546
564
645
654


*******************************************************************************/

#include
#include
#define BASE 10
int main()
{
    int x,y,z,j,t;
    long long num, n;
    int i, lastDigit;
    int freq[BASE];
    printf("Enter frequncy of x y and  z");
    scanf("%d %d %d", &x,&y,&z);
    t=x+y+z;

    for(j=1;j<(6*pow(10,t));j++)
    {
    /* Input number from user */
   // printf("Enter any number: ");
    //scanf("%lld", &num);
    
    /* Initialize frequency array with 0 */
    for(i=0; i
    {
        freq[i] = -10;
       // printf("%d",freq[i]);
    }

    /* Copy the value of 'num' to 'n' */
    num = j;
    n = num; 

    /* Run till 'n' is not equal to zero */
    while(n != 0)
    {
        /* Get last digit */
        lastDigit = n % 10;

        /* Remove last digit */
        n /= 10;

        /* Increment frequency array */
        if(lastDigit==4 || lastDigit==5 || lastDigit==6)
        {
                if(freq[lastDigit]==-10)
                    freq[lastDigit] = -1;
                freq[lastDigit]++;
        }
        else
        {
                num=-1;
                break;
        }
    }
    if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0)
        printf(" %d\n",num);
    }
        
    
    return 0;
}

Tuesday 29 September 2015


Problem Statement
The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:
  1. If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.
  2. If the book is returned in the same month as the expected return date, Fine = 15 Hackos × Number of late days
  3. If the book is not returned in the same month but in the same year as the expected return date, Fine = 500 Hackos × Number of late months
  4. If the book is not returned in the same year, the fine is fixed at 10000 Hackos.
Input Format
You are given the actual and the expected return dates in D M Y format respectively. There are two lines of input. The first line contains the D M Y values for the actual return date and the next line contains the D M Y values for the expected return date.
Constraints 
1D31 
1M12 
1Y3000
Output Format
Output a single value equal to the fine.
Sample Input
9 6 2015
6 6 2015
Sample Output
45
Explanation
Since the actual date is 3 days later than expected, fine is calculated as 15×3=45 Hackos.

Solution:-
 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
 int AD,ED,AM,EM,AY,EY;
    int D,M,Y,Fine=0;
    scanf("%d %d %d",&AD,&AM,&AY);
    scanf("%d %d %d",&ED,&EM,&EY);
    if((1<=AD<=31 && 1<=AM<=12 && 1<=AY<=3000)||(1<=ED<=31 && 1<=EM<=12 && 1<=EY<=3000))
    {

    Y=AY-EY;
    M=AM-EM;
    D=AD-ED;
    if(Y<0)
    {

        printf("0");
        return 0;
    }
      if(Y==0 && M<0)
    {

        printf("0");
        return 0;
    }
      if(Y==0 && M<=0 && D<0)
    {

        printf("0");
        return 0;
    }
    if(Y>0)
    {
        printf("10000");
        return 0;
    }
    if(M==0)
    {
        printf("%d",(D*15));
        return 0;
    }
    if(Y==0 && M>0)
    {
        printf("%d",(M*500));
    }
    }

    return 0;
}

Problem Statement
You are given time in AM/PM format. Convert this into a 24 hour format.
Note Midnight is 12:00:00AM or 00:00:00 and 12 Noon is 12:00:00PM.
Input Format
Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM
where
01hh12
00mm59
00ss59
Output Format
You need to print the time in 24 hour format i.e. hh:mm:ss
where
00hh23
00mm59
00ss59
Sample Input
07:05:45PM
Sample Output
19:05:45

Solution:-

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

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    char str[100];
    const char s[2]=":";
    char ans[10],ans1[10],ans3[10];
    char ss[10];
    char read;
    char *h;
    int arr[2],i=0;
    scanf("%s",str);
    // printf("%s",str);

    h=strtok(str,&s);
    //printf("%s",h);
    while(h!=NULL)
    {
        if(i==2)
        {
            strcpy(ss,h);
            //printf("%s",ss);
            read=ss[2];
            ss[2]='\0';
            strcpy(ans3,ss);
            arr[i]=atoi(ss);
         //   printf("%d\n",arr[i]);

        }
        else{

       // printf("%d\n",arr[i]);
      if(i==0)
      {
           strcpy(ans,h);

      }
      if(i==1)
      {
           strcpy(ans1,h);
       //printf("%s",ans);
      }
       arr[i]=atoi(h);
     //  ans++;
        }
        i++;
        h=strtok(NULL,&s);
    }
    if(read=='A'&&arr[0]==12)
    {
        arr[0]=00;
        strcpy(ans,"00");
    }
    if(read=='P')
    {
        if(arr[0]!=12)
        arr[0]=arr[0]+12;
    }
    if((1<=arr[0]<=12)&&(0<=arr[1]<=59)&&(0<=arr[2]<=59))
    {
        if(read=='P')
        {
            if(arr[0]==12)
                   printf("%s:",ans);
            else
                   printf("%d:",arr[0]);
    printf("%s:",ans1);
    printf("%s\n",ans3);
        }
    else
    {
        printf("%s:",ans);
            printf("%s:",ans1);
    printf("%s",ans3);
    }
    }
   // int hh=atoi()
    return 0;
}