Solved Hackerrank Questions in C, C++, Java etc | Hackerrank Coding Questions

In this article, we will be discussing some of the important Hackerrank questions and challenges asked in various product company recruitments. If you are someone interested in programming, then this list of important Hackerrank questions is a must to solve.

Hackerrank Questions with Solutions

Sample Hackerrank Questions: Bank Compare

There are two banks; Bank A and Bank B. Their interest rates vary. You have received offers from both banks in terms of the annual rate of interest, tenure, and variations of the rate of interest over the entire tenure.

You have to choose the offer which costs you least interest and reject the other. Do the computation and make a wise choice.

The loan repayment happens at a monthly frequency and Equated Monthly Installment (EMI) is calculated using the formula given below :

EMI = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 + monthlyInterestRate)^(numberOfYears * 12))

Constraints:

1 <= P <= 1000000
1 <=T <= 50
1<= N1 <= 30
1<= N2 <= 30

Input Format:

  • First line : P – principal (Loan Amount)
  • Second line : T – Total Tenure (in years).
  • Third Line: N1 is the number of slabs of interest rates for a given period by Bank A. First slab starts from the first year and the second slab starts from the end of the first slab and so on.
  • Next N1 line will contain the interest rate and their period.
  • After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
  • Next N2 lines are the number of slabs of interest rates for a given period by Bank B. The first slab starts from the first year and the second slab starts from the end of the first slab and so on.
  • The period and rate will be delimited by single white space.

Output Format: Your decision – either Bank A or Bank B.

Explanation:

Sample Input 1:

10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9

Sample Ouput 1:

Bank B

Sample Input 2:

500000
26
3
13  9.5
3  6.9
10  5.6
3
14  8.5
6  7.4
6  9.6

Sample Output 2:

Bank A

Solution:

#include <stdio.h>
#include<math.h>
int main() {
 double p,s,mi,sum,emi,bank[5],sq;
 int y,n,k,i,yrs,l=0;
    scanf("%lf",&p);
 scanf("%d",&y);
 for(k=0;k<2;k++)
 {
 scanf("%d",&n);
 sum=0;
 for(i=0;i<n;i++)
 {
     scanf("%d",&yrs);
     scanf("%lf",&s);
     mi=0;
     sq=pow((1+s),yrs*12);
     emi= (p*(s))/(1-1/sq);
     sum= sum + emi;
 }
 
 bank[l++]=sum;
 }
 if(bank[0]<bank[1])
 printf("Bank A");
 
 else
 printf("Bank B");
 return 0;
}

Sample Hackerrank Questions: One Egg

“One Egg” is an egg supply company which supplies eggs to retailers. They have M classes of eggs. Each class can have N number of eggs (N can be the same or can vary class to class).  They accept order via mail for X eggs. In response, they confirm if they can supply the eggs with a “Thank you” note and the number of eggs or with a “Sorry” note and the numbers of eggs they can supply. They also mention the breakdown of eggs by the class they will supply. The ordered eggs are adjusted against the different classes with the most number of eggs adjusted first then the balance is adjusted against the second-highest and so on.   The company is a bit superstitious as well. If the number of eggs ordered is greater than or equal to the total number of eggs in stock then they retain one egg and responds back with the “Sorry” note with total number of eggs in stock minus one and breakdown of eggs by class. 

Note: If the classes have the same number of eggs then class entered first should be selected to adjust.

Input Format:

First line contains two space-separated integers denoting the respective values of M (the number of classes of eggs) and X, the number of eggs ordered  The following M lines contain an integer each indicating the number of eggs available in each class

Output Format:

First line should be, if X is less than total number of Eggs then Print  ” Thank you, your order for X eggs is accepted”  Else if X is greater than or equal to total number of Eggs then print ”  ” Sorry, we can only supply (total number of Eggs in stock -1) eggs”  T hen M lines with 3 columns:  First column – Number of eggs available in each class  Second column – Eggs allocated against each class for that order  Third column – Balance Eggs against each class

Constraints:

1 ≤ M ≤ 20  N ≥ 1  X ≥ 1

Sample Input 1: 

5 150 
50 
15 
80 
10 
5  

Sample Output 1:

Thank you, your order for 150 eggs are accepted 
50        50        0 
15        15        0                     
80        80        0 
10        5         5         
5         0         5  

Explanation:

Total order of 150 eggs is less than the total number of Eggs 50+15+80+10+5 = 160. Hence the Thank you message.  150 was first adjusted against Class with the first highest number of eggs 80. Balance of 150-80 = 70 was adjusted against the second highest class of 50. Balance of 70-50 = 20 then adjusted against 15. Balance of 20-15 = 5 then adjusted against 10 leaving behind 5 eggs in that class.

Sample Input 2:

4 250 
80 
50 
70 
20 

Sample Output 2:

Sorry, we can only supply 219 eggs 
80        80        0 
50        50        0                     
70        70        0 
20        19        1          

Explanation:

The total order of 250 eggs was greater than the total number of eggs 80+50+70+20 = 220. Hence the sorry message.  250 was first adjusted against Class with the first highest number of eggs 80. Balance of 250-80 = 170 was adjusted against the second highest class of 70.   Balance of 170-70 = 100 was then adjusted against 50. Balance of 100-50 = 50 then adjusted against 20. Since Balance is greater than the last class of egg all but one egg is left in that last class.

Solution:

#include <stdio.h>
int main() {
int m,x,i,a[1000],sum=0,s;
scanf("%d %d",&m,&x);
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
if(sum>x)
    printf("Thank you, your order for %d eggs are accepted\n",x);
else
{
    printf("Sorry, we can only supply %d eggs\n",sum-1);
    x=sum-1;
}
for(i=0;i<m;i++)
{
    if(x>=a[i])
    {
        printf("%d\t%d\t%d\n",a[i],a[i],0);
        x=x-a[i];
    }
    else if(x<a[i])
    {
        s=a[i]-x;
     printf("%d\t%d\t%d\n",a[i],x,s);
     x=0;
     }
    
     else if(x==0)
     printf("%d\t%d\t%d\n",a[i],0,a[i]);
   
}
 return 0;
}

Sample Hackerrank Questions: Milk Man and His Bottles

A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply the desired quantity using as fewer bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand for milk.

Input Format:

  • The first line contains the number of test cases N
  • Next N lines, each contains a positive integer Li which corresponds to the demand of milk.

Output Format:

For each input Li, print the minimum number of bottles required to fulfill the demand

Constraints:

1 <= N <= 1000 Li > 0 1 <= i <= N

Sample Input and Output:

2
17
65        
2     7 

Explanation:

Number of test cases is 2
For 17 = 10*1 + 7*1 = 2
For 65 = 10*6 + 5*1 = 7

Few more examples:
For 99 = 10*9 + 7*1 + 1*2 = 12
For 63 = 10*6 + 1*3 =9

Solution:

#include <stdio.h>
int main()
{
 int n,b=0,i,m,s=0;
 scanf("%d",&m);
 for( i=0;i<m;i++)
 {
 scanf("%d",&n);
 b=n/10;
 n=n%10;
 s=s+b;
 b=0;
 b=n/7;
 n=n%7;             
 s=s+b;
 b=0;
 b=n/5;
 n=n%5;
 s=s+b;
 b=0;
 b=n/1;
 s=s+b;
 printf("%d\n",s);
 s=0;
 b=0;
 }
 return 0;
}

Sample Hackerrank Questions: Bride Hunting

Sam is an eligible bachelor. He decides to settle down in life and start a family. He goes bride hunting. He wants to marry a girl who has at least one of the 8 qualities mentioned below:-

1) The girl should be rich.
2) The girl should be an Engineer/Doctor.
3) The girl should be beautiful.
4) The girl should be of height 5.3″.
5) The girl should be working in an MNC.
6) The girl should be an extrovert.
7) The girl should not have spectacles.
8) The girl should be kind and honest.

He is in search of a bride who has some or all of the 8 qualities mentioned above. On bride hunting, he may find more than one contenders to be his wife.

In that case, he wants to choose a girl whose house is closest to his house. Find a bride for Sam who has maximum qualities. If in case, there are more than one contenders who are at equal distance from Sam’’s house; then

print ““Polygamy not allowed””.

In case there is no suitable girl who fits the criteria then print “”No suitable girl found””

Given a Matrix N*M, Sam’s house is at (1, 1). It is denoted by 1. In the same matrix, the location of a marriageable Girl is also denoted by 1. Hence 1 at location (1, 1) should not be considered as the location of a marriageable Girl’s location.

The qualities of that girl, as per Sam’’s criteria, have to be decoded from the number of non-zero neighbors (max 8-way) she has. Similar to the condition above, 1 at location (1, 1) should not be considered as the quality of a Girl. See Example section to get a better understanding.

Find Sam, a suitable Bride and print the row and column of the bride, and find out the number of qualities that the Bride possesses.

NOTE: Distance is calculated in number of hops in any direction i.e. (Left, Right, Up, Down and Diagonal)

Constraints:

2 <= N,M <= 10^2

Input Format:

  • First Line contains the row (N) and column (M) of the houses.
  • Next N lines contain the data about girls and their qualities.

Output Format:

It will contain the row and column of the bride, and the number of qualities that Bride possess separated by a colon (i.e. :).

Sample Input 1:

2 9
1 0 1 1 0 1 1 1 1
0 0 0 1 0 1 0 0 1

Sample Output 1:

1:7:3

Explanation:

The girl and qualities are present at (1,3),(1,4),(1,6),(1,7),(1,8),(1,9),(2,4),(2,6),(2,9).
The girl present at (1,3) has 2 qualities (i.e. (1,4)and (2,4)).
The girl present at (1,4) has 2 qualities.
The Bride present at (1,6) has 2 qualities.
The Bride present at (1,7) has 3 qualities.
The Bride present at (1,8) has 3 qualities.
The Bride present at (1,9) has 2 qualities.
The Bride present at (2,4) has 2 qualities.
The Bride present at (2,6) has 2 qualities.
The Bride present at (2,9) has 2 qualities.
As we see, there are two contenders who have maximum qualities, one is at (1,7) and another at (1,8).
The girl who is closest to Sam’s house is at (1,7). Hence, she is the bride.
Hence, the output will be 1:7:3.

Sample Input 2:

6 6
1 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 0 0 0 0

Sample Output 2:

4:4:8

Explanation:

The bride and qualities are present at (3,3),(3,4),(3,5),(4,3),(4,4),(4,5),(5,3),(5,4),(5,5)
The Bride present at (3,3) has 3 qualities (i.e. (3,4),(4,3) and (4,4)).
The Bride present at (3,4) has 5 qualities.
The Bride present at (3,5) has 3 qualities.
The Bride present at (4,3) has 5 qualities.
The Bride present at (4,4) has 8 qualities.
The Bride present at (4,5) has 5 qualities.
The Bride present at (5,3) has 3 qualities.
The Bride present at (5,4) has 5 qualities.
The Bride present at (5,5) has 3 qualities.
As we see, the girl present in (4,4) has the maximum number of Qualities. Hence, she is the bride.
Hence, the output will be 4:4:8.

Solution:

#include<stdio.h>
int main()
{
    int n,m,i,g[50][50],j,p,q,max=0,cnt=0,k=1,c=0,u=1,x[30],y[30],t1,min=0,
    sc[50],e,f,ct=0,a[50],count=0,t2=0,t=0;
    scanf(“%d %d”,&n,&m);
    for(i=1;i<=n;i++)
    {
    for(j=1;j<=m;j++)
    {
    scanf(“%d”,&g[i][j]);
    }
    }
    g[1][1]=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
         cnt=0;
         if(g[i][j]==1)
         {
             t++;
             for(p=i-1;p<=i+1;p++)
             {
                 for(q=j-1;q<=j+1;q++)
                 {
                  
                   if(g[p][q]==1)
                   {
                       cnt++;
                   }
                 }
             }cnt=cnt-1;
             a[k]=cnt;
             k++;
             }
        }
    }
             for(k=1;k<=t;k++)
             {
                 if(a[k]>max)
                 max=a[k];
             }
             if(max==0)
             {
             printf(“No suitable girl found”);
             return 0;
             }
             for(k=1;k<=t;k++)
             {
                 if(a[k]==max)
                 c++;
             }
             for(k=1;k<=t;k++)
             {
                 t2=0;
                 if(a[k]==max)
                 {
                     for(i=1;i<=n;i++)
                     {
                         for(j=1;j<=m;j++)
                         {
                             if(g[i][j]==1)
                             t2++;
                             if(t2==k)
                             {
                                 x[u]=i;
                                 y[u]=j;
                                 u++;
                             }
                        }
                     }
                 }
             }
            t1=u-1;
            if(c==1)
            printf(“%d:%d:%d”,x[1],y[1],max);
            else
            {
                for(u=1;u<=t1;u++)
                {
                    sc[u]=sqrt(((x[u]-1) * (x[u]-1)) + ((y[u]-1)*(y[u]-1)));;
                }
                min=sc[1];
                for(u=1;u<=t1;u++)
                {
                    if(sc[u]<min)
                    min=sc[u];
                }
                for(u=1;u<=t1;u++)
                {
                    if(sc[u]==min)
                    count++;
                }
                if(count>1)
                printf(“Polygamy not allowed”);
                if(count==1)
                {
                    for(u=1;u<=t1;u++)
                    {
                        if(sc[u]==min)
                    printf(“%d:%d:%d”,x[u],y[u],max);
                }
            }
       
          }
     return 0;
}