Saturday, February 16, 2013

Amazon question - Schedule a meeting based on given work timings

Consider the following input and output . In the input, first line contains number of input 5 and the time you need to schedule (120 minutes) . Next five lines contains the work timings of that person , so he is busy at these times . And the output shows the list of available timings. This question is asked by amazon in interviewstreet.com . First the timings are sorted using bubble sort based on start time .And you need to compare timings to get the available timings .

Sample Input:
5 120
16 00 17 00
10 30 14 30
20 45 22 15
10 00 13 15
09 00 11 00

Sample Output:
00 00 09 00
17 00 20 45

Code: (in JAVA)
import java.util.Scanner;
class Solution
{
public static void main( String args[] )
{
Scanner in = new Scanner(System.in);
int N,time_wanted;
N=in.nextInt();
time_wanted=in.nextInt();
int hr,mn;
int start[] = new int[N];
int end[] = new int[N];
for(int j=0;j<N;j++)
{
hr=in.nextInt();
mn=in.nextInt();
start[j]=hr*60+mn;
hr=in.nextInt();
mn=in.nextInt();
end[j]=hr*60+mn;
if(end[j]==0)
end[j]=1440;
}
schedule(start,end,N,time_wanted);
}
public static void schedule(int[] start, int[] end, int N,int time_wanted)
{
int min=0,max=24*60,temp;
//sort based on start time using bubble sort
for(int i=N-1;i>=0;i--)
{
for(int j=0;j<i;j++)
{
if(start[j]>start[j+1])
{
temp=start[j];
start[j]=start[j+1];
start[j+1]=temp;
temp=end[j];
end[j]=end[j+1];
end[j+1]=temp;
}
}
}
//find the answer
for(int i=0;i<N;i++)
{
if(start[i] >end[i])
{
continue;
}
else
{
if(start[i]>min && start[i]<max)
{
if(start[i]-min>=time_wanted)
{
display(min,start[i]);
}
}
}
if(min<end[i])
min=end[i];
}
if(max-min>=time_wanted)
display(min,max);
}
public static void display(int min, int max)
{
if(max==1440)
max=0;
if((min/60)/10==0)
System.out.print("0"+min/60+" ");
else
System.out.print(min/60+" ");
if(min%60 < 10)
System.out.print("0"+min%60+" ");
else
System.out.print(min%60+" ");
if((max/60)/10==0)
System.out.print("0"+max/60+" ");
else
System.out.print(max/60+" ");
if(max%60 < 10)
System.out.println("0"+max%60+" ");
else
System.out.println(max%60+" ");
}
}
view raw scheduler.java hosted with ❤ by GitHub

Please let me know if you have any questions .

2 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

Hi,

I tired this problem using C++

My Alogirthm is:

1) Sort the array by start time
2) calculating difference between Start time and End time of Alternate schedules.

##Note## this program passed First 7 test cases.

Can you please assist me where i did mistake..Which one i forgot to check.?

Here's my code.

#include
#include
#include
#include

using namespace std;

class meeting_Shld
{
vector slot;
int M,K,val,T,Time_sec,Start_HH=0,Start_MM=0,End_HH=24,End_MM=0;
void getData()
{
cin>>M>>K;
T=M*4;
// cout<>val;
slot.push_back(val);
}

}

void swapp(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
//cout<< """ """<<*b<<" "<<*a<<" "<slot[j])
{
swapp(&slot[i],&slot[j]);
swapp(&slot[i+1],&slot[j+1]);
swapp(&slot[i+2],&slot[j+2]);
swapp(&slot[i+3],&slot[j+3]);


}
else if(slot[i]==slot[j])
{
if(slot[i+1]>slot[j+1])
{
swapp(&slot[i],&slot[j]);
swapp(&slot[i+1],&slot[j+1]);
swapp(&slot[i+2],&slot[j+2]);
swapp(&slot[i+3],&slot[j+3]);

}
}


}




}
void display()
{
for(int i=0,j=1;i=0)
{
Time_sec=Time_sec+(slot[1]-Start_MM);
//cout<=K)
cout<=0)
{
Time_sec=Time_sec+(slot[i+3]-slot[i+1]);
if(Time_sec>=K)
cout<=0)
{

Time_sec=Time_sec+(End_MM-slot[T-1]);
//cout<=K)
cout<<setw(2)<<setfill('0')<<slot[T-2]<<" "<<setw(2)<<setfill('0')<<slot[T-1]<<" "<<setw(2)<<setfill('0')<<End_HH-24<<" "<<setw(2)<<setfill('0')<<End_MM<<endl;


}

}


//-----------------------------End of day --------------------------------------------------------------------------------------------------------------



}

public:

void get_start()
{
getData();
sortData();
//display();
//cout<<endl;
getMeeting();



}



};


int main()
{
meeting_Shld ms;
ms.get_start();
return 0;
}