Amazon Interview Question for Software Engineer in Tests






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

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 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's a good trick, if the question changes to
How to UNIX calculate the milliseconds spend fater 1970...

- OY October 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- 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?

- YetAnotherCoder October 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- 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?

- YetAnotherCoder October 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do not fgt the leap year

- ProblemDissolver October 05, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- PRJ October 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- jr June 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

thnx

- xyz October 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- Java Coder October 11, 2008 | 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