Amazon Interview Question for Software Engineer / Developers






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

Assume these two functions :
1. addMonths(date, n): Adds exactly 'n' months to 'date'
2. compare(date1, date2): Returns -1, 0, 1 depending on the date values
These functions are very easy to code. Then the solution for the problem will be the following function:

// this function returns
// -1 if the two dates are less than one month apart
// 0 if the two dates are exactly one month apart
// 1 if the two dates are more than one month apart
int compareDateMonths(Date date1, Date date2)
{
Date earlier;
Date later;
int comparison = compare (date1, date2);
if ( comparison == 0)
{
return -1;
}
else if (comparison == -1)
{
earlier = date1;
later = date2;
}
else
{
earlier = date2;
later = date1;
}

addMonths(earlier, 1);

return -(compare(earlier, later));
}

- Anonymous October 14, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the point should be to minimize the number of arithematic and comparison operations, not just get the right answer. So a number of pruning steps are required to return false.

1. if difference between years >= 2 then false
2. if difference between month names >= 2, like "Jan" and "Apr" (1 and 4) then false

now you serialize the date, by 1 being the first date of the smaller month, +30 (or whatever) being the first date of the second month (include difference in year too).. then convert both dates into this format, subtract, and see if its less than 30 days..

Also cases like February, with 28 days, might need to be special cases.

- Matchless February 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

find the date which is less than other
add 30 days to it say date3

now compare it to the other

if they are equal (exactly 30 days apart)
if date3 is greater (less than 30 days apart)
if date3 is lesser( more than 30 days apart)

- Ravi Kant Pandey May 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Matchless makes sense!
How about situation: 02/01/07 - 03/01/07?
They are month apart but above algorithm would return false!

- $ May 31, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Date1:
D1YY
D1MM
D1DD

Date2:
D2YY
D2MM
D2DD

If |D2YY - D1YY| > 1 => More than month apart
If |D2YY - D1YY| = 0 => Same Year
If |D2MM - D1MM | > 1 => More than a month apart
If |D2MM - D1MM| = 0 => Same month => Less than month apart
If |D2MM - D1MM| = 1
If |D2DD - D1DD| = 0 => Month Apart
If D2MM > D1MM
If D2DD > D1DD => More than a month
else => Less than a month
else
If D1DD > D2DD => More than a month
else => Less than a month

and so on... it is so tiring to write all this :)

- $ May 31, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will not work in the case, if for example we have months Feb and April .. technically Feb is the 2nd month, and April is 4th month ... hence the difference is 2 > 1 .. however, if we had dates like

Feb 28th, April 1st .. those are a month apart

- selekt November 07, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

example:
dd/mm/yyyy
-Assuming no leap years
-All months have 30 days

date1 = 04/06/2007
date2 = 09/07/2007

compute date1 and date2 as follows:
date1 = 2006*365 + 6*30 + 4
date2 = 2006*365 + 7*30 + 9

check modulus of |date2 - date1| > 30

- Sidharth August 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is too general....I guess a better solution should check all the cases.

- Daniel Johnson February 14, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we are allowed to use a method that given a date converts it into number of seconds from Jan 1 1970.

And then consider a month as 4 weeks = 28 days and just check if the difference between the dates in 28 days?

If we need to be strict abt 30,31,28 write up some if statements...

- mp December 09, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its a good and smart solution but it can error.

suppose we want to find number of days between 12/5/1699 and 1/1/1704.

By using system time, you port both the time to the current times so that it fits within our 1970 case.

So first date becomes 12/5/1970 and second one 1/1/1975. Here there is a leap year between these dates i.e. 1972. In the original dates there is no leap year since 1700 is not a leap year.

- Daniel Johnson February 25, 2008 | 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