## Microsoft Interview Question

Country: United States

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

do we need to convert the format from "Fridays, 13th" to dd.mm.yyyy or just print dates from 1st jan 1900 ???

Comment hidden because of low score. Click to expand.
0

we need to find and print out all 13th days of a month which fall on Friday (starting from 01.01.1900)

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

Just run a while loop with the condition till the date is equal to current date and in that loop check for Fridays with date thirteen. But then the problem is if its just checking date 13 is fine but how to check which day it is ??

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

@asm
Can I assume being provided a date addition function ?

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

``````const int Mon[12] = {31, 28, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31};

int CalcMonDays(int year, int mon)
{
if (0 == year || 0 == mon)
return 0;

bool bRYear = (0 == year%4 && 0 == year%100);

int i = 0, days = 0;
while (i < mon)
{
if (1 != i)
{
days += Mon[i];
}
else
{
// Check Ryear
days += bRYear?(Mon[i]+1):Mon[i];
}

++i;
}

return days;
}

void Print13thFri(int start_year)
{
int alldays = 0;
int y = 0, m = 0;
int mdays = 0;

while (y < (2012 - start_year))
{
m = 1;
mdays = 1;
while (m <= 12)
{
mdays = CalcMonDays(start_year + y, m - 1);
alldays += mdays;
int t = alldays + 13;

if (5 == t%7)
{
printf("Year: %d, Month %d, 13th is Friday.\n", start_year + y, m);
}

++m;
}

++y;
}
}``````

Comment hidden because of low score. Click to expand.
0

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

Sorry, miss one line,
alldays += CalcMonDays(start_year + y, m - 1); // need to be added above ++y;

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

Another solution..

void printAndCalculateDates(int& lastFriday, int& month, int& year, int numDays)
{
while (lastFriday <= numDays)
{
if (lastFriday == 13)
{
cout << "Date : " << lastFriday << "." << month << "." << year << endl;
}
lastFriday = lastFriday+7;
}

if (lastFriday > numDays)
{
lastFriday = lastFriday - numDays;
month = month+1;
//cout << "Date : " << lastFriday << "." << month << "." << year << endl;
if (month > 12)
{
month = 1;
year = year+1;
}

}

}

void printDate()
{
int currentDay = 10;
int currentYear = 2011;
int currentMonth = 11;
int lastFriday = 5;
int year = 1900;
int month = 01;

while ( (year <= currentYear) )
{
if ((year == currentYear) && (month == currentMonth))
{
if (lastFriday > currentDay)
{
break;
}
}

switch(month)
{
case 1:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}
case 2:
{
int numDays=28;
if (year/4 == 0)
{
numDays = numDays+1;
}
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 3:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}
case 4:
{
int numDays=30;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}
case 5:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}
case 6:
{
int numDays=30;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 7:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 8:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 9:
{
int numDays=30;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 10:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 11:
{
int numDays=30;
printAndCalculateDates(lastFriday, month, year, numDays);
break;
}

case 12:
{
int numDays=31;
printAndCalculateDates(lastFriday, month, year, numDays);

break;
}
}

}
}

int main()
{

printDate();
return 0;
}

Comment hidden because of low score. Click to expand.
0

void printAndCalculateDates(int& lastFriday, int& month, int& year, int numDays)
{
while (lastFriday <= numDays)
{
if (lastFriday == 13)
{
cout << "Date : " << lastFriday << "." << month << "." << year << endl;
lastFriday+=14;
}
else if(lastFriday==14) lastFriday+=14;
lastFriday = lastFriday+7;
}

and another correction assuming someone is running this in 2101:
if (year/4 == 0)
{
if(year%100==0)
{
if(year%400==0)
numDays = numDays+1;
}
else
numDays = numDays+1;
}

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

``````#include<stdio.h>
#include<iostream>

using namespace std;

int leap[12] = { 31,29,31,30,31,30,31,31,30,31,30,31};
int nonleap[12] = { 31,28,31,30,31,30,31,31,30,31,30,31};

int check(int year)
{
if(year % 4 != 0)
return 0;
if(year % 100 == 0)
{
if(year % 400 == 0)
return 1 ;
else
return 0;
}
return 1;
}

int main()
{
int year = 1900 ;
int curr = 2011 ;
int day = 5 ;  // Saturday
int i ;

while(year <= curr)
{
if(check(year))
{
for(i = 0; i < 12 ; i++)
{
day = day + leap[i];
day = day %  7;
if(day  == 4)
{
if(i < 11)
printf("leap : 13 %d %d\n",i+2,year);
else
printf("leap : 13 1 %d\n",year+1);
}
}
}
else
{
for(i = 0 ; i <12 ; i++)
{
day = day + nonleap[i] ;
day = day % 7 ;
if(day == 4)
{
if(i < 11)
printf("nonleap : 13 %d %d\n",i+2,year);
else
printf("nonleap : 13 1 %d\n",year+1);
}
}
}
year ++ ;
}
return 0;
}``````

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

``````#include<stdio.h>
#include<string.h>
int isLeapYear(int);
int main()
{
//int cent_code=0;
int months[12]={1,4,4,0,2,5,0,3,6,1,4,6};
int year_code,leap_code;
int n,i;
int date=13;
int day=6;
int leap_flag,result,val,count=0;
printf("%d\t%d\t%d\n",isLeapYear(1900),isLeapYear(2000),isLeapYear(1797));
for(n=1900;n<=2012;n++)
{
leap_flag=isLeapYear(n);
year_code=n%100;
leap_code=year_code/4;
result=year_code+leap_code;
for(i=0;i<12;i++)
{
if(leap_flag==1 && (i==0||i==1))
val=result+months[i]-1;
else
val=result+months[i];

if (n<2000) {
if((val+date)%7==day&&++count)
printf("%2d-%2d-%4d\n",date,i+1,n);
}
else
{
if((val+date-1)%7==day&&++count)
printf("%2d-%02d-%4d\n",date,i+1,n);
}

}
}
printf("\n\nTotal Possibilities:%d",count);
getchar();
return 0;
}``````

isLeapYear returns 1 if year is leap year else returns zero

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

We can use the following math formula to calculate the day of date.
n = d+2m+[3(m+1)/5]+y+[y/4]-[y/100]+[y/400]+2
n=n%7
[] suggests integer division
if n=1, then sunday and n=2 monday and so on...

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

static int gs_Month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

void Output()
{
int nCurrent = 13;
int nMonth = 1;
int nYear = 1900;

for(;nYear <= 2012;)
{
if(5 == nCurrent % 7 )
{
printf("%d/%d/%d\n", nMonth, 13, nYear);
}

nCurrent += gs_Month[nMonth -1];

if( (2 == nMonth )
&&
( 0 == (nYear % 4) )
)
{
if( (0 == nYear % 100) )
{
if(0 == nYear % 400)
{
++nCurrent;
}
}
else
{
++nCurrent;
}
}

++nMonth;

if(13 == nMonth)
{
++nYear;
nMonth -= 12;
}
}
}

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

A major point is that 1900 was governed by non-gregorian calendar in many countries, so the leap year won't work properly. W/o the location rules the problem is not solvable.

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.

### 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.