## Facebook Interview Question for SDE1s

• 0
of 0 votes

Country: United States

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

``````import java.util.HashMap;
import java.util.Map;

public class Solution{

static final Map<Integer, Integer> noOFDaysInMonthMap = new HashMap<Integer, Integer>();
static {
noOFDaysInMonthMap.put(1, 31);
noOFDaysInMonthMap.put(2, 28);
noOFDaysInMonthMap.put(3, 31);
noOFDaysInMonthMap.put(4, 30);
noOFDaysInMonthMap.put(5, 31);
noOFDaysInMonthMap.put(6, 30);
noOFDaysInMonthMap.put(7, 31);
noOFDaysInMonthMap.put(8, 31);
noOFDaysInMonthMap.put(9, 30);
noOFDaysInMonthMap.put(10, 31);
noOFDaysInMonthMap.put(11, 30);
noOFDaysInMonthMap.put(1, 31);
}

Calendar addDays(int year,int month,int day, int noOfDays){

int noRemaining = noOfDays;

while(noRemaining>0){

int noOfDaysinCurrMonth = noOFDaysInMonthMap.get(month);
if(month==2 && isLeapYear(year))
noOfDaysinCurrMonth+=1;

if(day+noRemaining> noOfDaysinCurrMonth){
noRemaining = noRemaining - (noOfDaysinCurrMonth - day);
day=0;
if(month==12){
month = 1;
year+=1;
}else{
month+=1;
}

}else{
day+=noRemaining;
noRemaining=0;
}
}

return new Calendar(year, month, day);
}

boolean isLeapYear(int year){

return (year%4==0 && year%100!=0) || year%400==0;
}

class Calendar{
private int year;
private int month;
private int day;

public Calendar(int year,int month,int day) {
this.year=year;
this.month=month;
this.day=day;
}

public int getYear() {
return year;
}

public int getMonth() {
return month;
}

public int getDay() {
return day;
}

}``````

}

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

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

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

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]

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

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

}``````

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

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

}``````

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

function dayafter(\$data, \$after){

\$sizearray=count(\$data);

if(\$sizearray==3){
return date("Y-m-d", strtotime(\$data[0]."-".\$data[1]."-".\$data[2]." + ".\$after." day"));
}else{
return 1;
}
}

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

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

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

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

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

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

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

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

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