Globaltech Research Interview Question
Software Engineer / DevelopersTake the input of Month, Year, Day as ints
int MDS= (Year-1)*365 + parseInt((Year-1)/4) + (Month-1)*30 + (Day - 11)
int COR = parseInt((Year-1)/100) - 16
int (COR>0) {MDS = MDS-COR}
int CO = parseInt((Year-1)/400) - 4
if (CO>0) {MDS = MDS+CO}
int X=0
for (int i = 1; i<=(Month - 1); i++)
{
X=X+1
if (X==1) {MDS= MDS+1}
if (X==3) {MDS= MDS+1}
if (X==5) {MDS= MDS+1}
if (X==7) {MDS= MDS+1}
if (X==8) {MDS= MDS+1}
if (X==10) {MDS= MDS+1}
if (X==12) {MDS= MDS+1}
if (X==2)
{
if (Year % 4 == 0)
{
if(Year>1752)
{
if (Year % 100 == 0)
{
MDS=MDS-2
if (Year%400==0) {MDS=MDS+1}
}
else
{MDS=MDS-1}
}
else
{MDS=MDS-1}
}
else
{MDS=MDS-2}
}
}
if (MDS % 7 == 0) {"Saturday"}
if (MDS % 7 == 1) {"Sunday"}
if (MDS % 7 == 2) {"Monday"}
if (MDS % 7 == 3) {"Tuesday"}
if (MDS % 7 == 4) {"Wednesday"}
if (MDS % 7 == 5) {"Thursday"}
if (MDS % 7 == 6) {"Friday"}
Given a date, Count #days to 1/1/2000, mod (that is, %) it by 7, lookup the corresponding week-day (implement simple num2day() fn) and print. The key is that the lookup should assume 'Monday' in first position.
- Nix October 17, 2009This is highly simplified by assuming 30-day months.
Say if date is 4/11/2009, we'll count days like this:
> (4-1)*30 + 11 + (2009-2000)*12*30 = 90 + 11 + 9*12*30 = 3341.
> 3341 % 7 = 2 ===> Tuesday.
Is it difficult to code?