Facebook Interview Question for Software Engineer / Developers






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

To count # of requests
Maintain a circular queue<int> of size 3600*(# sample per second). That is, if you are sampling every millisecond then (# of sample per second is 1000)
Now, if you request last 1 hr then sum all elements in the queue,
if you request last 1 min, then sum top 60*1000 elements
if you request last 1 sec, then sum top 1000 elements

- mithya September 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

the interface must be something as below , each event must have the capability of reporting itself , on occurrence . The Event class will have something like this interface

class Event
{
private:
int code ;
public :
Event(int code);
abstract notify();// do the purpose of the event and call the register method of the Register class
}

class Register
{
Register(int fno) ;
RegisterEvent(int code) ; // register this event with number "code"
private:
int lasthr ;// the number of events in the lasthour
int lastmin ;// the number of events in the lastmin , updated with every call to RegisterEvent
time_t time1 , time2 ;// to calculate the time
int alloc_new_file(); // the new file is allocated , which gives the new file , if the new file is overidden ;
}
many other choices are there for handling the overflow problem , but this seems to be the fastest

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

The key point here is about how to store the events and report them. Considering the storage space and reporting method, you do not need to store all events but dynamically aggregate them for saving the space.

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

The q says: "1) Record an Event". why do you assume: "you do not need to store all events" ?
How would you "dynamically aggregate them for saving the space"? how long will you save? last minute? last hour?

if we want to keep all the events: we can keep in the main-memory pairs of <start,end> addresses in the secondary memory. we will open a new pair every time a non consecutive memory is allocated.

Corrections are appreciated.

- adam2008 August 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming we only need the two counters (last hour\minute) here is my idea:
keep a buffer of pairs in the main memory <start time, secondery memory address>. Keep aside the start and end of this buffer. Every time a new event is entered: iterrate from the start, deallocate the memory of an event which is older than 1 hour and decrement the last-hour-events by one. enter your new event at the end and increment the counter by one.

Corrections are welcome.

- adam2008 August 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Register {
  private:
    int counters[3600];

  public:

    Register() {
      for(int i = 0; i < 3600; ++i) counters[i] = 0;
    }

    void add(const Event& e) {
      long time = e.timestamp; /* In Seconds */
      int bucket = time % 3600;
      counters[bucket]++;
    }

    long getLastHour() {
      long sum = 0;
      for(int i=0; i < 3600; ++i) sum += counters[i];
      return sum;
    }

    long getLastMinute() {
      long now = time(NULL);
      long sum = 0;
      for(int i = now; i > now - 60; --i) {
        sum += counters[i%3600];
      }
      return sum;
    } 
};

- nmj October 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If the last hour fits into long when you return it, why don't you just maintain a long variable for storage?

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

we can use splay tree for this. In this we can keep adding the new nodes to the top.

- Anonymous July 28, 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