Delve Networks Interview Question
Software Engineer / Developers#include "stdafx.h"
void ProcessYear(int nYear);
void ProcessMonth(int nMonth, int nYear);
void ProcessDay(int nDay, int nMonth, int nYear);
bool Palindrome(char* szDate);
int _tmain(int argc, _TCHAR* argv[])
{
int nStartYear = 0, nEndYear = 0;
printf("Enter the starting year\n");
scanf("%d", &nStartYear );
printf("Enter the Ending year\n");
scanf("%d", &nEndYear );
if((nStartYear > nEndYear) ||
(nStartYear <= 0) ||
(nEndYear <= 0))
{
printf("Incorrect input");
}
else
{
while(nStartYear <= nEndYear)
{
ProcessYear(nStartYear);
nStartYear++;
}
}
return 0;
}
void ProcessYear(int nYear)
{
for(int nMonth = 1; nMonth <= 12; nMonth++)
{
ProcessMonth(nMonth, nYear);
}
}
void ProcessMonth(int nMonth, int nYear)
{
bool bLeapYear = !(nYear / 4);
int nDays = 0;
switch(nMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
nDays = 31;
break;
case 2:
if(bLeapYear)
{
nDays = 29;
}
else
{
nDays = 28;
}
break;
default:
nDays = 30;
break;
}
for(int nDay = 1; nDay <= nDays; nDay++)
{
ProcessDay(nDay, nMonth, nYear);
}
}
void ProcessDay(int nDay, int nMonth, int nYear)
{
char szDate[256] = {0};
char szMonth[128] = {0};
char szDay[128] = {0};
if(nDay < 10 )
{
sprintf(szDay, "0%d", nDay);
}
else
{
sprintf(szDay, "%d", nDay);
}
if(nMonth < 10 )
{
sprintf(szMonth, "0%d", nMonth);
}
else
{
sprintf(szMonth, "%d", nMonth);
}
sprintf(szDate, "%s%s%d", szDay, szMonth, nYear);
if(true == Palindrome(szDate))
{
printf("%s/%s/%d\n", szDay, szMonth, nYear);
}
}
bool Palindrome(char* szDate)
{
bool bRet =true;
char* szTemp = szDate;
int nLength = 0;
while(*szTemp++ != '\0')
{
nLength++;
}
for(int nIndex = 0; nIndex <= (nLength / 2); nIndex++)
{
if(*(szDate + nIndex) != *(szDate + nLength - nIndex - 1))
{
bRet = false;
}
}
return bRet;
}
pseudo code:
for each year y in start to end
reverse y to form daymonth.
check if day month combination is valid for the year y. if so , print as palindrome
so for any given year, we can have atmost one palindrome in that year. order of this algo is O(n) where n = start-end+1
Let ABCD be the start year.
- Ankush December 12, 20091. For i = ABCD to WXYZ step 1
2. Check if 0<DC<13 & 0<BA<32
3. Check if its forming a valid date.
3a) Check with the static array of size 12 which contains the max month, basically to validate boundary values like 31.
3b) if its a leap year, check if its February to validate boundary value 29.
4 If all test passed, print the date
otherwise continue with step 1
Please correct my understanding if required.
Thanks
Ankush