## Amazon Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**Phone Interview

So i assumed one month as:

Date of One month to the Same Date of the next month, irrespective of the month.

So 27th Feb and 28th Feb are exactly one month apart.

Sorry I meant 27th Feb and 27th March are exactly one month apart.

This resolves the Leap year issue, 30/31 day month issue.

Apologies for the earlier post.

```
if(sameYearDates){
long daysNum = |CountDaysFromJan1(d1)-CountDaysFromJan1(d2)|;
if(daysNum<30) // less a month
else if (daysNum > 30) // greater than a month
else // excatly a month
}
else if(Y1-Y2==1) { // assuming y1 is before y2 put some more code to arrange y1 y2
if(Y1IsLeap)
int days1 = 366-CountDaysFromJan1(d1);
else
days1 = 365-CountDaysFromJan1(d1);
int days2 = CountDaysFromJan1(d1);
int daysNum = days1 +days2 ; // apply same logic like above for finding (>,<,==)month
}
else
// more than a month
```

A lot of code reuse can be done on this.. on doing it right now coz in hurry.

-- Second approach is if API is available get the long milliseconds and then do the math.

i suspect you need to use Calendar:

```
import java.util.*;
public class DatesCompare {
private static Calendar calendar = Calendar.getInstance();
public static int compare(Date date1, Date date2) {
calendar.setTime(date1);
int m1 = calendar.get(Calendar.YEAR) * 12
+ calendar.get(Calendar.MONTH);
int d1 = calendar.get(Calendar.DAY_OF_MONTH);
calendar.setTime(date2);
int m2 = calendar.get(Calendar.YEAR) * 12
+ calendar.get(Calendar.MONTH);
int d2 = calendar.get(Calendar.DAY_OF_MONTH);
switch (Math.abs(m1 - m2)) {
case 0: // the same month -> dates are too close to each other
return -1;
case 1: // adjacent months
if (d1 == d2) // exactly one month difference!
return 0;
if (m1 < m2)
if (d1 < d2)
return 1; // dates are too far away from each other
else
return -1; // dates are too close to each other
else if (d1 > d2)
return 1; // dates are too far away from each other
else
return -1; // dates are too close to each other
default:
return 1; // dates are too far away from each other
}
}
}
```

How do you know the number of days in the month?

E.g. is it leap year or not?

If you start thinking about it you will need to write your Calendar class - and it is not as simple as it seems

Its Simple.. all month days are fixed. Leap year calculation is single line. O(1)

```
int leapyear (int yr)
{
if ((yr % 4 == 0) && !(yr % 100 == 0))
cout<<yr;
else if (yr % 400 == 0) // year divisible by 4 but not by 100
cout<<yr;
return yr;
}
```

I agree that if we need number of milli-seconds and support different time zones then writing calendar is complex but in this case none of that is required.

Date without year is not a complete date.

Also the question says that "Write an algorithm that will take two dates and tell you..."

Date is wrapper around long time

it may be unix time or different time - but it is point on time axe

Different calendars treat this point time very differently when they deduce year, month and date

My point is very simple - it is very undedfinrd question and if you want to answer it right you have to take into account a LOT

Including both dates or not?

- King@Work April 08, 2013