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?

- King@Work April 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I coded with both dates inclusive.

- JSDUDE April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- King@Work April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- JSDUDE April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you mean 27Feb to 28 March..

- King@Work April 08, 2013 | Flag
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.

- JSDUDE April 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- guest April 09, 2013 | Flag
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.

- King@Work April 08, 2013 | Flag Reply
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;
		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
		}
	}
}

- gena April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- King@Work April 11, 2013 | Flag
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

- gena April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- King@Work April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- gena April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ok - and how do you know the year?

- gena April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- King@Work April 11, 2013 | Flag
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

- gena April 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- King@Work April 11, 2013 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More