Google Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
11
of 13 vote

We can simply use an array keeping a track of every second. The length of the array for 24 hours = 24 * 60 * 60. So we will have int[] clicksPerSec = new int[86400];

To make it O(1) amortized(since summing) time to find the clicks in last minute/ hour/ day or any query in a range, we can sum the clicks int the array arr[i] += arr[i-1] (note: here we can overflow the integer). So whenever we need to have the clicks for a specific second we return arr[second] - arr[second - 1]. For minutes we return arr[minute * 60] - arr[(minute - 1) * 60], etc..

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

Nice solution!

- albertone9 December 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why would you need 24*60*60 elements in the array? You only need to store the last hour (60*60 elements).

You also need to maintain the index for the array and when you hit the last element (3599th element), you roll over and overwrite the 1st element.

Also, at the end of every hour, you should print out the number of hourly hits (or save to database or whatever)
When you need the number hits for the last minute, you just sum the last 60 elements in the array. If you need hits for the last hour, you sum the elements of the entire array.

- Gokay Huz July 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if all we need is hits-per-last-sec, hits-per-last-min and hits-per-last-hour, then we can use "rolling sums":
- one cur-sec counter for the current second
- sec[60] for last 60 sec, sum(sec) a summary of all sec and a now-sec cyclic pointer
- min[60 for last 60 min, sum(sec) a summary of all min and a now-min cyclic pointer

Whenever a second is over:
- subtract sec[now-60] from the sum(sec)
- add cur-sec to the sum(sec)
- write cur-sec instead of sec[now-60]
- increment the cyclic pointer

Whenever a min is over:
- subtract min[now-60] from the sum(min)
- add sum(sec) to sum(min)
- write sum(sec) instead of min[now-60]
- increment the cyclic pointer

The same approach can be cheaply extended to last-day, last-week, etc.
All the operations are O(1).

- Anonymous January 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
First you can have a {{{hashtable url}}} storing all the website url information (I assume you want to count massive url clicks). Then for the {{{url}}}, I gonna use a hit per minute as a sample. {{{ Class Url{ int hitsPreMin; int CurrentMin; int hitsInSec[60]; void click(int hh, int mm, int ss){ if(mm > currentMin){ //Count all clicks in pervious minutes and update the hitsPreMin; //clear hitsInSec; CurrentMin = mm; } hitsInSec[ss]++; } } - Leo October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My interpretation of the question: Given a website you have to find number of hits in the last second, last minute and last hour.

Idea:
Have all the counters as data member and two methods click() and query(), click updates the counter whenever the site gets a hit and query gives you hits in the last second, last minute or last hour.
Following is the pseudocode for member functions for second counter. Could be easily extended to minutes and hours.

class counter{
private:
  int hitspersecond;  //hits in last second
  int lastsec;  //last hit instant
public:
void click(){
    currentsec = getcurrenttimeinsec() //absolute time in second, use standard library funtion to get that
   
  if(currentsec == lastsec) hitspersecond++;   //lastsec is last hit absolute time in seconds
  else {
       hitspersecond=1;
       lastsec=currentsec;
  }
 //Similar code for minutes and hours
}

void Query(){
  if(getcurrenttimeinsec() == lastsec) return hitspersecond;
  else return 0;
   //Similar code for minutes and hours
}
};

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

Or can mantain three different arrays to store counters for secs, mins and hours.
int secs[24*60*60]
int mins[24*60]
int hours[24]

This way click can update all the counters to provide access in O(1) time. (offcourse at cost of extra memory)

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

This is my phone interview question from Google. I passed it. :)

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

hey HH, what was ur answer ? n wat were your other questions?

- beginner October 31, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In a non-programmatic sense, you could send some kind of UDP indication for each hit that would hit a server and write to a log file. Then searching by second, minute, or hour could be as simple as doing a grep "time-format" log-file | wc -l

- dchang.me December 06, 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