Yatra.com Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
1
of 3 vote

#include <iostream>
using namespace std;
int main()
{
int x = 3;
int y =4;

int i = 1;
while (i < y)
{
x*=x;
i*=2;
}
cout<<"result="<<x;
return 0;
}

- kk.nitrkl January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

assuming y is always 2^n

- kk.nitrkl January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I propose the same ..

- Sanjay Kumar January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello KK and Sanjay, I quite didn't understand the line "i*=2;" . Shouldn't it be i+=1 instead? For the given problem where x=3,y=4 the answer is (3*3*3*3=81), but the while loop breaks when x=27 as i is already 4. Am I missing something here?

- Anonymous January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ anonymous
i*=2 is correct as at every step we have x*=x that means squaring of x is done and that value is again assigned to x
at given problem loop still breaks at x=81 not at x=27 ( as when i=1 -> x=3*3 =9 , i=2 -> x=9*9 =81 , i=4 -> loop terminates and we get 81 as x)

- priyankajaggi4 January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

can we still write more efficient algo for the case when y=2^n.
eg: y=2^13
where n=13 ~= 1101. And applying some bit arithmatic here . Interviewer give me only this much of hint . How to reach to a solution from here??

- priyankajaggi4 January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Was interview specific about 2. If yes then,
2^13 = 2^(1101b) = 2^8 * 2^4 * 2 = (2 << 8) * (2 << 4) * 2
Though not sure if the interview was expecting this answer...

- abhishekatuw January 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use Divide and Conquer to do so

X^y= X^(y/2) * X^(y/2) : if y is even
=X^(y/2) * X^(y/2) *X : if y is odd

Further small powers can be percalculated and the answer to recursion is the answer !

- scofield January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If y = 2^n,

x^y = x << n.

- vijay January 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@vijay that is not right
as x<<n = (2^n) *x= y*x
But something like that i had to do during interview using bit arithmatic, but was not able to figure out exact solution there.
@ scofield , your solution is obvious , but is there any way to even reduce its time complexity.

- priyankajaggi4 January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If y is 2^n then x ^ y is x multiplied with itself and then the result is multiplied with itself, etc for n-1 times. This is not recursive but can be done with n-1 multiplication.
Another issue is that with ints this is a maths questions because of the overflow happening very fast :)

- Selmeczy, Péter January 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

int z;
int pow(int x, int y){

if (y==0)
return 1;

if(y%2==1)
{
z=pow(x,(int)(y/2));
x=z*z*x;
}
else
{
z=pow(x,(int)(y/2));
x=z*z;
}
return x;
}

int main()
{
int x,y;
while(cin>>x>>y)

cout<<pow(x,y)<<endl;

return 0;

}

- Anonymous January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int pow(int a, int b)
        {
            int result = 1;
          
            while (b!= 0)
            {               
                if ((b& 1) > 0)
                    result *= a;
                b>>= 1;
                a *= a;
            }          

            return result;
     }

- Ran January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{use divide and conquer method .... i.e if y=2n (even) then z=y^n; answer=n*n; else z=y^n; answer=n*n*y; so, T(n)=T(n/2)+constant time; - damodarnitt January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{use divide and conquer method .... i.e if y=2n (even) then z=y^n; answer=n*n; else z=y^n; answer=n*n*y; so, T(n)=T(n/2)+constant time; - damodarnitt January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*Write an efficient power function.
int power (int x, int y) for calculating x^y,
especially for the case that y is of the form 2^n.*/
function pow( x, y){
/* Cases:       x^0 = 1
                x^1 = x
                x^y = z
                x^(2^n) = z
*/
        if ( y < 2 ) {
                var ret;
                (y == 0) ? ret = 1 : ret = x;
                return ret;
        }
        var isOdd = (y & 1);
        if (!isOdd) {
                return pow(x * x, y/2);
        } else {
                return x * pow(x * x, (y-1)/2);
        }

}

- srt January 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If y=2^n you just need to right shift by n

#include<stdio.h>

int main()
{
        int n,x;
        scanf("%d",&x);

        scanf("%d",&n);

        printf("%d",x<<n);
}

- Kesha May 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int pow(int x, int y){
        
        int result=x,rem;
        if(y==0)
            return 1;
        else if(y==1)
            return x;
        else{
            int i = 1;
            while(i*2<=y){
                result = result*result;
                i=i*2;
            }
            rem = y%i;
            if(rem!=0){
                result = result *pow(x,rem);
            }
        }
    return result;
    }

- Shreshtha December 26, 2016 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More