Microsoft Interview Question
InternsCountry: India
Interview Type: Written Test
you just have to calculate the number of days present between two dates.
For example let the date be 10 dec 2013.No of days between two date is 474(do keep note of leap year :)).Then apply modulo 7 on number of days.In this case it will be 5.add 5 to to the thursday it will become tuesday.Similar will be logic fo past date
Using ashishanand's logic i made this code.Works fine :-
#include <iostream>
using namespace std;
/*
Harcoded Today's Given Date 27/08/2012
Day Thursday
*/
class date
{
int dd,mm,yyyy;
public:
date(int d,int m,int y)
{dd=d;mm=m;yyyy=y;}
void compute_day();
};
void date :: compute_day()
{
int count_year=0,count_days=0;
int year=yyyy,month=mm,d=dd;
cout<<"Date is "<<d<<"/"<<month<<"/"<<year<<"\n";
while(year != 2012)
{
if(year>2012) // if year is greater compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year+=2;
else
count_year++;
year-=1;
}
else // if year is less compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year-=2;
else
count_year--;
year+=1;
}
}
while(month != 8)
{
if(month>8)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) // if month is greater compute odd days
count_days+=3;
else if(month==2)
;
else
count_days+=2;
month--;
}
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
count_days-=3;
else if(month==2)
;
else
count_days-=2;
month++;
}
}
if(d>27) // if days are greater compute odd days
{ count_days+=(d-27);}
else
{ count_days-=(27-d);}
int gap=(count_days+count_year)%7;
if(gap<0) gap+=7; //mathematical modulo
if(gap==0)
cout<<"Thurday";
else if(gap==1)
cout<<"Friday";
else if(gap==2)
cout<<"Saturday";
else if(gap==3)
cout<<"Sunday";
else if(gap==4)
cout<<"Monday";
else if(gap==5)
cout<<"Tuesday";
else if(gap==6)
cout<<"Wednesday";
else ;
}
int main()
{
date toCheck(26,10,2016); // change it to any date you want to check
toCheck.compute_day();
system("pause");
return 0;
}
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ParseDate {
/**
* Given today is Thursday and 23rd August, 2012. Write a function to input
* a date (future or past) and tell which day it is:- int day_of_week(int
* dd, int mm, int yyyy) Mon -1, Tue - 2, Wed -3......Sun-7
*/
public static int day_of_week(int dd, int mm, int yyyy) {
Calendar cal = new GregorianCalendar();
cal.set(yyyy, mm, dd);
Calendar today = new GregorianCalendar();
today.set(2012, 7, 23);
int diff = 4;
if (today.compareTo(cal) < 0) {
while (today.compareTo(cal) != 0) {
today.add(Calendar.DAY_OF_YEAR, 1);
diff = (diff + 1) % 7;
if (diff == 0) {
diff = 7;
}
}
} else if (today.compareTo(cal) > 0) {
while (today.compareTo(cal) != 0) {
today.add(Calendar.DAY_OF_YEAR, -1);
diff = (diff - 1) % 7;
if (diff == 0) {
diff = 7;
}
}
} else {
;
}
System.out.println(diff);
return 0;
}
public static void main(String[] args) {
day_of_week(1, 6, 2012);
}
}
#include <iostream>
using namespace std;
/*
Harcoded Today's Given Date 27/08/2012
Day Monday
*/
class date
{
int dd,mm,yyyy;
public:
date(int d,int m,int y)
{dd=d;mm=m;yyyy=y;}
void compute_day();
};
void date :: compute_day()
{
int count_year=0,count_days=0;
int year=yyyy,month=mm,d=dd;
cout<<"Date is "<<d<<"/"<<month<<"/"<<year<<"\n";
while(year != 2012)
{
if(year>2012) // if year is greater compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year+=2;
else
count_year++;
year-=1;
}
else // if year is less compute odd days
{
if(year%400==0||(year%4==0&&year%100!=0))
count_year-=2;
else
count_year--;
year+=1;
}
}
while(month != 8)
{
if(month>8)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) // if month is greater compute odd days
count_days+=3;
else if(month==2)
;
else
count_days+=2;
month--;
}
else
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
count_days-=3;
else if(month==2)
;
else
count_days-=2;
month++;
}
}
if(d>27) // if days are greater compute odd days
{ count_days+=(d-27);}
else
{ count_days-=(27-d);}
int gap=(count_days+count_year)%7;
if(gap<0) gap+=7; //mathematical modulo
if(gap==0)
cout<<"Tuesday";
else if(gap==1)
cout<<"wedday";
else if(gap==2)
cout<<"Thursday";
else if(gap==3)
cout<<"friday";
else if(gap==4)
cout<<"satday";
else if(gap==5)
cout<<"sunday";
else if(gap==6)
cout<<"monday";
else ;
}
int main()
{
date toCheck(19,9,2014); // change it to any date you want to check
toCheck.compute_day();
return 0;
}
More concise way:
- tin February 03, 2013just compute the number of days (relative to 0th year) for both the given date and the user-input date separately. Of course needs to take care of leap years in the computation, but that is easy. Then simply do a minus to find the difference in number of days from the given date. Then play around the difference mod 7 to map to a day of week.