Google Interview Question
Developer Program EngineersCountry: United States
After detecting value change in variable first thread should sleep for 1sec - few milisecond . (say . sleep for 950 msecond).
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
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.
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.
}
}
your algo might work but is it efficient?. Won't the control pass through the while loop several times in one sec?
@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.
Thanks Kumar,
while (1)
{
if( var != temp )
{
temp =var ;
update_clock(); // increase clock time by 1 second.
}
Sleep(50) ; // sleep for 50 milli seconds
}
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.
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());
}
}
#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;
}
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.
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