InvestCloud Interview Question for Integration Software Engineers


Country: United States
Interview Type: In-Person




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

public static void main(String[] args){
	String date = "12-31-2015";
	DateCalculator dateCalculator = new DateCalculator();
        System.out.println("Number of Days is: " + dateCalculator.dateToDays(date));
}

public class DateCalculator {
    public int dateToDays(String date) {

        String[] dateArr = date.split("-");
        if(dateArr.length != 3)
            throw new DateException();
        int month = Integer.parseInt(dateArr[0]);
        int day = Integer.parseInt(dateArr[1]);
        int year = Integer.parseInt(dateArr[2]);

        int monthInDays = convertMonthToDays(month, year);
        return day + monthInDays;
    }

    private int convertMonthToDays(int month, int year) {
        int monthsToCount = month - 2;
        int numOfDays = 0;
        boolean isLeapYear = Year.isLeapYear(year);
        while (monthsToCount >= 0){
            numOfDays += Month.getDaysInMonth(monthsToCount);
            monthsToCount--;
        }
        if(isLeapYear && month > 2){
            return numOfDays + 1;
        }
        return numOfDays;
    }

}

public class Year {
    public static boolean isLeapYear(int year) {
        if(year % 4 != 0)
            return false;
        if(year % 100 != 0)
            return false;
        if(year % 400 != 0)
            return false;
        return true;
    }
}

public class Month {
    private static final int[] months = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
    public static int getDaysInMonth(int month) {
        if(month < 0 || month > 11) throw new DateException();
        return months[month];
    }
}

public class DateException extends RuntimeException {
    public DateException(){
        super();
    }
}

- mlblount45 March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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)

- CareerCupDom March 19, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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

- Inucoder March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- zealoftoday March 21, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why month-1?
Because months are counted from 1 to 12, but in our program index is from 0.
So for 3rd month, index will be 2.
Correct me if I am wrong.

- Anonymous March 26, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- SK March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- inucoder March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

####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")

- Ankit March 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

####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")

- Ankit March 20, 2015 | 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