Amazon Interview Question
Software Engineer in Tests- What is the format of the date used as an input.
- Are we allowed to use predefined Date Time functions. Or We need to parse it?
- Parsing method
- 15 July 2004 - 17 August 2006
(2006-2004) years *365 + for(i=2004 to 2007) YearMap[i] + (8-7) *30 + for(i = july to aug-1) monthmap[i] + (17-15) *1
- D1 M1 Y1 - D2 M2 Y2
= (Y2-Y1)*365 + for(i = Y1 to Y2-1) Yearmap[i]
+ (M2-M1)*30 + for(i = M1 to M2-1) Monthmap[i]
+(D2-D1)*1
This might give incorrect result.
Any Better algorithm?
- What is the format of the date used as an input.
- Are we allowed to use predefined Date Time functions. Or We need to parse it?
- Parsing method
- 15 July 2004 - 17 August 2006
(2006-2004) years *365 + for(i=2004 to 2007) YearMap[i] + (8-7) *30 + for(i = july to aug-1) monthmap[i] + (17-15) *1
- D1 M1 Y1 - D2 M2 Y2
= (Y2-Y1)*365 + for(i = Y1 to Y2-1) Yearmap[i]
+ (M2-M1)*30 + for(i = M1 to M2-1) Monthmap[i]
+(D2-D1)*1
This might give incorrect result.
Any Better algorithm?
1. Parse both the dates, Separate Day of month, month and year
2. For each date calcuate how many days are remaning for it to
make the end of the year, for instance start date need 'x'
days to meet the end of the year, end date needs 'y' more days
3. Calcuate the number of years by calcuating the difference between
the year value of start date and end date, let number of years be
'z'. Calculate z/4 = m which gives number of leap years in the
duration
4. The final value can be calcuated as
days = 365*(z-m) + 366*m + x - y
z/4 will not detect all leap years. eg. 2004 to 2006 ..z would be 2, and m = 0. but 2004 is a leap year. it would be better to find the first leap year (divisible by 4) from the starting year and then keep adding 1 to leap_year_count while start_year+4 < end_year
Otherwise a good solution.
package google;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Dates {
private Date d1 = new Date();
private Calendar c1;
private Calendar c2;
private int numYears;
private int numMonths;
private int numDays;
private void initDates(){
c1 = new GregorianCalendar(2006,06,11);
c2 = new GregorianCalendar(2008,06,21);
}
private int getNumYears(){
int numYears = Math.abs(c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR));
return numYears;
}
private int getNumDays(){
int dayOfTheYear = c1.get(Calendar.DAY_OF_YEAR);
int daysRemaining = 365 - c1.get(Calendar.DAY_OF_YEAR);
return daysRemaining;
}
private int getDiff(){
int dayOfTheYearC1 = c1.get(Calendar.DAY_OF_YEAR);//DAY_OF_YEAR;
int dayOfTheYearC2 = c2.get(Calendar.DAY_OF_YEAR);
int diff = dayOfTheYearC2 - dayOfTheYearC1;
return diff;
}
private int calcNumDays(){
Integer numYears = getNumYears();
int numDays = 0;
if(numYears == null){
System.out.print("Null Number of Years!");
return 0;
}
while(numYears > 1){
numDays = numDays + 365;
numYears--;
}
if(numYears == 1){
int numDaysinThisyear = getNumDays();
int numDaysTotal = numDaysinThisyear + c2.get(Calendar.DAY_OF_YEAR);
return numDays + numDaysTotal;
}
else {
numDays = numDays+getDiff();
return numDays;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Dates obj = new Dates();
obj.initDates();
int numDays = obj.calcNumDays();
System.out.println(numDays);
}
}
For each Date find the number of milliseconds spend after 1970 i.e. unix time. Take the difference and convert into number of days.
- DateDifferenciator October 05, 2008