Question 1: (8 marks)

Write a program to print the pattern below. Taking n as input, print n lines that print the pattern.

Sample input 1:
3

Sample output 1:
*    *
**  **
******

Sample input 2:
5

Sample output 2:
*        *
**      **
***    ***
****  ****
**********

 

Question 2: (12 marks)

The int datatype has a limitation of maximum value. It can only store values upto a certain limit. So what we want to do is take a very big integer and add 1 to it.

 

INPUT FORMAT:

First line contains number of digits in the integer, n. This is at maximum 50.

The next line consists of all the digits of the integer, separated by spaces.

You are required to add 1 to this integer and output the digits of the resulting integer (without spaces)

 

Sample input:
12
8 7 5 1 2 5 4 8 7 5 4 5

Sample output:
875125487546

 

Question 3: (8 marks)

Leap years are an interesting way of putting together lost time over the previous 4 years. These are those years when February has 29 days. One of your friends Raj was born on 29th of February. He wishes to know how many times till date he lived through his actual birthday (29th February). Take his birth year and present year as inputs and print the number of times he has experienced 29th of February. Note if present year is a leap year, do include its 29th of February as well. You may also display an error as “Birth Year is incorrect” if the birth year entered is not a leap year.

Note: Leap years are years which are multiples of 4 with the exception of years which are divisble by 100 but not by 400.

Sample input 1:
1996 2019

Sample output 1:
6

Sample input 2:
1997 2019

Sample output 2:
Birth year is incorrect

 

Question 4: (12 marks)

Sort an array in ascending order by flipping (exchanging) 2 adjacent integers not in the correct order until there is no such pair.

The leftmost swappable pair must be swapped first, i.e. the first pair encountered while going from left to right, which is in the opposite (descending order) should be swapped. Then the whole process should be repeated.

 

INPUT FORMAT:

First line contains 1 integer N denoting the number of integers in input. Next line contains N integers separated by spaces.

 

OUTPUT FORMAT:

For each flip performed, display the array in a line separated by spaces.

Sample input:
5
10 7 6 2 5

Sample output
10 7 6 2 5
7 10 6 2 5
7 6 10 2 5
6 7 10 2 5
6 7 2 10 5
6 2 7 10 5
2 6 7 10 5
2 6 7 5 10
2 6 5 7 10
2 5 6 7 10

 

Question 5: (12 marks)

In this question we’ll deal with Fibonacci series. The series is defined on whole numbers like fib(0), fib(1),… as follows:

fib(0) = 0

fib(1) = 1

fib(n) = fib(n-1) + fib(n-2), for n >= 2

 

INPUT FORMAT:

An integer N, 0 <= N <= 1000

 

OUTPUT FORMAT:

Add the unit digits of all the Fibonacci numbers from fib(0) to fib(N) and print only this sum

Sample Input:
8

Sample Output:
24

 

Question 6: (8 marks)

You are to write a program which takes 2 complex numbers as input and prints the sum and multiplication of the 2 complex numbers.

 

INPUT FORMAT:

The first and second lines contain 2 integers (the real and imaginary parts) for each complex number involved.

 

OUTPUT FORMAT:

The complex numbers sum and product on separate lines in the form a+bi or a-bi (depending on whether the complex part is negative or not).

Sample input:
1 2
1 3

Sample output:
2+5i
-5+5i
posted in Interview Experience May 18, 2019
by
9,218 views
12
Like
1
Love
0
Haha
1
Wow
0
Angry
1
Sad

29 Comments

29 Comments

Like

For Question 1 Pattern Program 

#include<iostream>
using namespace std;
int main()
{
    int i,j,n;
    cin>>n;
    for(i=1;i<=n;i++)
       {

        for(j=1;j<=(2*n);j++)
    {
        if(j<=i||(j>=(2*n+1)-i))
            cout<<"*";
            else
            cout<<" ";
    }
    cout<<"\n";
    }
}

 

Like
//question 6 sum and product of 2 complex number

#include <stdio.h>

int main(void) {
	 
    int r1,r2,i1,i2;
    
    scanf("%d",&r1);
    scanf("%d",&i1);
    scanf("%d",&r2);
    scanf("%d",&i2);
    
    //sum
    
    printf("%d+%di \n",r1+r2,i1+i2);
    
    
    //product
    int r=(r1*r2)-(i1*i2);
    int i=(r1*i2)+(r2*i1);
    printf("%d+%di",r,i);
    
	return 0;
}

 

Like
//question 4 arrange by left swap first.
#include <stdio.h>
 
int main(void) {
	 
    int n,x[100],i,j,temp,k;
    scanf("%d",&n);
    
   for(i=0;i<n;i++)
   {
    scanf("%d",&x[i]);
    printf("%d ",x[i]);
   }
   
   printf("\n");
   
    
   for(k=1;k<=n-1;k++)
   {
     for(i=0;i<=k-1;i++)
      
      if(x[i]>x[i+1])
      {
          temp = x[i];
          x[i]=x[i+1];
          x[i+1]=temp;
          k--;
          
          for(j=0;j<n;j++)
          printf("%d ",x[j]);
          printf("\n");
         
          break;
      }
    }
    
    return 0;
}

 

Like
This time questions were too easy. Many students got out of marks.
Like
Allowed languages?
Like

@toxicdesire Both C and C++ were allowed. I was there.

 

Like
C and C++ were allowed (Compiler was g++ ). Only restriction was that STL was prohibited if C++ was used (atleast this is what they mentioned before they started the test, during the tutorial).
Like
No such thing was mentioned to us. There was no restriction on the use of STL.
Like
@kunal If you were in Software Lab 2, then you should have heard what that TA (who was wearing a Red and Blue striped Tshirt) mentioned: "You can use C++ but don't use Standard Template Library"
(To be honest, I have no idea how they would check whether someone used STL or not, but maybe few people who got 50+ didn't get shortlisted because they used STL (or maybe it was because of that bodhitree website's glitch) ? I don't know if this is the case though)
Like
No restrictions. I did use vector at many places(although it wasn't needed). These problems didn't require STL anyway.
Like

@d3ba If there would have been any restriction then they should have told so in all labs. However we were not told, that means STL is allowed. 

And many with 50+ were not selected bcoz the cutoff was too high (probably 55+ or maybe 60).. it has nothing to do with using of STL.

Like
1

1st code

#include<iostream>
using namespace std;

int main()
{
	int n;
	cin>>n;
	
	int w = (2*n)-2;
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=i;j++)
		{
			cout<<"*";
		}
		
		for(int k=1;k<=w;k++)
		{
		       cout<<" ";
		}
		
		for(int j=1;j<=i;j++)
		{
			cout<<"*";
		}
		
		
		w=w-2;
		
		cout<<"\n";
	}
}

 

Like
1

2nd Code

#include<iostream>
using namespace std;

int main()
{
	int a[50],n;
	
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	
	int cin =0;
	
          for(int i=n;i>0;i--)
          {
          	if(a[i]!=9 && i==n)
          	a[i]=a[i]+1;
		
          	if(a[i]==9 && cin==0 && i==n)
          	{
          		a[i]=0;
          		cin=1;
		}
		
		if(a[i]==9 && cin==1)
          	{
          		a[i]=0;
          		cin=1;
		}
		
		else if (cin==1 && i!=n)
		{
			a[i]=a[i]+1;
			cin=0;
		}
		
		
	}
	
	if(cin==1)
	cout<<"1";
	
	for(int i=1;i<=n;i++)
	{
		cout<<a[i];
	}

return 0;
}

 

Like
2

@NOBITA

no, 1st one giving runtime error https://ideone.com/NL7c7T

edited Oct 2, 2019 by
Like
1

@srestha

Runs fine in dev cpp environment

 

Compiler used TDM GCC 4.9.2 64 bit release

Like
1

The run time error can be dissolved in online compilers if u add return 0; before the last braces 

U can try to run it here https://www.onlinegdb.com/online_c++_compiler Will give no error, dont know what website u have used and what crappy compiler they are using

Like
1

3rd Code

#include<iostream>
#include<stdlib.h>
using namespace std;


void cleap(int n)
{
	if(n%4==0)
	{
		if(n%100==0)
		{
			if(n%400==0)
			{
			}
			else
			{
				cout<<"Birth Year is incorrect";
				
				exit(0);
			}
		}
	}
	else
	{
			cout<<"Birth Year is incorrect";
				
				exit(0);
	}
}



int countleap(int n,int z)
{

	if(n%4==0)
	{
		if(n%100==0)
		{
			if(n%400==0)
			{
				z++;
				return z;
			}
			else
			return z;
			
		}
		
		else
		{
		z++;
		return z;
	          }
	}
	else
	return z;	
}


int main()
{
	int n;
	int t;
	
	cin>>n>>t;
	
	
	cleap(n);
	int k=0;
	
	for(int i=n;i<=t;i++)
	{
		k=countleap(i,k);
	}
	
	cout<<k;
return 0;


}




 

Like
1

5th CODE

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
	int n;
	
	cin>>n;
	
	int a[n];
	a[0]=0;
	a[1]=1;
	
	for(int i=2;i<=n;i++)
	{
		a[i]=a[i-1]+a[i-2];
	}
	
	
	int s=0;
	
	for(int i=0;i<=n;i++)
	{         
	          if(a[i]!=0)
	          {
	
		int c=ceil(log10(a[i]));
		if(c==1)
		{
		
		int b=(int)(pow(10,(c)));
		int p= a[i] % b;
		s=s+p;
	          }
	          
		if(c>=2)
		{
		int b=(int)(pow(10,(c-1)));
		int p= a[i] % b;
		s=s+p;
	          }
	          } 
	          
	          if(a[i]==1)
	          {
	          	s=s+1;
		}
	          
	          
	}
cout<<s;
return 0;

}



 

Like
1
How much time was given for doing these 6 codes?
Like
1

4th Code

#include<iostream>
using namespace std;

int main()
{
	int n;
	cin>>n;
	
	int a[n];
	
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<" ";
	}
	cout<<"\n";
	
	
              
              int j=0;
		
		while(j!=n)
		{
			int p=0;
			if(a[j]>a[j+1] && j!=(n-1))
			{
			  int t=a[j];
			  a[j]=a[j+1];
			  a[j+1]=t;
			  p=1;
		          }
		          if(p==0)
			j++;
			else
			j=0;
			
			if(p==1)
			{
			
			    for(int k=0;k<n;k++)
	                        {   
		                    cout<<a[k]<<" ";
	                         }
	                          cout<<"\n";
	                    }
	                    
		}
		
	
}

 

Like
1

6th CODE

#include<iostream>
using namespace std;

int main()
{
    int a,b,c,d;
    
    
    cin>>a>>b>>c>>d;


//ADDITION LOGIC
    
    int x=a+c;
    int y=b+d;
        
    
    
    if(y<0)
    {
    cout<<x<<y<<"i";    
    }
    
          if(y>=0)
    {
    cout<<x<<"+"<<y<<"i";
          }


cout<<"\n";
    
//MULTIPLICATION LOGIC

int q= c*a;
int w= c*b;
int e=d*a;
int r=-1*(d*b);

int m= q+r;
int n= w+e;

if(n>=0)
cout<<m<<"+"<<n<<"i";
else
cout<<m<<n<<"i";
    
return 0;    

    
}
Like
This code will give error for n=50, it will give array index out of bound error, since you have read the array from a[1] to a[n] instead of a[0] to a[n-1] and declared the array as a[50]

So for n = 50 , your code tries to read in the loction a[50] which does not exise.
Like
code for 2nd question(increment array by 1):

#include <iostream>
#define MAX 51

using namespace std;
void printInteger(int *a, int n){
    for(int i=0; i<n; i++){
        cout<< *(a+i) ;
        }
    cout<<endl;
}
int incrementInteger(int *a, int n){
    int carry=1, i;
    for(i=n-1; i>=0; i--){
        int temp = *(a+i)+carry;
        *(a+i) = temp%10;
        carry = temp/10;
        if(!carry)
            break;
    }
    if(carry && i<0){
        //check for MSB carry
        for(int j=0; j<n; j++)
            *(a+i+1) = *(a+i);
        *(a+0) = carry;
        n++;
    }
    return n;
}

int main ()
{
  int a[MAX]={0};
  int n;
  cin >> n;
  for(int i=0; i<n; i++)
    cin >> a[i];
  n = incrementInteger(a, n);
  printInteger(a,n);
}
Like
// 2nd code 
#include<iostream>
using namespace std;
int main()
{
    int carry=0,n=0;
    cin>>n;
    int a[n];
    for(int i= n-1; i>=0; i--)
    {
        cin>>a[i];
    }
    for(int i=0; i<n; i++)
    {
        if(i==0)
        {
            if(a[i]==9)
            {
                a[i]=0;
                carry = 1;
            }
            else
            {
                a[i]++;
                carry =0;
            }
        }
        else
        {
            if(carry==1)
            {
                if(a[i]==9)
                {
                    a[i]=0;
                    carry = 1;
                }
                else
                {
                    a[i]++;
                    carry = 0;
                }
    

            }
        }

    }
    if(carry == 1)
    {
        cout<<1;
    }
    for(int i=n-1; i>=0; i--)
    {
        cout<<a[i];
    }
}
Like
//2nd Code
#include<stdio.h>
#include<stdlib.h>
int main(){
    int i,n;
    int a[50];
    scanf("%d",&n);
    for (i = 0; i < n; i++)
    {
        scanf("%d",&a[i]);
    }
    for (i = n-1; i >= 0; i--)
    {
        if(a[i] + 1 == 10)
            a[i] = 0;
        else{
            a[i] = a[i] + 1;
            break;
        }
    }
    for (i = 0; i < n; i++)
    {
        printf("%d",a[i]);
    }
}

 

Like
//5th code
//Fibonacci using DP
#include<stdio.h>
#include<stdlib.h>
int fib(int n);
int main()
{
    int n;
    scanf("%d",&n);
    printf("%d",fib(n));
    return 0;
}
int fib(int n){
    int F[n+1];
    int c = 0;
    F[0] = 0;
    F[1] = 1;
    for(int i = 2; i<=n; i++){
        F[i] = (F[i-1] + F[i-2])%10;
    }
    for (int i = 0; i <= n; i++)
    {
        c = c + F[i];
    }
    return c;
}

 

Like
//4th code
#include<iostream>
using namespace std;

int main()
{
    int n,i,temp;
    cin>>n;
    int a[n];
    for(int i=0;i <n; i++)
    {
        cin>>a[i];
    }
    for(int i=0;i <n; i++)
    {
      cout<<a[i]<<" ";
  
    }
    cout<<"\n";
    i=0;
    while(i< n-1)
    {
        temp=0;
        if(a[i]>a[i+1])
        {
            temp = a[i];
            a[i] = a[i+1];
            a[i+1] = temp;
            for(int k=0;k < n; k++)
            {
                cout<<a[k];
                cout<<" ";
            }
                cout<<"\n";

            i = 0;
            continue; 

        }
        i++;
    }

}

 

Like
//complex number multiplication and sum
//code no. 6
#include<iostream>

using namespace std;

int main()
{
	int a,b,c,d,sum, multi;
	cin>>a>>b>>c>>d;
	cout<<(a+c)<<"+"<<b+d<<"i\n";
	cout<<(a*c-b*d)<<"+"<<a*d+c*b<<"i\n";

}

 

edited Apr 21, 2021 by
Like
code 1:

#include<bits/stdc++.h>
using namespace std;
int main()
{int n,i,j,k,k1,k2;
cin>>n;
k=1;
k2=1;
k1=(n-1)*2;
for(i=0;i<n;i++)
{
    for(j=1;j<=k;j++)
    cout<<"*";
    k++;
    
    for(j=k1;j>0;j--)
    cout<<" ";
    k1=k1-2;
    
    for(j=1;j<=k2;j++)
    cout<<"*";
    k2++;
    cout<<"\n";
    
}

    return 0;
}
-----------------------------------------------------

code 2:

#include<bits/stdc++.h>
using namespace std;
int main()
{
     int n;
    cin>>n;
    int a[n],i,f=0;
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    for(i=n-1;i>=0;i--)
    {
        
        if(a[i]<9)
        {f=1;a[i]=a[i]+1;
            break;
        }
        
        else
        {
            a[i]=0;
        }
    }
    if(f==0)cout<<1;
    for(i=0;i<n;i++)
    {cout<<a[i];
    }
return 0;    
}

------------------------------------

code 3:

#include<bits/stdc++.h>
using namespace std;
int main()
{int a,b,i,c=0;
    cin>>a>>b;
    for(i=a;i<=b;i++)
    {
    if(i==a && i%4!=0){
            cout<<"Birth Year is incorrect";return 0;
    }
    if(i%4==0 )
    {
        if(i%100==0 && i%400!=0 )
        {if(i==a)
            {
            cout<<"Birth Year is incorrect";return 0;}
            else
            continue;
        }
        else
        {
            c++;cout<<i<<endl;
        }
    }
}cout<<c<<endl;
}

-----------------------------------------

code 4:

#include<bits/stdc++.h>
using namespace std;
int main()
{int i,n,i1;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
    cin>>a[i];
}
for(i=0;i<n;i++)cout<<a[i]<<" ";cout<<"\n";i=0;
while(i<n-1)
{
    if(a[i]>a[i+1])
    {
        int t=a[i];
        a[i]=a[i+1];
        a[i+1]=t;
        
        for(i1=0;i1<n;i1++)cout<<a[i1]<<" ";cout<<"\n";
        i=0;
        //cout<<i<<endl;
    }else i++;
}return 0;

}

-----------------------------------------

code 5:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,a,b,c,s=0;
    cin>>n;
    a=0;b=1;
    for(i=0;i<=n;i++)
    {c=a+b;
    s+=a%10;//cout<<a<<endl;
        a=b;
        b=c;
    }cout<<s<<endl;return 0;
}

--------------------------------------------------

code 6:

#include<bits/stdc++.h>
using namespace std;
int main()
{int a,b,x,y;
    cin>>a>>b;
    cin>>x>>y;
    cout<<a+x<<"+"<<b+y<<"i"<<endl;
    cout<<a*x-b*y<<"+"<<a*y+x*b<<"i"<<endl;return 0;
}

hope this helps!!