prabhashrathore
BAN USER- -1of 1 vote
AnswersWrite a business_days_from_now() method, which takes as an input a number of business days, and returns a Date object which is that many business days from now. For this, a business day is only a weekday and not a weekend.
- prabhashrathore in United States
for example:
Today is Wednesday the 8th.
business_days_from_now(5)
Current Date: Jan 8, 2013 Wednesday
Output:
Wednesday the 15th| Report Duplicate | Flag | PURGE
Netflix Senior Software Development Engineer Coding
public class WordDistanceFinder {
private String[] text;
public WordDistanceFinder(String[] list) {
this.text = list;
}
/**
* Brute Force method to find the distance between two words.
* Time Complexity: O(n)
* Space Complexity: O(1)
*
*/
public int findDistance(String a, String b) {
int size = text.length;
int distance = -1; //distance between words to be sent as output when they are not present in the given text
/* When both words are same then distance should be zero*/
if(a == b) {
return 0;
}
for(int i = 0; i < size; i++) {
if(text[i] == a || text[i] == b) {
boolean temp = text[i] == a;
b = temp ? b : a; //Search for b if a is found or search for a
distance++; //reset distance from -1 to 0
while(i < (size - 1) && text[i] != b) {
i++;
distance++;
if(text[i] == b) {
return distance;
}
}
}
}
return distance;
}
public static void main(String[] args) {
String[] a = new String[] {"the", "quick", "brown", "fox", "quick"};
WordDistanceFinder distanceFinder = new WordDistanceFinder(a);
System.out.println("Distance between words is: " + distanceFinder.findDistance("fox", "the"));
System.out.println("Distance between words is: " + distanceFinder.findDistance("quick", "fox"));
System.out.println("Distance between words is: " + distanceFinder.findDistance("brown", "brown"));
System.out.println("Distance between words is: " + distanceFinder.findDistance("apple", "mango"));
}
}
Here is my program in Java:
/*
* This program is written to print the spiral of a given rectangular matrix in clockwise manner.
* This is done by maintaining two pointer for rows and two pointers for columns starting from top to bottom for rows and left to right for columns.
* The Big-Oh of this program is O(n ^ 2)
*
* @author Prabhash Rathore
* @date Jan 25, 2014
*
*/
public class SpiralMatrix {
private static final char[][] a = {{'1', '2', '3', '4', '5'}, {'6', '8', '9', 'a', 'b'}, {'c', 'd', 'e', 'f', 'g'}, {'h', 'i', 'j', 'k', 'l'}};
public static void printSpriralMatrix(char[][] matrix, int rowLength, int colLength) {
int topRowPointer = 0; // top pointer to row position
int leftColPointer = 0; // left pointer to column position
int bottomRowPointer = rowLength - 1; // bottom pointer to row position
int rightColPointer = colLength - 1; // right pointer to column position
System.out.println("Here is the Spiral Matrix: ");
while((topRowPointer < rowLength/2) && (leftColPointer < colLength/2)) {
// loop variables
int i = 0;
int j = 0;
for(i = topRowPointer, j = leftColPointer; j < rightColPointer; j++)
System.out.println(matrix[i][j]);
for(i = topRowPointer, j = rightColPointer; i < bottomRowPointer; i++)
System.out.println(matrix[i][j]);
for(i = bottomRowPointer, j = rightColPointer; j > leftColPointer; j--)
System.out.println(matrix[i][j]);
for(i = bottomRowPointer, j = leftColPointer; i > topRowPointer; i--)
System.out.println(matrix[i][j]);
topRowPointer++;
leftColPointer++;
bottomRowPointer--;
rightColPointer--;
}
}
public static void main(String[] args) {
int rowSize = a.length;
System.out.println("Row size is: " + rowSize);
int colSize = a[0].length; //assuming given matrix is a rectangular matrix
System.out.println("Column size is: " + colSize);
// print the input matrix
System.out.println("Here are the elements in 2D Matrix:");
for(int i = 0; i < rowSize; i++) {
for(int j = 0; j < colSize; j++)
System.out.println(a[i][j]);
}
printSpriralMatrix(a, rowSize, colSize);
}
}
Here is my version of code, this is without using any Java API:
import java.util.HashMap;
import java.util.Map;
/*
* Calculating day by adding business day to current day value and doing the % 5 to get the final business day. Doesn't take holidays into account.
* For date, first calculating number of weeks involved in business days by dividing it by 5 and then multiply the result with 2 to get total number of weekend days
* Then finalDate = (currentDate + numberoFWeekendDays + numberOfBusinessDays) % 30 -> Assuming every month has 30 days.
* Note: This implementation doesn't take Holidays into account
*
*/
public class FindDayOfWeek {
private Calendar currentDate = new Calendar(0, 5); // Setting the current day and date through constructor
Calendar futureCalendarDate = new Calendar();
/*
* Backtrack the current day to immediate last Monday and then find number of weekends days from Monday to the last day of business date range
* To calculate final date, add current date, differential from current date to Monday, number of business days and weekends and take mod of 31
* For Business days, just add number of business day and take mod of 5
* @parameter integer
* @return Calendar
*
*/
public Calendar findFutureBusinessDay(int n) {
int curDay = currentDate.getDay();
int curDate = currentDate.getDate();
System.out.println("Current Day: " + currentDate.dayMap.get(curDay) + " the " + curDate);
System.out.println("Number of business days: " + n);
int futureDay = -1;
int futureDate = -1;
// handling boundary conditions
if(n < 0) {
System.out.println("Invalid number of business days provided, please provide a valid business day..");
futureCalendarDate.setDay(futureDay);
futureCalendarDate.setDate(futureDate);
return futureCalendarDate;
} else if(n == 0) {
futureDay = curDay;
futureDate = curDate;
futureCalendarDate.setDay(futureDay);
futureCalendarDate.setDate(futureDate);
return futureCalendarDate;
}
if(curDay > 0 && curDay < 6) { // this will take care of condition when current day is a weekend day
futureDay = curDay + n;
} else {
futureDay = n;
}
futureDay = futureDay % 5;
if(futureDay == 0) {
futureDay = 5; // if futureDay is zero => this implies it's a Friday so setting this to 5 as per my hash table mapping..
}
//difference between the hash map value of Monday and any other day, done to reset the cuurent day to May for calculation of number of weekends
int bufferDaysToMonday = curDay - 1;
int counter = bufferDaysToMonday + n; //adjusting counter to account for moving the current day to Monday
futureDate = curDate - bufferDaysToMonday; //moving the future date back to the immediate Monday in that week
// Calculating the number of weekend days falling in the given business days starting from Monday
int numberOfWeekendDays = (counter/5) * 2;
futureDate = futureDate + counter + numberOfWeekendDays;
//condition to handle when current date is a Saturday
if(curDay == 6) {
futureDate = futureDate - 1; // if current day is Saturday then subtract by 1 to account for only one weekend day for the current weekend
}
// assuming every month has 31 days
futureDate = futureDate % 31;
futureCalendarDate.setDay(futureDay);
futureCalendarDate.setDate(futureDate);
return futureCalendarDate;
}
public static void main(String[] args) {
FindDayOfWeek findDayOfWeek2 = new FindDayOfWeek();
System.out.println("\nFUTURE DATE IS: " + findDayOfWeek2.findFutureBusinessDay(27));
}
// Inner Class which represents a Date Object
private class Calendar {
private int day;
private int date;
private int month;
private int year;
private Map<Integer, String> dayMap = new HashMap<Integer, String>();
public Calendar() {
setDayMap();
}
public Calendar(int day, int date) {
this.day = day;
this.date = date;
setDayMap();
}
// Setting the Day name mappings in the Hashmap
private void setDayMap() {
dayMap.put(1, "Monday");
dayMap.put(2, "Tuesday");
dayMap.put(3, "Wednesday");
dayMap.put(4, "Thursday");
dayMap.put(5, "Friday");
dayMap.put(6, "Saturday");
dayMap.put(0, "Sunday");
dayMap.put(-1, "Garbage");
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @return the date
*/
public int getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(int date) {
this.date = date;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return dayMap.get(getDay()) + " the " + getDate();
}
}
}
There is 1 day error in the output of the above program.
- prabhashrathore January 12, 2014There is 1 day error in the output of the above program.
- prabhashrathore January 12, 2014this needs to be programmed without using existing Java Date and Calendar API..
for finding the business day, it's pretty easy, you can add business day to current day enum value and take a mod of 5, that will give the business day. However calculating date is hard because of weekends, holidays and variable number of days in months considerations.
I wrote a sample program assuming each month has 30 days but I don't think it's pretty consistent with future date.
import java.util.HashMap;
import java.util.Map;
/*
* Calculating day by adding business day to current day value and doing the % 5 to get the final business day. Doesn't take holidays into account.
* For date, first calculating number of weeks involved in business days by dividing it by 5 and then multiply the result with 2 to get total number of weekend days
* Then finalDate = (currentDate + numberoFWeekendDays + numberOfBusinessDays) % 30 -> Assuming every month has 30 days.
* Note: This implementation doesn't take Holidays into account
*
*/
public class FindDayOfWeek {
private Calendar currentDate = new Calendar(3, 8); // Setting the current day and date through constructor
Calendar futureCalendarDate = new Calendar();
public Calendar calculateDay(int n) {
int curDate = currentDate.getDate();
int curDay = currentDate.getDay();
//find number of weekend days included in the given business days
int numberOfWeeks = n / 5;
System.out.println("Number of weeks: " + numberOfWeeks);
int numberOfWeekendDays = numberOfWeeks * 2;
System.out.println("Number of weekend days: " + numberOfWeekendDays);
int tempFutureDate = (curDate + n + numberOfWeekendDays);
System.out.println("Value of temporary future date is: " + tempFutureDate);
//assuming every month has 30 days
int futureDate = tempFutureDate % 30;
System.out.println("Here is the future date: " + futureDate);
futureCalendarDate.setDate(futureDate);
int futureDay = (curDay + n) % 5; // Add number of business day to current day and take the remainder operator of 5
futureCalendarDate.setDay(futureDay);
System.out.println("Future day is: " + futureDay + " : " + futureCalendarDate.dayMap.get(futureDay));
return futureCalendarDate;
}
public static void main(String[] args) {
FindDayOfWeek findDayOfWeek = new FindDayOfWeek();
System.out.println("\nFUTURE DATE IS: " + findDayOfWeek.calculateDay(8));
System.out.println("----------------------");
FindDayOfWeek findDayOfWeek2 = new FindDayOfWeek();
System.out.println("\nFUTURE DATE IS: " + findDayOfWeek2.findFutureBusinessDay(8));
}
// Inner Class which represents a Date Object
private class Calendar {
private int day;
private int date;
private int month;
private int year;
private Map<Integer, String> dayMap = new HashMap<Integer, String>();
public Calendar() {
setDayMap();
}
public Calendar(int day, int date) {
this.day = day;
this.date = date;
setDayMap();
}
// Setting the Day names in the Hashmap
private void setDayMap() {
dayMap.put(1, "Monday");
dayMap.put(2, "Tuesday");
dayMap.put(3, "Wednesday");
dayMap.put(4, "Thursday");
dayMap.put(0, "Friday");
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @return the date
*/
public int getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(int date) {
this.date = date;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return dayMap.get(getDay()) + " the " + getDate();
}
}
}
Repcrystalblibby, Analyst at Achieve Internet
By professional i am teacher. Successfully supervised and assisted students, grade artwork, encouraged creativity and new technique.Familiar with art ...
Trying to think if this can solved in less than O(n) time.
- prabhashrathore May 02, 2014