Question need help asap




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

I believe O(n) is possible since they are sorted by start time.
Keep auxiliary variables storing the current maximum end time of the processes seen so far and the time the current set of consecutive processes started.

Go through the entries in the log in order. If the current process starts at a time larger than the previous maximum time the processor was busy, then this is a break (it was idle) and update the current start time.

// for simplicity, consider the log as an array with N entrys each with start and end times.
// in reality this would be just fetching from the log file - no big difference.
int GetMaxBusyTime(Entry* entries, int N) {
     int max_end = 0, res = 0, cur_begin = 0;
     for (int i = 0 ; i < N ; i++) {
          if (entries[i].start > max_end) { // it was idle before this one
              res = max(res , max_end - cur_begin + 1);
              cur_begin = entries[i].start;
              max_end = entries[i].end;
          }
          else {
              max_end = max(max_end, entries[i].end);
          }
     }
     return max(res , max_end - cur_begin + 1); // also check the final one
}

- Miguel Oliveira December 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks! but why you using the + 1?

- Dany December 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

if you consider closed intervals like [start, end] in seconds, the amount of time is end-start+1 seconds.

Example [2, 10] : 10 - 2 + 1 = 9 seconds.

- Miguel Oliveira December 05, 2013 | Flag




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