## Amazon Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

Including both dates or not?

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

I coded with both dates inclusive.

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

And month apart means 30 days? In this what is the requirements for Feb like months?

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

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.

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

I think you mean 27Feb to 28 March..

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

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.

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

in java

``````public static long dateDiff(Date date1, Date date2) {

long millis = date1.getTime();

long millis1 = date2.getTime();

return (millis - millis1) / (1000 * 60 * 60 * 24);
}``````

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

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

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

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;
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
}
}
}``````

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

Why do we need to use calendar?? Dont we already know the number of days in each month.

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

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

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

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.

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

and what about day saving - it can also move Date from one day to another

and day saving rules are different for different zones and were changed several times

The

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

ok - and how do you know the year?

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

Date without year is not a complete date.
Also the question says that "Write an algorithm that will take two dates and tell you..."

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

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

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

Makes sense.. Use calendar and its API. Problem solved :)

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.