Facebook Interview Question
SDE1sCountry: United States
public static Calendar getDateIn(int days, Calendar now) {
Map<Integer, Integer> daysMap = monthToDaysMap();
int dayInYear = days + now.day;
for(int m=1; m<now.month; m++) {
dayInYear += daysMap.get(m);
}
int day = 0;
int month = 1;
int year = now.year;
while(dayInYear > 365) {
year ++;
dayInYear -= 365;
}
while(dayInYear > daysMap.get(month)) {
dayInYear -= daysMap.get(month);
month ++;
}
day += dayInYear;
return new Calendar(year, month, day);
}
private static Map<Integer, Integer> monthToDaysMap() {
Map<Integer, Integer> daysMap = new HashMap<Integer, Integer>();
daysMap.put(1, 31);
daysMap.put(2, 28);
daysMap.put(3, 31);
daysMap.put(4, 30);
daysMap.put(5, 31);
daysMap.put(6, 30);
daysMap.put(7, 31);
daysMap.put(8, 31);
daysMap.put(9, 30);
daysMap.put(10, 31);
daysMap.put(11, 31);
daysMap.put(12, 31);
return daysMap;
}
you can optimize the add_days function
class Calendar:
NUM_OF_DAYS_IN_MONTH = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}
def __init__(self):
self.year = None
self.month = None
self.day = None
def set_date(self, year, month, day):
self.year = year
self.month = month
self.day = day
def add_days(self, N):
while N:
N -= 1
if Calendar.NUM_OF_DAYS_IN_MONTH[self.month] > self.day:
self.day += 1
elif Calendar.NUM_OF_DAYS_IN_MONTH[self.month] == self.day:
self.day = 1
if self.month == 12:
self.year += 1
self.month = 1
else:
self.month += 1
def get_date(self):
return [self.year, self.month, self.day]
package com.algorithm;
import java.util.HashMap;
import java.util.Map;
public class addDaysInCalender {
static final Map<Integer,Integer> getDaysInAMonth=new HashMap<Integer,Integer>();
static{
getDaysInAMonth.put(1, 31);
getDaysInAMonth.put(2, 28);
getDaysInAMonth.put(3, 31);
getDaysInAMonth.put(4, 30);
getDaysInAMonth.put(5, 31);
getDaysInAMonth.put(6, 30);
getDaysInAMonth.put(7, 31);
getDaysInAMonth.put(8, 31);
getDaysInAMonth.put(9, 30);
getDaysInAMonth.put(10, 31);
getDaysInAMonth.put(11, 30);
getDaysInAMonth.put(12, 31);
}
public static boolean isLeapYear(int year){
boolean b=false;
if((year%4==0 && year%100 != 0)||(year % 400==0)){
b=true;
}
return b;
}
public class Calender{
int day;
int month;
int year;
public Calender(int year,int month,int day){
this.day=day;
this.month=month;
this.year=year;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
}
static void addDays(Calender c, int days){
int month=c.getMonth();
int daysInMonth=getDaysInAMonth.get(month);
int outMonth=0;
int outYear=0;
int outDays=0;
if(month == 2 && isLeapYear(c.getYear())){
daysInMonth= daysInMonth + 1;
}
if((c.getDay() + days) >daysInMonth){
outMonth=c.getMonth() + 1;
outDays=(c.getDay() + days) -daysInMonth;
if(outMonth > 12 ){
outYear=c.getYear() +1;
}
else{
outYear=c.getYear();
}
}
System.out.println("Year:" + outYear + " Month:" + outMonth + " Days:" + outDays);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calender c=new addDaysInCalender().new Calender(2017, 2, 21) ;
int days=8;
addDays(c,days);
}
}
package com.algorithm;
import java.util.HashMap;
import java.util.Map;
public class addDaysInCalender {
static final Map<Integer,Integer> getDaysInAMonth=new HashMap<Integer,Integer>();
static{
getDaysInAMonth.put(1, 31);
getDaysInAMonth.put(2, 28);
getDaysInAMonth.put(3, 31);
getDaysInAMonth.put(4, 30);
getDaysInAMonth.put(5, 31);
getDaysInAMonth.put(6, 30);
getDaysInAMonth.put(7, 31);
getDaysInAMonth.put(8, 31);
getDaysInAMonth.put(9, 30);
getDaysInAMonth.put(10, 31);
getDaysInAMonth.put(11, 30);
getDaysInAMonth.put(12, 31);
}
public static boolean isLeapYear(int year){
boolean b=false;
if((year%4==0 && year%100 != 0)||(year % 400==0)){
b=true;
}
return b;
}
public class Calender{
int day;
int month;
int year;
public Calender(int year,int month,int day){
this.day=day;
this.month=month;
this.year=year;
}
public int getDay() {
return day;
}
public int getMonth() {
return month;
}
public int getYear() {
return year;
}
}
static void addDays(Calender c, int days){
int month=c.getMonth();
int daysInMonth=getDaysInAMonth.get(month);
int outMonth=0;
int outYear=0;
int outDays=0;
if(month == 2 && isLeapYear(c.getYear())){
daysInMonth= daysInMonth + 1;
}
if((c.getDay() + days) >daysInMonth){
outMonth=c.getMonth() + 1;
outDays=(c.getDay() + days) -daysInMonth;
if(outMonth > 12 ){
outYear=c.getYear() +1;
}
else{
outYear=c.getYear();
}
}
System.out.println("Year:" + outYear + " Month:" + outMonth + " Days:" + outDays);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calender c=new addDaysInCalender().new Calender(2017, 2, 21) ;
int days=8;
addDays(c,days);
}
}
static Calendar addDays(Calendar in, int days) {
if (days <= 0) {
return in;
}
int rem = 0;
switch (in.month) {
case 2:
if (isLeapYear(in.year)) {
rem = 29 - in.day;
} else {
rem = 28 - in.day;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
rem = 31 - in.day;
break;
default:
rem = 30 - in.day;
break;
}
Calendar out = Calendar.copyy(in);
if (rem <= days) {
out.day = 1;
days = days - rem - 1;
if (out.month == 12) {
out.month = 1;
out.year = out.year + 1;
} else {
out.month = out.month + 1;
}
} else {
out.day = out.day + days;
days = 0;
}
return addDays(out, days);
}
static boolean isLeapYear(int year) {
return year % 4 == 0;
}
public class Calendar {
public int year;
public int month;
public int day;
public Calendar(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return this.day + "/" + this.month + "/" + this.year;
}
public static Calendar copyy(Calendar in) {
return new Calendar(in.year, in.month, in.day);
}
public static void main(String ...args) {
Calendar c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 4));
c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 20));
c1 = new Calendar(2017, 12, 31);
System.out.println(addDays(c1, 1));
c1 = new Calendar(2008, 2, 27);
System.out.println(addDays(c1, 2));
c1 = new Calendar(2017, 1, 1);
System.out.println(addDays(c1, 63));
}
//2017 3 20 - 4
//2017 3 20 - 20
//2017 12 31 - 1
//2008 2 27 - 2
//2017 1 1 - 63
static Calendar addDays(Calendar in, int days) {
if (days <= 0) {
return in;
}
int rem = 0;
switch (in.month) {
case 2:
if (isLeapYear(in.year)) {
rem = 29 - in.day;
} else {
rem = 28 - in.day;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
rem = 31 - in.day;
break;
default:
rem = 30 - in.day;
break;
}
Calendar out = Calendar.copyy(in);
if (rem <= days) {
out.day = 1;
days = days - rem - 1;
if (out.month == 12) {
out.month = 1;
out.year = out.year + 1;
} else {
out.month = out.month + 1;
}
} else {
out.day = out.day + days;
days = 0;
}
return addDays(out, days);
}
static boolean isLeapYear(int year) {
return year % 4 == 0;
}
}
public class Calendar {
public int year;
public int month;
public int day;
public Calendar(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return this.day + "/" + this.month + "/" + this.year;
}
public static Calendar copyy(Calendar in) {
return new Calendar(in.year, in.month, in.day);
}
public static void main(String ...args) {
Calendar c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 4));
c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 20));
c1 = new Calendar(2017, 12, 31);
System.out.println(addDays(c1, 1));
c1 = new Calendar(2008, 2, 27);
System.out.println(addDays(c1, 2));
c1 = new Calendar(2017, 1, 1);
System.out.println(addDays(c1, 63));
}
//2017 3 20 - 4
//2017 3 20 - 20
//2017 12 31 - 1
//2008 2 27 - 2
//2017 1 1 - 63
static Calendar addDays(Calendar in, int days) {
if (days <= 0) {
return in;
}
int rem = 0;
switch (in.month) {
case 2:
if (isLeapYear(in.year)) {
rem = 29 - in.day;
} else {
rem = 28 - in.day;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
rem = 31 - in.day;
break;
default:
rem = 30 - in.day;
break;
}
Calendar out = Calendar.copyy(in);
if (rem <= days) {
out.day = 1;
days = days - rem - 1;
if (out.month == 12) {
out.month = 1;
out.year = out.year + 1;
} else {
out.month = out.month + 1;
}
} else {
out.day = out.day + days;
days = 0;
}
return addDays(out, days);
}
static boolean isLeapYear(int year) {
return year % 4 == 0;
}
}
public class Calendar {
public int year;
public int month;
public int day;
public Calendar(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
@Override
public String toString() {
return this.day + "/" + this.month + "/" + this.year;
}
public static Calendar copyy(Calendar in) {
return new Calendar(in.year, in.month, in.day);
}
public static void main(String ...args) {
Calendar c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 4));
c1 = new Calendar(2017, 3, 20);
System.out.println(addDays(c1, 20));
c1 = new Calendar(2017, 12, 31);
System.out.println(addDays(c1, 1));
c1 = new Calendar(2008, 2, 27);
System.out.println(addDays(c1, 2));
c1 = new Calendar(2017, 1, 1);
System.out.println(addDays(c1, 63));
}
//2017 3 20 - 4
//2017 3 20 - 20
//2017 12 31 - 1
//2008 2 27 - 2
//2017 1 1 - 63
static Calendar addDays(Calendar in, int days) {
if (days <= 0) {
return in;
}
int rem = 0;
switch (in.month) {
case 2:
if (isLeapYear(in.year)) {
rem = 29 - in.day;
} else {
rem = 28 - in.day;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
rem = 31 - in.day;
break;
default:
rem = 30 - in.day;
break;
}
Calendar out = Calendar.copyy(in);
if (rem <= days) {
out.day = 1;
days = days - rem - 1;
if (out.month == 12) {
out.month = 1;
out.year = out.year + 1;
} else {
out.month = out.month + 1;
}
} else {
out.day = out.day + days;
days = 0;
}
return addDays(out, days);
}
static boolean isLeapYear(int year) {
return year % 4 == 0;
}
}
}
- Anonymous June 09, 2017