Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

boolean isOneMonthApart(Date d1, Date d2){
	if(Math.abs(d1.year - d2.year)>=2 || Math.abs(d1.month%12 - d2.month%12)>=2){
		return false;
	}
	
	if(Math.abs(d1.year - d2.year) == 1){
		Date small = d1.year < d2.year ? d1 : d2;
		Date large = small == d1 ? d2 : d1;
		return small.month == 12 && large.month == 1 && small.day >= large.day;
	}else{
		Date small = d1.month <= d2.month ? d1 : d2;
		Date large = small == d1 ? d2 : d1;
		return large.month == small.month || large.month-small.month == 1 && small.day >= large.day;
	}
}

- lizhong207 September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i think we need a better definition of "month". is it 30 days?

- Miguel Oliveira September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Let's suppose "one month apart" means two dates have only a difference of 1 in their mm modulo 12, That is 9/29/13 is 1 month apart from 8/29/13 as well as 10/29/13, and so is 12/29/13 1 month apart from 1/29/14.

- Rangarajan Chari September 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

And I should add only a difference of 1 in their yy's.

- Rangarajan Chari September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if((d2==d1) && ((y2*12+m2)==(y1*12+m1+1)))

- ys September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will fail.
For eg. if d1= 31 January and d2= 28 Feb , even though d1 and d2 are not same but still both are 1 month apart.

- GT1 October 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

///
long difference = Math.abs(date1.getTime() - date2.getTime());
boolean isThirtydays = difference / (1000 * 60 * 60 *24) == 30 ? true : false;
\\\

- Anonymous October 10, 2013 | Flag Reply


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