Amazon Interview Question


Country: United States




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

This is the problem of job scheduling:
There are N jobs and K lines.
Sort all the jobs (meetings) by their finish time and then start selection from the jobs that finishes first putting it in one room and select the next job put it in a room that does not overlap with other jobs if there are more than one lines (rooms) then select the one that has the least finished time interval to the start of this job.

- Nooreddin July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

A greedy algorithm doesn't guarantee scheduling the maximum number of meetings. This problem can be mapped to a conflict graph: every meeting can be mapped to a vertex in a graph. If two meetings have overlap (conflicts), they share an (undirected) edge in the graph. Now the question becomes a variant of vertex coloring problem where two adjacent vertices don't share the same color (two conflict meetings cannot share the same room).

To optimally solve this, a backtracking solution can do the job, but with two cases:

1. if the graph is k-colorable, just return N

2. if the graph is not k-colorable, increase k until it's m-colorable (m>k). Now you will get m set of vertices. Sort them with non-increasing order of set sizes and return the total size of the first k sets.

- putin July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This can transformed to :-

- Construct each interval as a node.
- Connect two nodes if those intervals overlap, to build an "undirected graph"
(Note: graph could be a forest also(disconnected components))
- Now problem is :- how many of N nodes can be colored with K colors ?

- X July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not clear, how do you use the "connected" information for your coloring problem?

- hieu.pham July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@hieu.pham overlapped meeting are adjacent to each other.
if you have 'k' meeting rooms, then you can assign each vertex 1 color out of these 'k'.
all the adjacent vertices must not have that same color, but among themselves they can have a different same color if they don't have a direct edge between them.
example:
M1: 13:00-14:00 - Color 1
M2: 12:30-13:30 - Color 2
M3: 13:30-14:00 - Color 2

(M2,M3) are adjacent to M1
M2 & M3 are not adjacent so the can share the same color

- eyal.safran July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is the problem of job scheduling using N jobs and K factory line. There is a greedy solution for this problem.
Sort jobs based on their finish time and select the jobs for the lines (rooms), if there is more than one line (room) that job can fit inside then select the line with the least interval of the last finished job on that line and the current job. If the job doesn't fit then drop that job and go to the next job.

- nooreddin July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have quite similar solution, but I maintain the K room sorted by the last ending of the meeting assigned to it.

- hieu.pham July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Idea is to sort the Intervals with absolute value of startTime and EndTime, but in real store the -EndTime in the array. Count the maximum value of consecutive StartTime and in the that max will be the result. A quick code is below:

public static int maximumMeetingScheduler(ArrayList<Interval> intervals) {
    ArrayList<Integer> convertedIntervals = new ArrayList<>();
    for(Interval interval : intervals) {
        convertedIntervals.add(interval.startTime);
        convertedIntervals.add(-interval.endTime);
    }
    
    Comparator<Integer> comp = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return Math.abs(o1).compareTo(Math.abs(o2));
        }
    }
    
    Collections.sort(convertedIntervals, comp);
    
    int max = 0;
    int result = max;
    for(int i=0;i<convertedIntervals.size();i++) {
        if(convertedIntervals.get(i)>0) {
            max++;
        } else {
            max--;
        }
        if(result<max) result = max;
    }
    return result;
}

- Invincible July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just sort based on start time and schedule meetings in the rooms. Loop N/K times.
However, i have one question on this, what if we find out that one of the meeting start time will be passed by the time the current running meetings end. Do we ever schedule this meeting. Is this a valid use case or the test cases given are such that this case will never arise?

- Alok July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sort meetings by their start time. Assign available room to each meeting starting from first one, make sure to assign room which is able to fit tightest schedule.

- Amazing July 09, 2015 | 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