## InvestCloud Interview Question

Integration Software Engineers**Country:**United States

**Interview Type:**In-Person

EDIT: I misread your code and thought you were checking == 0 not != 0 when checking 4. It looks like yours works, my mistake.

I don't think your leap year calculation is correct. You need nested if statements for the check.

This is because if the year is divisible by 4 and not by 100 it IS a leap year.

```
if(year % 4 == 0)
{
leap = true;
if(year % 100 == 0)
{
leap = false;
if(year % 400 == 0)
{
leap = true;
}
}
}
```

or smush them all into one statement

`(year % 4 == 0 && year % 100 != 0) || ( year % 400 == 0)`

Consider the following code:

```
public class DateUtils {
static final int IDX_YEAR = 0;
static final int IDX_MONTH = 1;
static final int IDX_DAY = 2;
static int[] daysCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static boolean isLeapYear(int year){
return year % 4 == 0 && year % 10 != 0;
}
public static int getDayOfYear(String date){
String[] parts = date.split("-");
int year = Integer.parseInt(parts[IDX_YEAR]);
int month = Integer.parseInt(parts[IDX_MONTH]) - 1;
int day = Integer.parseInt(parts[IDX_DAY]);
boolean leapYear = isLeapYear(year);
int dayOfYear = 0;
for (int m = 0; m < month; m++){
dayOfYear += daysCount[m];
//february
if (m == 1 && leapYear){
dayOfYear++;
}
}
dayOfYear += day;
return dayOfYear;
}
}
```

Can you explain why are you substracting 1 from month ? I understood that not doing that won't give correct result but still I am wondering logically what is that meant for?

First, it might be helpful to use string split here since it saves room.

Two tricky things are just going to be knowing how many days are in each month. secondly, we need to take into account leap years (the formula for which is annoying to remember).

It is a good idea to split up the code when writing this, and you will see that it is not so bad.

```
public int dayFromDate(String date) {
String[] info = date.split("-");//split the string up so we can retrieve data more easily
int d, m, y;
d = Integer.parseInt(info[2]);//parse this info
m = Integer.parseInt(info[1]);
y = Integer.parseInt(info[0]);
int output = 0;
for (int i = 1; i < m; i++ ) {//go through all of the months starting from january and add the number of days for each month into a sum
output += numDays(i, y);
}
return output + d;
}
public int numDays(int month, int year) {// given a month and a year, return how many days are in that month
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 1 && isLeapYear(year)) {// if we encounter a leapyear, and the month we check is february, return 29 instead of 28
return 29;
}
return days[month - 1];
}
public boolean isLeapYear(int year) {// check if a year is a leapyear
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
}
```

Consider the following code:

```
public class DateUtils {
static final int IDX_YEAR = 0;
static final int IDX_MONTH = 1;
static final int IDX_DAY = 2;
static int[] daysCount = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private static boolean isLeapYear(int year){
return year % 4 == 0 && (year % 100 != 0 && year % 400 != 0);
}
public static int getDayOfYear(String date){
String[] parts = date.split("-");
int year = Integer.parseInt(parts[IDX_YEAR]);
int month = Integer.parseInt(parts[IDX_MONTH]) - 1;
int day = Integer.parseInt(parts[IDX_DAY]);
boolean leapYear = isLeapYear(year);
int dayOfYear = 0;
for (int m = 0; m < month; m++){
dayOfYear += daysCount[m];
//february
if (m == 1 && leapYear){
dayOfYear++;
}
}
dayOfYear += day;
return dayOfYear;
}
}
```

```
####Program in Python By Ankit
def GiveDaynumber(Date):
list=['0','31','28','31','30','31','30','31','31','30','31','30','31']
numday=0
date=Date.split("-")
year=date[0]
day=date[2]
month=date[1]
if int(year)%4 == 0:
list[2]='29' #Adding 29 days for February in a Leap year
for i in range(0,int(month)):
numday+=int(list[i])
return (numday+int(day))
print GiveDaynumber("2011-12-31")
print GiveDaynumber("2016-12-31")
```

```
####Program in Python
def GiveDaynumber(Date):
list=['0','31','28','31','30','31','30','31','31','30','31','30','31']
numday=0
date=Date.split("-")
year=date[0]
day=date[2]
month=date[1]
if int(year)%4 == 0:
list[2]='29' #Adding 29 days for February in a Leap year
for i in range(0,int(month)):
numday+=int(list[i])
return (numday+int(day))
print GiveDaynumber("2011-12-31")
print GiveDaynumber("2016-12-31")
```

- mlblount45 March 19, 2015