Epic Systems Interview Question
Software Engineer / Developerswhy can't we give only (currentYear%4==0) as the required condition, instead of ((currentYear%400==0)||((currentYear%100!=0)&&(currentYear%4==0)))?
I think I got a pretty good sol. (java)
//find the palindrome dates b/w yr1 and yr2 in format MMDDYYYY
public class GetPalindromeDate {
/**
* @param args
*/
public static void main(String[] args) {
printPalindromes(0, 8000);
}
private static void printPalindromes(int start, int end) {
// 00022000
int[] daysMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int curYr = start; curYr < end; curYr++) {
// every year there is only one palindrome => just check if the
// palyndrome is a valid date
// 2010
// last digit third digit
int day = curYr / 1000 + (curYr / 100 - curYr / 1000 * 10) * 10;
// first digit second digit*10
int month = curYr % 10 * 10 + (curYr % 100 - curYr % 10) * 10;
// check if the day and month is legal
boolean monthValid = month > 0 && month <= 12;
if (monthValid) {
boolean dayValid = day > 0
&& (day < daysMonth[month] || (curYr % 4 == 0
&& month == 2 && day == 29));
if (dayValid) {
System.out.printf("P %02d%02d %04d\n", month, day, curYr);
}
} else {
// System.out.printf("%02d%02d %04d\n", month, day, curYr);
}
}
}
}
<pre lang="" line="1" title="CodeMonkey68043" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey68043" input="yes">//Java class
class Palindrome{
private static int[] days = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private boolean isLeap(int year){
if ((year%4) != 0 || ((year%100 == 0) && (year%400) 1= 0)
return false;
return true;
}
private int getDays(int year, int month){
if (month == 2 && isLeap(year)
return days[month} + 1;
return days[month];
}
private String padding2(int dm){
if (dm < 10)
return "0" + dm;
return "" + dm;
}
public void printPalindrome(int startYear, int endYear){
for (int year = startYear; year <= endYear; year++){
int tempMonth = year % 100;
int tempDay = year / 100;
int mm = (tempMonth%10)*10 + tempMonth%10;
int dd = (tempDay%10)*10 + tempDay%10;
if (mm > 0 && mm < 13 && dd > 0 && dd <=getDays(year, mm))
System.out.println(padding(mm) + padding(dd) + year);
}
}
public void testPrintPalindrome(){
printPalindrome(2000, 2010);
}
}</pre>
<pre lang="" line="1" title="CodeMonkey26579" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
palinDate(0, 9999);
}
}
public boolean palinDate (int num1, int num2){
for(int i=num1;i<=num2;i++){
for(int mm = 1; mm <= 12; mm++) {
for(int dd = 1; dd <= 31; dd++) {
if(dateValid(mm,dd, i)) {
if(checkPalin(mm, dd, i) {
System.out.println(mm+" / "+dd+" / "+i);
}
}
}
}
}
}
public boolean dateValid(int mm, int dd, int yyyy) {
if(yyyy < 0) {
return false;
}
if (leapYear(yyyy) && mm == 2) {
if (dd <= 29) return true;
else return false;
}
if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
if (dd <= 30) return true;
else return false;
}
}
public boolean leapYear (int yyyy) {
return (yyyy%400 == 0 || yyyy%4 == 0)
}
public boolean checkPalin (int mm, int dd, int yyyy) {
String date = Integer.toString(mm) + Integer.toString(dd) + Integer.toString(yyyy);
int len = date.length();
for(int i=0; i<len/2; i++) {
if(date.charAt(i) != date.charAt(len-i-1))
return false;
}
return true;
}
</pre><pre title="CodeMonkey26579" input="yes">
</pre>
#include<stdio.h>
main()
{ int i,j,n,k, s=0,m;
int tmp=0;
printf("Enter the date in dd/mm/yyyy format \n");
scanf("%d/%d/%d",&j,&k,&n);
s=n+10000*k+ 1000000*j;
m=s;
while(s>0)
{ i=s%10;
s=s/10;
tmp=tmp*10 +i;
}
if(m==tmp)
printf("The date is pallindrome\n");
else
printf("The date is not pallindrome\n");
}
<pre lang="java"> #include<stdio.h>
main()
{ int i,j,n,k, s=0,m;
int tmp=0;
printf("Enter the date in dd/mm/yyyy format \n");
scanf("%d/%d/%d",&j,&k,&n);
s=n+10000*k+ 1000000*j;
m=s;
while(s>0)
{ i=s%10;
s=s/10;
tmp=tmp*10 +i;
}
if(m==tmp)
printf("The date is pallindrome\n");
else
printf("The date is not pallindrome\n");
}
- @123 o </pre>
#include<stdio.h>
main()
{ int i,j,n,k, s=0,m;
int tmp=0;
printf("Enter the date in dd/mm/yyyy format \n");
scanf("%d/%d/%d",&j,&k,&n);
s=n+10000*k+ 1000000*j;
m=s;
while(s>0)
{ i=s%10;
s=s/10;
tmp=tmp*10 +i;
}
if(m==tmp)
printf("The date is pallindrome\n");
else
printf("The date is not pallindrome\n");
}
not the previous one that i posted. Its by mistake.. :p
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int leap(int y)
{
if(y%400==0||y%4==0)
return 1;
else return 0;
}
int valid(int dd,int mm, int yyyy)
{
if(mm==2)
{
if(leap(yyyy))
{
if(dd<=29)
return 1;
else return 0;
}
else
{
if(dd<=28)
return 1;
else return 0;
}
}
else if(mm==4||mm==6||mm==9||mm==11)
{
if(dd<=30)
return 1;
else return 0;
}
else return 1;
}
int palin(int dd, int mm, int yyyy)
{
int y1,y2,i;
char str1[5],str2[5],str3[5];
int flag1=100,flag2=100;
y1=yyyy/100;
if(mm<10)
{
mm=10*mm;
if(mm==y1)
flag1=1;
}
else if(mm==10)
{
if(y1==1)
flag1=1;
}
else
{
itoa(mm,str1,10);
strrev(str1);
mm=atoi(str1);
if(mm==y1)
flag1=1;
}
y2=yyyy%100;
if(y2<10)
{
y2=y2*10;
if(dd==y2)
flag2=1;
}
else if(y2==10)
{
if(dd==1)
flag2=1;
}
else
{
itoa(y2,str1,10);
strrev(str1);
y2=atoi(str1);
if(dd==y2)
flag2=1;
}
if(flag1==1&&flag2==1)
return 1;
else
return 0;
}
void main()
{
int dd,mm,yyyy,st,en;
clrscr();
printf("\n Enter the starting year\t");
scanf("%d",&st);
printf("\n Enter the ending year.");
scanf("%d",&en);
for(yyyy=st;yyyy<=en;yyyy++)
{
for(mm=1;mm<=12;mm++)
{
for(dd=1;dd<=31;dd++)
{
if(valid(dd,mm,yyyy))
{
if(palin(dd,mm,yyyy))
{
printf("\n %d/%d/%d is a palindrome.",dd,mm,yyyy);
}
}
}
}
}
getch();
}
working java code
public class PalindromeDates {
static int[] mon = {0, 31,28,31,30,31,30,31,31,30,31,30,31};
public static void genPalin(int y) {
String mm = Integer.toString((y%1000)/100) + Integer.toString(y/1000);
String dd = Integer.toString(y%10) + Integer.toString((y/10)%10);
int m = Integer.parseInt(mm);
int d = Integer.parseInt(dd);
String yyyy = Integer.toString(y);
if (m>0 && m<13) {
if (d>0 && d <= (m != 2 ? mon[m] : (y%4!=0) ? 28:29))
System.out.println(dd+mm+yyyy);
}
}
public static void main(String args[]) {
int y1 = 2001;
int y2 = 2040;
for(int i=y1;i<=y2;i++)
genPalin(i);
}
}
Here is a working code in C++. It is clearly mentioned in the problem statement that the format is MMDDYYYY, so I have considered that all the years will be represented by 4 digits, months and days by 2 digits.
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int dd, mm, var, i, yyyy1,yyyy2, yy1, arr1[5], mon[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cin>>yyyy1>>yyyy2;
var=yy1;
while (yyyy1<=yyyy2)
{
var=yyyy1;
while (var!=0)
{
arr1[i]=var%10;
var/=10;
i++;
}
mm=arr1[0]*10+arr1[1];
dd=arr1[2]*10+arr1[3];
if ((yyyy1%4==0 && yyyy1%100!=0) || (yyyy1%400==0))
{
if (((dd<=mon[mm-1]) || (mm==2 && dd==29)) && (mm<=12) && (mm>0) && (dd>0))
{
cout<<arr1[0]<<arr1[1]<<'-'<<arr1[2]<<arr1[3]<<'-'<<yyyy1<<endl;
}
}
else
{
if (dd<=mon[mm-1] && (mm<=12) && (mm>0) && (dd>0))
{
cout<<arr1[0]<<arr1[1]<<'-'<<arr1[2]<<arr1[3]<<'-'<<yyyy1<<endl;
}
}
yyyy1++;
i=0;
}
return 0;
}
public class PalindromeDates {
public static void main(String[] args) {
printPalidromeDates(2010, 2050);
}
static void printPalidromeDates(int start, int end) {
for (int yyyy = start; yyyy <= end; yyyy++) {
String mm = Integer.toString(yyyy % 10)
+ Integer.toString((yyyy / 10) % 10);
String dd = Integer.toString((yyyy / 100) % 10)
+ Integer.toString((yyyy / 1000) % 10);
int m = Integer.parseInt(mm);
int d = Integer.parseInt(dd);
if (m >= 1 && m <= 12) { // Valid month.
if (d >= 1) {// Valid lower limit for day.
// Upper limit must be less than number of days in each
// month (including leap year)
boolean validDay = false;
int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
if (m == 2) { // Since its Feb, we need to check for leap
// year.
if ((yyyy % 4 == 0 && yyyy % 100 != 0)
|| yyyy % 400 == 0) {
if (d <= 29) {
validDay = true;
}
} else {
if (d <= days[m - 1]) {
validDay = true;
}
}
} else { // Not feb, just pick days from the array.
if (d <= days[m - 1]) {
validDay = true;
}
}
if (validDay) { // Valid day && Valid month
System.out.println(mm + "/" + dd + "/" + yyyy);
}
}
}
}
}
}
public class PalindromeDate {
public static void main(String[] args) {
printDates(1000, 3010);
}
public static void printDates(int start, int end) {
int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int year = start; year <= end; year++) {
String yyyy = Integer.toString(year);
String mm = Integer.toString(year%10)+Integer.toString((year/10)%10);
String dd = Integer.toString((year/100)%10)+Integer.toString((year/1000)%10);
int m = Integer.parseInt(mm);
int d = Integer.parseInt(dd);
if (m > 0 && m < 13) {
if (d > 0 && d <= ((m-1 != 2)? daysInMonth[m-1]: (year%4 != 0)?28:29)) {
System.out.println(mm+dd+yyyy);
}
}
}
}
}
Why not just use year%100 to get month and then year/100 %100 to get the day? Why bother doing all the changes? But great logic, with a typo of m-1!=2. And also, leap year is not just %4=0, don't forget those years such as 1900
I found my mistake in understanding the change between month and year...But zeros can always be added anyway. Thanks for shareing.BTW, my friend just got tested by this one.
import java.io.*;
class datepalindrome
{
public static void main(String [] args)
{
int start=2000;
int end=3010;
int dd,mm;
int currentyr;
int[] month={31,28,31,30,31,30,31,31,30,31,30,31};
for (int i=start;i<end;i++)
{
currentyr=i;
mm=currentyr%100;
dd= currentyr/100;
if((i%400)==0||(i%100)==0&&(i%4)==0&&dd<=30&&mm==2)
{
System.out.println(mm+ "/"+dd+"/"+i);
}
else if(mm<13&&mm>0&&dd<month[mm-1])
{
System.out.println(mm+ "/"+dd+"/"+i);
}
else
{
continue;
}
}
}
}
import java.io.*;
class datepalindrome
{
public static void main(String [] args)
{
int start=2000;
int end=3010;
int dd,mm;
int currentyr;
int[] month={31,28,31,30,31,30,31,31,30,31,30,31};
for (int i=start;i<end;i++)
{
currentyr=i;
mm=currentyr%100;
dd= currentyr/100;
if((i%400)==0||(i%100)==0&&(i%4)==0&&dd<=30&&mm==2)
{
System.out.println(mm+ "/"+dd+"/"+i);
}
else if(mm<13&&mm>0&&dd<month[mm-1])
{
System.out.println(mm+ "/"+dd+"/"+i);
}
else
{
continue;
}
}
}
}
Taking the year 2010, According to the above code it shud be 10202010. which is not a palindrome
I was thinking on the same lines.
@Arjune - For years less than 1000 and > 9999
we need to parse the year as strings ans then find if it is a palindrome.
For eg, year 211
1-12-211 and 11-2-211, both work.
In cases like these we might have to parse it from the last character of the year string, say first take '1' and the rest of the string '12', now find if they are valid date and month.
Then again now take '11' and '2', they are both valid date and month.
Now in the 3rd iteration we reach the length of the string and we STOP.
My question is do we have to consider dates like 1-1-211?? this is still a palindrome.
For years > 9999, needs some other technique I guess.
Has anybody accounted for dates that are <1000 and >9999.
What if the input is palindrome(10, 620)??
//find the palindrome dates b/w yr1 and yr2 in format MMDDYYYY
public class GetPalindromeDate {
/**
* @param args
*/
public static void main(String[] args) {
printPalindromes(0, 8000);
}
private static void printPalindromes(int start, int end) {
// 00022000
int[] daysMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int curYr = start; curYr < end; curYr++) {
// every year there is only one palindrome => just check if the
// palyndrome is a valid date
// 2010
// last digit third digit
int day = curYr / 1000 + (curYr / 100 - curYr / 1000 * 10) * 10;
// first digit second digit*10
int month = curYr % 10 * 10 + (curYr % 100 - curYr % 10) * 10;
// check if the day and month is legal
boolean monthValid = month > 0 && month <= 12;
if (monthValid) {
boolean dayValid = day > 0
&& (day < daysMonth[month] || (curYr % 4 == 0
&& month == 2 && day == 29));
if (dayValid) {
System.out.printf("P %02d%02d %04d\n", month, day, curYr);
}
} else {
// System.out.printf("%02d%02d %04d\n", month, day, curYr);
}
}
}
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
/* Function declarations */
void findPalindroms(string year1,string year2);
void printPalindrom(int year);
bool isValidDate(string mm,string dd,string year);
bool isLeapYear(int year);
/* Function definitions */
void findPalindroms(string year1,string year2){
int diff=atoi(year2.c_str())-atoi(year1.c_str());
cout<<diff<<endl;
for(int i=0; i<=diff; i++)
{
printPalindrom(atoi(year1.c_str())+i);
}
}
void printPalindrom(int year){
stringstream ss;
ss<<year;
string year_string=ss.str();
string dd,mm;
int i=0;
for(int i=year_string.length()-1; i>=2; i--)
{
mm.append(1,year_string.at(i));
}
for(int i=1; i>=0; i--)
{
dd.append(1,year_string.at(i));
}
bool isValid=isValidDate(mm,dd,year_string);
if(isValid)
cout<<mm<<dd<<year_string<<endl;
}
bool isValidDate(string mm,string dd,string year_string){
int month=atoi(mm.c_str());
int date=atoi(dd.c_str());
int year=atoi(year_string.c_str());
if(month==1 || month ==3 || month==5 || month==7 || month==8 || month==10 || month==12)
if(date>0 && date <=31)
return true;
else
return false;
if(month==4 || month ==6 || month==9 || month==11)
if(date>0 && date <=30)
return true;
else
return false;
if(month==2)
if(isLeapYear(year))
if(date>0 && date<=29)
return true;
else
return false;
else
if(date>0 && date<=28)
return true;
else
return false;
}
bool isLeapYear(int year)
{
if(year%400==0)
return true;
else if(year%100==0)
return false;
else if(year%4==0)
return true;
else
return false;
}
int main()
{
char key;
string year1, year2;
cout<<"Enter two years in YYYY format"<<endl;
cin>>year1;
cin>>year2;
findPalindroms(year1,year2);
cin>>key;
return 0;
}
<pre lang="java" line="1" title="CodeMonkey90276" class="run-this">//this is a refinement of the above code with proper calculation of leapyear
- Anonymous August 21, 2010class PalindromeYear{
public void printPalindrome(int year1, int year2){
int[] month={31,28,31,30,31,30,31,31,30,31,30,31};
String mm="";
String dd="";
String yy="";
int m=0;
int d=0;
for(int currentYear=year1; currentYear<=year2; currentYear++){
if((currentYear%400==0)||((currentYear%100!=0)&&(currentYear%4==0))) month[1]=29;
yy=Integer.toString(currentYear);
mm=Integer.toString(currentYear%10)+Integer.toString((currentYear/10)%10);
dd=Integer.toString((currentYear/100)%10)+Integer.toString((currentYear/1000)%10);
m=Integer.parseInt(mm);
d=Integer.parseInt(dd);
if(m>0 && m<=12 && d>0 && d<=month[m-1]) System.out.println(mm+dd+yy);
}
}
public static void main(String[] args){
PalindromeYear py=new PalindromeYear();
py.printPalindrome(2000,2060);
}
}</pre><pre title="CodeMonkey90276" input="yes">
</pre>