Akamai Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

If intervals are [x_i, y_i]

Sort by x_i. Assume x_1 <= x_2 <= ...

Enumerate in order of x_i pushing the intervals onto a stack.

If the current interval intersects with the one on the top, merge them into one interval, and pop the stack and push the merged interval.

In the end, the stack will contain disjoint intervals.

Now keep popping the stack, and add up the time.

O(n log n) time, O(n) space.

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

take an array of size 50 init it with all ZEROs
make an entry as 1 of the interval for all given interval
count number of 1 in the array & divide it by 50......

- Anonymous August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you dont sort this will be O(n) time

- Mike August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Mike: Then you might have to consider multiple intervals for merging... and might become quadratic.

Care to elaborate?

- Anonymous August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

There is no need to use stack. We just have to know where is the end of the intervals which has been added so far. Here is algo:
1) Sort intervals according to 'start' coordinate;
2) Add first interval to the sum
3) Set 'current end' = firstInter.end
4) Iterate over remaining intervals and check 3 conditions. If new interval is
4.1) fully included in the already calculated intervals - then skip
4.2) is partially included e.g [1,7][5,10] - the add only remaining part and update 'current end'
4.3) excluded - add whole interval -and update 'current end'
5) result = sum/length
O(n log n) time, O(1) space.
Code:

package akamai;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class VideoInterval {

    public static class Interval {
	Integer start;
	Integer end;

	public Interval(int start, int end) {
	    this.start = start;
	    this.end = end;
	}

	public static void sort(List<Interval> intervals) {

	    Collections.sort(intervals, new Comparator<Interval>() {
		@Override
		public int compare(Interval o1, Interval o2) {
		    return o1.start.compareTo(o2.start);
		}

	    });
	}
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

	List<Interval> intervals = new ArrayList<VideoInterval.Interval>();
	// 1-7, 10-15, 5-11, 20-25
	intervals.add(new Interval(1, 7));
	intervals.add(new Interval(10, 15));
	intervals.add(new Interval(5, 11));
	intervals.add(new Interval(20, 25));
	Interval.sort(intervals);

	Interval first = intervals.get(0);
	int sum = first.end - first.start;
	int currentEnd = first.end;
	for (int i = 1; i < intervals.size(); i++) {
	    Interval next = intervals.get(i);
	    if (next.start < currentEnd) {
		// fully included - skip
		if (next.end <= currentEnd) {
		    continue;
		    // partially included - add remaining part
		} else {
		    sum += next.end - currentEnd;
		    currentEnd = next.end;
		}
		// excluded - add whole interval
	    } else {
		sum += next.end - next.start;
		currentEnd = next.end;
	    }
	}

	System.out.println((float) (sum + 1) / 50); // +1 include last minute -
						    // [1 - 7] is 7 not 6
    }

}

- Anonymous June 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public double getFraction(int[] start,int[] end, int total) {
double res = 0;
byte[] c= new byte[total+1];
for(int i=0;i<start.length;i++) {
for(int j=start[i];j<=end[i];j++) {
c[j] = 1;
}
}
for(int i=1;i<total+1;i++) {
if(c[i]==1) {
res++;
}
}
return res/total;
}

- Shivam Maharshi August 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will have Time Complexity of O(n). But this will have memory complexity of O(total). The time complexity is gud but the memory complexity is horrible. Thus I would recommend the solution with merge sorting first and then simple comparisons but the two consecutive range.

- Shivam Maharshi August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Interval {
     Integer start;
     Integer end;
}

int findWatchedLength(List<Interval> intervals) {
	Set<Integer> points = new HashSet<Integer>(50);
	for(Clip c : clips ){
		for(int i = c.start ; i <= c.end ; i++) {
			points.add(i);
		}
	}

	return (points.size()/50);
}

- JavaSolution February 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it should be 0 -7 instead of 1 -7
and the rest has already been solved by 2 of the above codes

- anon July 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

(1-7,5-11,10-15): In these intervals a total of 15 mins video has been watched. In the interval (20-25) 5 mins of video has been watched. So, total(15+5)=20 mins of video has been watched. Fraction of video watched=(20/50)=0.4.

- UV August 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So helpful. Thanks.

- Anonymous August 10, 2012 | 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