Google Interview Question






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

4gig ~= 2^32, assuming each instruction sent to cpu increments the counter by 1, it will take 2^64/2^32, ie..2^32 seconds (136 years) to increment it.

- Harshal July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Below is my analysis:
64-bit max int number is 9,223,372,036,854,775,807
4Ghz speed means 4,000,000,000 cycles / sec
For 1 cycle it takes 1 / 4,000,000,000 sec = 0.00000000025 sec
Lets the below code takes 20 machine cycles to run the for loop each time for one number to verify
for( ; val > 0; val++)
{
// cout << val << endl;
}
So, total time take is
9,223,372,036,854,775,807 * 0.00000000025 * 20 seconds ~= 74 years

- Anonymous July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Small correction to above analysis:

int max number is 2^63 - 1 (Not 2^64)
2^63 / 2^32 seconds = 68 years

And the for loop will take around 20 cycles to run each loop

so total time will be 68 * 20 = 1360 years

- Anonymous July 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the counter is unsigned integer then int max is 2^64
then answer will be 136 years and total time with for loop takes 20 cycles for each loop 136 * 20 = 2720 years

- Anonymous July 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1GHz = 10^9 HZ
1HZ = cycles per second
signed long = 2^63-1
cpu cycles for 'inc': suppose 1

(((2**63-1)*1)/10**9) / 60 / 60 / 24 = 292 years

- m@}{ July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

given speed was 4GHz... 292/4

- Anonymous July 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are right ))).

- max July 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the key here is to demonstrate if 4Ghz gives you any clue to complete the math without calculator.

So let's assume increment takes one cycle, 4ghz will be able to increment 32 bit in one second.
So now I know it will take one second to increment lower 32 bits one time, which means it will take 4 billion seconds to increase the whole 64 bit ... which is divided by 60 is roughly 66 million minutes ... and so on

- Dan July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Default is signed integer whose range is from -(2^32) to (2^32)-1
1GHz = 1000MHz = 10^6KHz = 10^9Hz (cycles per second)
Since initially the value is 0 we have to increment 2^32 times to overflow the counter.
Assuming it takes one cycle to increment by 1 it takes (2^32)/(10^9) seconds which is 4.2949673 seconds.

- M. Naveen Reddy July 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just to put another - probably wrong - calculation to the table:

MAX_INT64 = 2^63-1
4GHZ = ~4*10^9 increments per second -> assuming every Hz increments the counter

MAX_INT64 / 4GHZ = 23058430092137 seconds
23058430092137s / 60 / 60 / 24 / 365 = ~73 years

- NaN July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isnt it 2^64 -1 ??, for four bit integer the max value is 15, which is 2 ^ 4 -1 . correct me if am making any mistake below.

1hz is 1 cycle per second
4ghz, 1/ (4 * 10^9) will give the time taken per cycle.

assuming each increment takes a cycle we have 2 ^ 64 cycles. ignoring the -1

(2 ^ 64) / ( (4 * 10^9) * 60 * 60 * 24 * 365 ) ~= 146 years.

is this correct?

- Anonymous July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think ur right, i thought in the same way u did

- Julian September 28, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

AFAIK it would be 2^64-1 if it's an unsigned 64 bit integer, which i can't see mentioned in the question. It's probably best to ask in the interview...
A signed 64 bit integer needs one bit for the sign (+/-) and thus only has 63-1 remaining to represent a number.

- NaN July 15, 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