Code challenge 1:30h to finish

well, I failed. How you Jow Forums solve this?

James is a businessman. He is on a tight schedule this week. The week starts on Monday at 00:00 and ends on Sunday at 24:00. His schedule consists of M meetings he needs to take part in. Each of them will take place in a period of time, beginning and ending on the same day (there are no two ongoing meetings at the same time). James is very tired, thus he needs to find the longest possible time slot to sleep. In other words, he wants to find the longest period of time when there are no ongoing meetings. The sleeping break can begin and end on different days and should begin and end in the same week. You are given a string containing M lines. Each line is a substring representing one meeting in the schedule, in the format "Ddd hh:mm-hh: mm". "Ddd" is a three-letter abbreviation for the day of the week when the meeting takes place: "Mon" (Monday), "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" hh :mm-hh: mm" means the beginning time and the ending time of the meeting (from 00:00 to 24:00 inclusive) The given times represent exact moments of time. So, there are exactly five minutes between 13:40 and 13:45.

chegg.com/homework-help/questions-and-answers/james-businessman-tight-schedule-week-week-starts-monday-00-00-ends-sunday-24-00-schedule--q32456897

Attached: IMG-20190718-WA0026.jpg (1280x960, 178K)

Other urls found in this thread:

chat.stackoverflow.com/transcript/71097/2018/7/16/16-18
twitter.com/NSFWRedditGif

Do your own homework. This is literally an intro to prolog question.

This sounds like something to use a linear programming library for.

That's a big burger.

Lol, this is a clasic traveling salesman problem (TSP) Google the shit out of it in the language you need

...

Isn't this the assignment problem? like N jobs to M processors

Both wrong. It's a matter of finding the pair of jobs A,B such that the end time of A minus the start time of B is maximal.
This boils down to parsing the string and transform it into an array of consecutive M values that can be compared.

I did parse the string and made dictionary with all the jobs. when calculating the time between them the time run out and my results were wrong. I'm feeling so dumb. =(

I had an hour and a half to complete two tasks, the first one was very simple, took me about 5 minutes. the rest of the time I used to do this one and I failed. fuck my life

What did you do? Even ansi C has library for parsing time / date into epoch (single number).

Just parse all dates, sort them and then in one cycle find the maximum difference between date[i] and date[i+1]. Thats your answer?

that's what I tried to do, but the results were wrong. I messed up and didn't had the chance to fix it or even find out what I did wrong. I spent some time thinking of the solution, then I started to write the code.

I also wrote code I didn't needed. like the same day could have several meetings so I wrote a bunch of edge cases. 1 hour and a half was to short.

this is a matter of finding the longest possible "open" time slot, you can represent the entire week as a boolean array, with the first 2400 indices representing monday, then next 2400 for tuesday, and so on. So if one of your inputs is TUE 01:00-02:00, start at index 2400 * 1, then add 100, and set the next 100 indices to True. Then it's a matter of doing a linear scan to find the longest subarray with no meeting (ie, finding the longest possible subarray with only False's.

whoops I forgot 60 minutes in an hour not 100 I'm rarded, but the idea is still the same

some samples in:
chat.stackoverflow.com/transcript/71097/2018/7/16/16-18

why did you delete the pic?

Is this a hiring test? I'd turn 360 degrees and dive out a window if some retard gave this as a hiring questions. It would almost certainly mean their staff was full of autists.

yes, hiring interview

b-b-but i was told they're gonna ask fizzbuzz

I feel like this is really easy, though I might be missing something.

1. Convert all the times to integers, where Monday 00:00 is 0 and Sunday 24:00 is 10,080.
2. Sort the M meetings by start time.
3. For each meeting, get the ending time and the start time of the next meeting. Find this difference, and repeat for each meeting (except the last).
4. Find the time from time 0 to the first meeting, and the last meeting to time 10,080. Take the max of all these values. That's your answer.

The key is "there are no two ongoing meetings at the same time". If you had overlapping meetings this would be a lot harder, and you should think about this problem.

This is retarded, what are you going to do if the problem was to get a year of meetings? Make an array with 365 * 24 * 60 entries?

Attached: 1527668401647.jpg (1242x771, 244K)

solved:

how long did it took you?

Hugely inefficient, just put all meetings into a vector sorted by start-time and then return the time in-between a pair of meetings if the meetings are on different days.

You can solve it in O(1) space by iterating over the lines and the lines shifted by 1 and saving the maximal difference.
Normalize to unix time to have an absolute frame of reference.

>Separate strings to individual variables
>run simple loop to pull all the schedules and store each of these values in an array with an associated id.
>find the difference between each appointment using a while loop
>determine the longest time between the two appointments

Am I retarded?
The meetings can't overlap so just sort by starting time into array M.
Then look for largest gap M[i+1].start - M[i].end

What do you think linear programming solver is for? You're literally just maximizing a value subject to a bunch of restraints.

>simple sort and scan problem
>1.5 hours to finish
>Jow Forums anons saying "TSP, Linear Programming, Prolog, ..."
Who the fuck are you peopel?

Attached: 1561371060585.png (417x480, 326K)

>starts Monday at 00:00
>ends Sunday at 00:00
These are the same time so no time has elapsed. There is no period of time for sleep between these 2 times. GG no re

Who the fuck says Sunday at 24:00

>Sunday at 24:00.
but that is Monday 00:00 ?
unless it ends at Sunday 00:00 and Sunday is not part of the week?

Sunday at 24:00*

Can I just ask why James is only sleeping once a week? No wonder he's very tired

Maybe you don't have talent for problem solving, but you might be able to get into management. Since you are already outsourcing your work.

Look into Constraint Satisfaction Problems.
You basically model your problem as a bunch of logical equations(e.g A->B (NOT(A) OR B)).

it's an optimization not a constraint satisfaction

kek

>71960424
only if the list of meetings is sorted

I'm seeing a lot of talking, but none solutions. there could be more than one meeting port day, they just can't overlap. and it was not 1:30 hours for just this one, this was only the second part of the challenge. it was 1:30 hours for both of them. since I had to make a decision on how I would approach the challenge I lost some minutes. anyway, it was hard for some reason

I'll post my code that I submitted anyway

Just by looking at it, it will be a really stupid simple loops that goes through each line, saving temporarly the last line and a third variable for saving the difference if it is bigger than the last and the date when this time started. At the end of each day this difference time and the startvalue is printed on screen and all values are reset to zero for the next day. Will take me about 10 to 20 lines including two stupid if-paths. If this isn't enough, I would completely fail the test.

You're assuming that there will be no meetings in the night and even worse, you're assuming that his sleep will begin and end on the same day.

What is here to solve? Parse that shit into objects with two timestamps, slap those fuckers into a list, order them by start time and iterate through it comparing
>next.start - current.end
if it's longer than the previous, save the date and time.

>because the meetings listed in the string will already be in chronological order
Thank you for coming in.

In my opinion this is easier than fizzbuzz.

>I'm seeing a lot of talking, but none solutions
Then you must be blind

>become boss
>cancel all meetings
>24/7 nap time

Here, I solved it for maximum nap time.

Who the fuck thinks up these scenarios?
"Fuck I'm tired. I need a program to find me the longest period I.can sleep for during the week."