## Tricon Interview Question for Java Developers

Country: India
Interview Type: Written Test

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

Can the dates span over multiple munths and multiple years?

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

yes...but should not count overlapping dates

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

``````if (SDate 2 > SDate 1 && EDate2 > EDate1)
Change EDate1 to EDate2
else if (SDate2 > SDate1 && EDate2 < EDate1)
Do Nothing
else if (SDate2 < SDate1 && EDate2 < EDate1)
Change SDate1 to SDate2
else if (SDate2 < SDate1 && EDate2 < EDate1)
SDate1 = SDate2
EDate1 = EDate2
end if

DateDiff(Edate1,Sdate1)``````

Pass in the two ranges , the second time the SDate and EDate would have moved to the next set of values

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

First of all the difference between 15 and 7 is 8 not 9 so the answer should be 17. And here is the solution
Just keep all the dates in order in an array
arr[]={7,15,12,16,16,24}
Take a diff variable as 0 and add all the differences among consecutive elements as:
Diff=Diff+arr[i+1]-arr[i].
So Diff=8-3+4+0+8=17. Here you can apply the loop as:

``````int diff=0;
for(int i=0;i<n-1;i++)
{
diff=diff+arr[i+1]-arr[i];
}
return diff;``````

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

``````public int returnNoOfDays(String[] givenDates){
int days = 0;
SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
try {
for(int i=0; i<(givenDates.length-1 ); i=i+1){

Date date1 = sdf.parse(givenDates[i]);
Date date2 = sdf.parse(givenDates[i+1]);

days = days+ noOfDays(date1, date2) ;

}
} catch (ParseException ex) {
System.out.println("Dates are not given in correct format.");
}
return (days+1);
}

public int noOfDays(Date d1, Date d2)
{

int days=(int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));

return days;
}``````

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

``````public static void nonOverlappingdateDifferenceInDay(String[] array) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = null;

TreeSet<Date> dates = new TreeSet<Date>();

for (int i = 0; i < array.length; i++) {
String dateStr =  array[i];
date = sdf.parse(dateStr);
}

for (Date sortedDate : dates) {
System.out.println(sortedDate.toString());
}

long dayDiff = (dates.last().getTime() - dates.first().getTime())/ (1000 * 60 * 60 * 24);

System.out.println("Non overlapping date difference : " + dayDiff);
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

package com.lara5;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TestJava {
public static int daysBetween(Date d1, Date d2)
{
/*This function returns the difference in days given 2 dates as input*/

int days=(int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
return days;
}

public static void main(String args[]) throws ParseException
{
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();

SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
Date date = sdf.parse("07122012"); // Date1
cal1.setTime(date);
date = sdf.parse("15122012"); // Date2
cal2.setTime(date);
int daysBetween = daysBetween(cal1.getTime(),cal2.getTime()); // Days between Date1 and Date2
int interval = daysBetween(cal1.getTime(),cal2.getTime());
System.out.println("Interval1 == "+ interval);

date = sdf.parse("12122012"); // Date3
cal1.setTime(date);
date = sdf.parse("16122012"); // Date4
cal2.setTime(date);
daysBetween += daysBetween(cal1.getTime(),cal2.getTime()); // Days between Date3 and Date4 is added to the previously available value of days between Date1 and Date2
interval = daysBetween(cal1.getTime(),cal2.getTime());
System.out.println("Interval2 == "+ interval);

date = sdf.parse("16122012");
cal1.setTime(date);
date = sdf.parse("24122012");
cal2.setTime(date);
daysBetween += daysBetween(cal1.getTime(),cal2.getTime()); // Total required difference as per the question
interval = daysBetween(cal1.getTime(),cal2.getTime());
System.out.println("Interval3 == "+ interval);

System.out.println("Total Interval == "+ daysBetween);
}
}

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.

### 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.