Google Interview Question for Software Engineer in Tests






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

void fn(int n)
{
int d,i=0;
int j=n;
while(i<j)
{
i++;
//d=i*i+(j-.25)*(j-.25)-n*n;
//if(d>0) j--;
}
}

We have O(n) complexity in that case

Then lets assume that d is ALWAYS be positive, than we have O(1/2 n) that is O(n) anyway

- O(n) September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) because even if our j never change inside while loop i will be executed at max n number of times case when d<=0 always.

- nikhil May 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think d>0 always.
Necessary condition is i^2 >= n^2 - (j-0.25)^2
if we ignore 0.25, i^2 >= n^2-j^2
base condition is i=0, j=n, where condition won't be true.

- Anonymous September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In this easy case, it has nothing to do with d... i is still increased until a value from n/2 to n.
Worst case is O(n). Best case O(n/2) which is O(n).

- S September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right. Let me try to rephrase it so it becomes clearer:
In the "worst case" j is never decremented, and we need n iterations to finish. Perhaps this is an impossible run, but it is definitely impossible for a run to be any worse.
In the "best case" j is always decremented, and we need n/2 iterations to finish. Perhaps this is an impossible run, but it is definitely impossible for a run to be any better.
Hence O(n).
Thank you S.

- lupuslupus September 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lets assume n=4;

1st Iteration: i=0; j=4
2nd iteration: i=1; j=4
3rd iteration: i=2; j=3 --> as d>0 satisfies here
4th iteration: i=3; j=2

So this is O(n) complexity

- O(?) September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

even though if we dont care about what the value of d going to be then also time complexity will be O(n) because if d>0 then j will move one move left which will decrease no of move by i.if d<0 then all n move will be done by i

- ramu kumar September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it was a time wasting objective question in that paper.

- Anonymous September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is o(n/2)

- Debanjan September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In terms of complexity o(n/2) is equal to o(n).

- kilotaras September 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

In terms of complexity o(n/2) is equal to o(n).

- kilotaras September 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Big O is about the worst case. Here the worst case is the time where J is never decremented. Therefore, we assume that d is always negative, and we will find an average value.

- mal January 25, 2011 | 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