Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
import java.io.*;
import java.util.*;
import java.text.*;
public class week {
public static void main (String[] args) {
String dt = "09/13/2012";
printWeek(dt);
}
static void printWeek(String str) {
SimpleDateFormat ft = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat sft = new SimpleDateFormat("E MM/dd/yyyy");
try {
Date date = ft.parse(str);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int temp = cal.get(cal.DAY_OF_WEEK);
while (temp > 0) {
cal.add(Calendar.DATE, -1);
temp--;
}
while (temp < 7) {
cal.add(Calendar.DATE, 1);
System.out.println(sft.format(cal.getTime()));
temp++;
}
} catch(ParseException e) {
System.out.println("Unparsable");
}
}
}
<pre lang="" line="1" title="CodeMonkey21243" class="run-this"> public static void main(String[] args) {
String weekNm[]={"Sunday","Monday","Tues","wed","thurs","fri","sat"};
int weekDayInNum[]={0,1,2,3,4,5,6};
Calendar cal=Calendar.getInstance();
// Set the date .. can take user input and then parse it to these values
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, 31);
int monthsDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
String nameOfMonths[]={"Jan","feb","Mar","Apr","May","June","july","Aug","Sept","Oct","Nov"
,"dec"};
if((year % 400 == 0 ||(year % 4 == 0 && year % 100 != 0)))
monthsDays[1]=29;
int j=0;
int i=0;
int domNew=dom;
int indexOfMonth=month;
String datesOfWeek[]=new String[7];
for( i=dow-1;i<(weekDayInNum.length);i++)
{
if((domNew)%monthsDays[indexOfMonth]!=0)
datesOfWeek[i]=""+(domNew)%monthsDays[indexOfMonth];
else
datesOfWeek[i]=""+monthsDays[indexOfMonth];
domNew++;
}
for(j=dow-2;j>=0;j--)
{
datesOfWeek[j]=""+dom;
}
for(int k=0;k<datesOfWeek.length;k++)
{
System.out.println(" "+datesOfWeek[k]+" "+weekNm[k]);
}
}
}
</pre><pre title="CodeMonkey21243" input="yes">
</pre>
These steps are also added to get those values.
int month = cal.get(Calendar.MONTH) ;
int year = cal.get(Calendar.YEAR);
int dow = cal.get(Calendar.DAY_OF_WEEK); // if value is 2 which means monday
int dom = cal.get(Calendar.DAY_OF_MONTH);
Sorry friends..
pls add few more conditions in place of for loop with j as counter. It should be complete then even if the date is 1st of any month . it will fetch dates fro previous months. Thanks.
for(j=dow-2;j>=0;j--)
{
--dom;
if(dom<0)
{
datesOfWeek[j]=""+(int)((int)monthsDays[indexOfMonth-1]+(int)dom);
}
else if(dom==0)
{
datesOfWeek[j]=""+monthsDays[indexOfMonth-1];
}
else
{
datesOfWeek[j]=""+dom;
}
}
bool isLeapYear(int year)
{
returns true if it's a leap year else false
}
enum Day {1="monday",2="tuesday",3="wednesday"...};
struct Date{
int date;
int month;
int year;
Day day;
}currdate;
Date tempdate;
for(int i=0;i<currdate.day;i++)
{
SET tempdate variables as the following:
currdate.day+i
(currdate.date+i)%31 if currdate.month=1,3,5,7,8,10,12
(currdate.date+i)%30 if currdate.month=4,6,9,11
(currdate.date+i)%28 if currdate.month=2 && !isLeapYear(currdate.year)
(currdate.date+i)%29 if currdate.month=2 && isLeapYear(currdate.year)
(currdate.month+i) if currdate.month=1,3,5,7,8,10,12 date crosses 31
(currdate.month+i) if currdate.month= 4,6,9,11 && date crosses 30
(currdate.month+i) if currdate.month=2 && !isLeapYear(currdate.year) && date crosses 28
(currdate.month+i) if currdate.month=2 && isLeapYear(currdate.year) && date crosses 29
currdate.year+i if currdate.month==12 and currdate.date crosses 31
}
for(i=0;i<7-currdate.day;i++)
{
SET tempdate variables as the following:
currdate.day+i
(currdate.date+i)%31 if currdate.month=1,3,5,7,8,10,12
(currdate.date+i)%30 if currdate.month=4,6,9,11
(currdate.date+i)%28 if currdate.month=2 && !isLeapYear(currdate.year)
(currdate.date+i)%29 if currdate.month=2 && isLeapYear(currdate.year)
(currdate.month+i) if currdate.month=1,3,5,7,8,10,12 date crosses 31
(currdate.month+i) if currdate.month= 4,6,9,11 && date crosses 30
(currdate.month+i) if currdate.month=2 && !isLeapYear(currdate.year) && date crosses 28
(currdate.month+i) if currdate.month=2 && isLeapYear(currdate.year) && date crosses 29
currdate.year+i if currdate.month==12 and currdate.date crosses 31
}
bool has_leaped = (year % 4 == 0) && month >= 3;
int day_of_week = (day_of_month + month_correction[month] + year + (year-1)%4 + (has_leaped ? 1 : 0) ) % 7;
The day of the week advances one when the day advances one; it advances some pre-calculated amount when the month advances one (so use a pre-calculated array for this value); it advances one when the year increases (because 365%7 == 1), and an additional one on leap years. Leap years are years that are either divisionable by 4 but not by 100, or divisionable by 400. See wikipedia for more details: "Leap Year" or "Weekday Determination"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int NUMBEROFDAYS[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isLeapYear(int year)
{
return (year%400 == 0)||(year%4 == 0 && year%100 != 0);
}
int numberOfDaysInMonthAndYear(int month, int year)
{
if(isLeapYear(year) && month == 2)
{
return 29;
}
else return NUMBEROFDAYS[month];
}
void printDay(int day, int month, int year, int whichDay)
{
cout<<day<<", "<<month<<", "<<year<<" is "<<whichDay<<endl;
}
void findWeek(int day, int month, int year, int whichDay)
{
for( int i = whichDay ; i > 0 ; -- i )
{
if(day == 1)
{
if(month == 1)
{
year--;
month = 12;
day = numberOfDaysInMonthAndYear(month, year);
}
else month--;
}
else day--;
}
for(int i = 0; i < 7 ; ++ i)
{
printDay(day, month, year, i);
day++;
if(day > numberOfDaysInMonthAndYear(month, year) )
{
day = 1;
month++;
if(month > 12)
{
month = 1;
year++;
}
}
}
}
int main (int argc, const char * argv[])
{
findWeek(1,1,2001,3);
return 0;
}
and }#include <iostream>
#include <vector>
#include <string>
using namespace std;
void week_day(int day, int month, int year);
bool is_leap(int year);
void up_date(int *day, int *month, int *year, int *week_day);
void down_date(int *day, int *month, int *year);
int num_days(int month, int year);
int daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string dayOfWeek[7] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
int f_day = 1, f_month = 1, f_year = 1970, f_week_day = 4;
int main(){
week_day(1, 6, 2012);
return 0;
}
void week_day(int day, int month, int year){
int n_day = f_day, n_month = f_month, n_year = f_year, n_week_day = f_week_day;
while(day != n_day || month != n_month || year != n_year){
//cout<<n_day<<"/"<<n_month<<"/"<<n_year<<"/"<<n_week_day<<endl;
up_date(&n_day, &n_month, &n_year, &n_week_day);
}
cout<<dayOfWeek[n_week_day]<<endl;
}
bool is_leap(int year){
if(year%400 == 0 || year%4 == 0)
return true;
if(year%100 == 0)
return false;
return false;
}
void up_date(int *day, int *month, int *year, int *week_day){
if((*day+1) > num_days(*month, *year)){
if((*month+1) < 12){
(*month)++;
*day = 1;
}
else{//if it is the last day of the year
*month = 1;
*day = 1;
(*year)++;
}
}
else{//if it is not the end of the month just increase the day
(*day)++;
}
if(*week_day < 6)
(*week_day)++;
else
*week_day = 0;
}
int num_days(int month, int year){
if((month-1) < 0 || (month-1) > 11){
cout<<"Invalid month"<<endl;
return -1;
}
if((month-1) == 1 && is_leap(year))
return daysInMonth[month-1]+1;
return daysInMonth[month-1];
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class FindWeekDates {
public static void main(String args[]) throws IOException
{
GregorianCalendar g= new GregorianCalendar();
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the date");
String d=br.readLine();
System.out.println("Enter the month with value 0-Jan and so on");
String mon=br.readLine();
System.out.println("Enter the year");
String yr=br.readLine();
int dt=Integer.parseInt(d);
g.set(Calendar.DATE, dt);
g.set(Calendar.MONTH, Integer.parseInt(mon));
g.set(Calendar.YEAR, Integer.parseInt(yr));
int day_of_week=g.get(Calendar.DAY_OF_WEEK);
System.out.println("Day of the week s "+day_of_week+" for the given date "+g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
System.out.println(day_of_week);
int temp=day_of_week;
while(temp>1)
{
g.add(Calendar.DATE, -1);
System.out.println(g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
temp--;
}
//lets reassign the values of the day of the week and also the entered date;
temp=day_of_week;
g.set(Calendar.DATE, dt);
g.set(Calendar.MONTH, Integer.parseInt(mon));
g.set(Calendar.YEAR, Integer.parseInt(yr));
System.out.println("temp now s "+temp);
while(temp<7)
{
g.add(Calendar.DATE,1);
System.out.println(g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
temp++;
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class FindWeekDates {
public static void main(String args[]) throws IOException
{
GregorianCalendar g= new GregorianCalendar();
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the date");
String d=br.readLine();
System.out.println("Enter the month with value 0-Jan and so on");
String mon=br.readLine();
System.out.println("Enter the year");
String yr=br.readLine();
int dt=Integer.parseInt(d);
g.set(Calendar.DATE, dt);
g.set(Calendar.MONTH, Integer.parseInt(mon));
g.set(Calendar.YEAR, Integer.parseInt(yr));
int day_of_week=g.get(Calendar.DAY_OF_WEEK);
System.out.println("Day of the week s "+day_of_week+" for the given date "+g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
System.out.println(day_of_week);
int temp=day_of_week;
while(temp>1)
{
g.add(Calendar.DATE, -1);
System.out.println(g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
temp--;
}
//lets reassign the values of the day of the week and also the entered date;
temp=day_of_week;
g.set(Calendar.DATE, dt);
g.set(Calendar.MONTH, Integer.parseInt(mon));
g.set(Calendar.YEAR, Integer.parseInt(yr));
System.out.println("temp now s "+temp);
while(temp<7)
{
g.add(Calendar.DATE,1);
System.out.println(g.get(Calendar.DATE)+" "+g.get(Calendar.MONTH));
temp++;
}
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class WeekOfDates {
public static void printDays(String date) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat df = new SimpleDateFormat("E MM/dd/yyyy");
try {
Date d = sdf.parse(date);
Calendar cal = new GregorianCalendar();
cal.setTime(d);
int tmp = cal.get(Calendar.DAY_OF_WEEK);
int tmp1 = tmp;
while (tmp > 0) {
System.out.println(df.format(cal.getTime()));
cal.add(Calendar.DATE, -1);
tmp--;
}
while (tmp <= tmp1) {
cal.add(Calendar.DATE, 1);
tmp++;
}
while (tmp <= 7) {
System.out.println(df.format(cal.getTime()));
cal.add(Calendar.DATE, 1);
tmp++;
}
} catch (ParseException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
printDays("02/19/2013");
}
}
import java.io.*;
import java.util.*;
import java.text.*;
public class calend {
public static void main (String[] args) throws ParseException {
String dt = "03/13/2013";
printWeek(dt);
}
static void printWeek(String str) throws ParseException {
SimpleDateFormat ft = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat sft = new SimpleDateFormat("E MM/dd/yyyy");
Date date = ft.parse(str); System.out.println(date);
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int temp = cal.get(cal.DAY_OF_WEEK); System.out.println(temp);
while (temp > 0) {
cal.add(Calendar.DATE, -1);
temp--;
}
while (temp < 7) {
cal.add(Calendar.DATE, 1);
System.out.println(sft.format(cal.getTime()));
temp++;
}
}
}
I think the examples given in the question for the year 2001 seem to be wrong by day ahead. I checked with online calendars and it does not match with the calendars provided online (i even cross checked it with google calendar). Here is a working C++ code which works without the fault in the question.
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int dd, mm, yy, i, ans, mon[12]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char day[7][10]={ "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"};
cin>>mm>>dd>>yy;
for (i=1583; i<yy; i++)
{
if ((i%4==0 && i%100!=0) || i%400==0)
{
ans+=366;
}
else
ans+=365;
}
for (i=0; i<(mm-1); i++)
{
ans+=mon[i];
}
ans+=dd;
if (((yy%4==0 && yy%100!=0) || yy%400==0) && mm>2)
{
ans+=1;
}
ans=ans%7;
cout<<day[ans];
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef struct dateStruct {
int yy;
int mm;
int dd;
} date;
int Months[] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
string weekDays[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
class solution {
private:
bool isDateLarger (date A, date B) {
if (A.yy > B.yy)
return true;
else if (A.yy < B.yy)
return false;
if (A.mm > B.mm)
return true;
else if (A.mm < B.mm)
return false;
if (A.dd > B.dd)
return true;
else if (A.dd < B.dd)
return false;
return true;
}
bool isLeap(int yy) {
if (yy % 4 == 0 && yy % 100 != 0)
return true;
return false;
}
//Default A < B
int monthDays(int mmA, int mmB, int ddA, int ddB, int yy) {
int mm, days = 0;
for (mm = mmA + 1; mm < mmB; mm++) {
days += Months[mm - 1];
if (isLeap(yy) && mm == 2)
days++;
}
if (mmA == mmB)
days = ddB - ddA;
else {
days = days + Months[mmA - 1] - ddA;
if (isLeap(yy) && mmA <= 2)
days++;
days = days + ddB;
}
//cout << Months[mmA - 1] - ddA << endl;
//cout << days << endl;
return days;
}
//Default A < B
int diffDays (date A, date B) {
int yy;
int days = 0;
for (yy = A.yy + 1; yy < B.yy; yy++) {
if (isLeap(yy))
days += 366;
else
days += 365;
}
//cout << days << endl;
if (A.yy == B.yy)
days = monthDays(A.mm, B.mm, A.dd, B.dd, A.yy);
else {
days += monthDays(A.mm, 12, A.dd, 31, A.yy);
days += monthDays(1, B.mm, 1, B.dd, B.yy) + 1;
}
return days;
}
public:
int findWeek (date findDate, date baseDate, int baseWeek) {
bool isLarger = isDateLarger(findDate, baseDate);
int days, weekDay;
if (isLarger) {
days = diffDays(baseDate, findDate);
weekDay = (baseWeek + days) % 7;
} else {
days = diffDays(findDate, baseDate);
weekDay = (baseWeek - (days % 7) + 7) % 7;
}
//cout << days << endl;
return weekDay;
}
};
int main() {
solution sol;
date baseDate;
baseDate.yy = 2015;
baseDate.mm = 3;
baseDate.dd = 16;
date findDate;
findDate.yy = 2007;
findDate.mm = 4;
findDate.dd = 1;
int weekday = sol.findWeek(findDate, baseDate, 1);
cout << weekDays[weekday] << endl;
return 0;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef struct dateStruct {
int yy;
int mm;
int dd;
} date;
int Months[] = {31, 28, 31, 30,
31, 30, 31, 31,
30, 31, 30, 31};
string weekDays[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
class solution {
private:
bool isDateLarger (date A, date B) {
if (A.yy > B.yy)
return true;
else if (A.yy < B.yy)
return false;
if (A.mm > B.mm)
return true;
else if (A.mm < B.mm)
return false;
if (A.dd > B.dd)
return true;
else if (A.dd < B.dd)
return false;
return true;
}
bool isLeap(int yy) {
if (yy % 4 == 0 && yy % 100 != 0)
return true;
return false;
}
//Default A < B
int monthDays(int mmA, int mmB, int ddA, int ddB, int yy) {
int mm, days = 0;
for (mm = mmA + 1; mm < mmB; mm++) {
days += Months[mm - 1];
if (isLeap(yy) && mm == 2)
days++;
}
if (mmA == mmB)
days = ddB - ddA;
else {
days = days + Months[mmA - 1] - ddA;
if (isLeap(yy) && mmA <= 2)
days++;
days = days + ddB;
}
//cout << Months[mmA - 1] - ddA << endl;
//cout << days << endl;
return days;
}
//Default A < B
int diffDays (date A, date B) {
int yy;
int days = 0;
for (yy = A.yy + 1; yy < B.yy; yy++) {
if (isLeap(yy))
days += 366;
else
days += 365;
}
//cout << days << endl;
if (A.yy == B.yy)
days = monthDays(A.mm, B.mm, A.dd, B.dd, A.yy);
else {
days += monthDays(A.mm, 12, A.dd, 31, A.yy);
days += monthDays(1, B.mm, 1, B.dd, B.yy) + 1;
}
return days;
}
public:
int findWeek (date findDate, date baseDate, int baseWeek) {
bool isLarger = isDateLarger(findDate, baseDate);
int days, weekDay;
if (isLarger) {
days = diffDays(baseDate, findDate);
weekDay = (baseWeek + days) % 7;
} else {
days = diffDays(findDate, baseDate);
weekDay = (baseWeek - (days % 7) + 7) % 7;
}
//cout << days << endl;
return weekDay;
}
};
int main() {
solution sol;
date baseDate;
baseDate.yy = 2015;
baseDate.mm = 3;
baseDate.dd = 16;
date findDate;
findDate.yy = 2007;
findDate.mm = 4;
findDate.dd = 1;
int weekday = sol.findWeek(findDate, baseDate, 1);
cout << weekDays[weekday] << endl;
return 0;
}
- Anonymous February 23, 2012