Google Interview Question for Developer Program Engineers


Country: United States




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

2 threads. First thread checks in an infinite loop for a new value and interrupts the clock thread which updates its state on every interrupt

- Anonymous April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

After detecting value change in variable first thread should sleep for 1sec - few milisecond . (say . sleep for 950 msecond).

- jigs September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@jigs - and how would it know wen 950 msec are over ?
Doing this is pointless if it has the knowledge of another calibrated clock ...

- AJK November 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

I think this requires the observer-observable pattern. Basically the variable that is changing must belong to a class that supports "observable". The clock class should be an "observer". When its variable changes, it should send a "notify" which will cause the clock or here the "observer" to update its time.

- axecapone April 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@axecapone
Using Observer pattern for a state changing so frequently will turn out to be huge performance bottleneck.
If doesn't matter if the pattern is implemented in PULL/PUSH manner, the state is going to change every second, so the overhead is going to be the same in both cases if we have to implement a clock using this concept.

- Learner April 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

2 threads. First thread checks in an infinite loop for a new value and interrupts the clock thread which updates its state on every interrupt

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

Algo::

extern int var;   // this variable is being changed from external world
int temp = var ; 

while (1)
{
    if( var != temp )
    {
		temp =var ; 
        update_clock(); // increase clock time by 1 second.  
    }
}

- siva.sai.2020 April 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

your algo might work but is it efficient?. Won't the control pass through the while loop several times in one sec?

- Kumar April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Kumar: It's a tradeoff between how quickly you want the system to respond to changes vs. how much processing power you want to spend waiting. Here, the response is as fast as possible in exchange for full processing power being used.

- eugene.yarovoi April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks Kumar,

while (1)
{
if( var != temp )
{
temp =var ;
update_clock(); // increase clock time by 1 second.
}
Sleep(50) ; // sleep for 50 milli seconds
}

- siva.sai.2020 April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't "var" be declared as volatile?

- AP April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are right, variable "var " should be Volatile

- siva.sai.2020 April 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

would not be it better to sleep for close to 900 msec to save cpu for other processes.

- ether June 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Since we are designing a clock, or timer, there won't something like sleep for certain time, right? Using two threads may be better.

- Daru June 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

why bother to update the clock if that clock is not queried?

set a base value for variable, map the base to a time stamp. then calculate the real time whenever been queried.

- yongning April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The problem just says that the variable changes, not that it's incremented. As far as we know, it may just flip between 0 and 1...

- Anonymous May 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.concurrent.atomic.AtomicInteger;

public class Clock
{
    static class WallClock
    {
        private int hour;
        private int min;
        private int sec;
        
        public WallClock(final int epoch)
        {
            this.sec = epoch % 60;
            int m = epoch / 60;
            this.min = m % 60;
            this.hour = m / 60;
        }
    }
    
    private static AtomicInteger epoch = new AtomicInteger();
    private static volatile WallClock wc;
        
    public static void tick()
    {
        wc = new WallClock(epoch.incrementAndGet());
    }
}

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

#include<stdio.h>
#include<unistd.h>

unsigned int var=172795;

int main()
{
	unsigned int sec=0;
	unsigned int min=0;
	unsigned int hr=0;
	
	while(1)
	{
		++var;
		sleep(1);
		sec=var%60;
		min=((var/60)>=60)?((var/60)%60):(var/60);
		hr=((var/3600)>=24)?((var/3600)%24):(var/3600);

		printf("%u:%u:%u\n",hr,min,sec);
	}

	return 0;

}

- okaysh June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

I think this is simple designing question. In which we will have 2 function:
1) GetTime, Which will give current time
2) Set time, In this function we will take 2 counters, 1 is for minute, 2nd if for hours. Then based on changing variable we can increment minutes and hours. Also we will take 2 more variable, Max Minutes and Max Hours which will trigger resetting of Minutes and Hours function.

- Andy2000 September 10, 2012 | 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