Code for a function that just returns after an hour -interview question - algorithm

I saw this question online from an interview:
Suppose you have this code:
void myFunction(){
int time = clcTime();
while (clcTime()-time!=3600);
}
When clcTime() is a method that returns the seconds that passed since 00:00 of today.
(1). Find what this code snippet does.
(2). Some QA tester said this code fails at specific case. What's that case and how can you solve that issue?
(3). Another QA tester that during the day this code worked fine, but when he got to sleep - something went wrong. What can possibly be the problem and how can you solve it?
My attempt:
For (1), I think this function just suppose to run in a loop for an hour.
For (3), I think the problem is when the time variable get its value when the current hour of the day is in the range [23:00:00,23:59:59]. And that's because on that case, the value of time will be in the range [23*3600,23*3600 + 3599] and clcTime() can't return a matching value in the range [24*3600, 24*3600 + 3599]. So in that case, we know that the condition 'clcTime()-time' will never get a value of 3600 and we will get an infinite loop.
My suggestion for solving it is replacing the while line with those lines:
int measure = clcTime() - time;
int measureModulo = measure % 3600;
while (measure==0 || measureModulo!=0){
measure = clcTime() - time;
measureModulo = measure % 3600;
}
The only problem I still have is that I can't figure out (2) - I don't find any other problem with this code.
Do you have any idea what else can be problematic with this code?
Also, please feel free to correct me if I was wrong with what I wrote for (1) and (3).

Another problem with this code, and your fix, is that it checks clcTime() for an exactly matching value. If the system is busy and the loop doesn't get to run for more than a second, then it will miss the matching second and continue waiting for at least another hour.
Also there will be problems when the user changes the system clock or system time zone, when daylight savings time comes into or out of effect, when the clock is automatically adjusted for leap seconds, etc.

Related

How do I create a strategy trigger for a daily trade at a certain time?

I want to test a simple strategy based on time: every day at fixed time check some conditions and go long. every day at fixed time exit.
Whatever I try to get timeOpenCondition I get syntax error.
Tried things similar to:
EntryTime = if hour=0800 and minute=0
ExitTime = if hour=1400 and minute=0
It feels like I don't get the concept of how this works. Appreciate any help!

Laravel Task Scheduling - when does the execution starts

Laravel Task Scheduling has options like, everyMinute() which will run a command every minute. I want to know what is the time it actually starts executing. Is it when I run the server or any specific second of the minute?
I am trying to run a function depending on the time difference. Here's a pseudocode
Run myCustomFunction() if diffInMin(now, customTime) <= 1
I thought it will run 1 time but it ran twice every time.
The scheduler usually runs every minute right around the zero secound mark based on the server's current time as #apokryfos mentioned.
Assuming the customTime is a fixed DateTime, what makes you think the code you wrote will only run once?
When now() === customTime the diffInMin() would be zero so the
condition diffInMin(now, customTime) <= 1 will evaluate to true.
The next minute, the diffInMin() would be 1, so the
condition diffInMin(now, customTime) <= 1 will still evaluate to true.

How to see what value is being calculated pine Editor

I have the following script running with the intention of closing a trade after it has been open for a period of 4 days since the trade was taken.
TimeDiff = time - time[1]
MinutesPerBar = TimeDiff / 60000
//calcuates how long one bar is in minutes
BarsSinceSwingLongCondition = barssince(SwingLongCondition)
// Calculates how many bars have passed since open of trade
CurrentSwingTradeDuration = BarsSinceSwingLongCondition * MinutesPerBar
//calculates the duration that the trade has been opened for (minutes*number of bars)
MaximumSwingTradeDuration = 4*1440
// Sets maximum trade duration. Set at 4 Days in minutes
SwingLongCloseLogic3 = CurrentSwingTradeDuration > MaximumSwingTradeDuration
// Closes trade when trade duration exceeds maximum duration set (4days)
The close logic however isn't executing when I run the strategy as i have trades open for longer than the maximum duration.
Is there any way to see what value each element of the formula is calculating so that I can see where the error is (i suspect it could be the time element). Or can anyone see where I am going wrong in the code?
The fastest way to achieve that is using the plotchar function, which would show the values in the data-window on mouse-over on each bar. The user manual contains several other techniques available for debugging.

Find the difference between 2 dates and check if smaller than a given value

my issue is that I want to be able to get two time stamps and compare if the second (later taken) one is less than 59 minutes away from the first one.
Following this thread Compare two dates with JavaScript
the date object may do the job.
but first thing i am not happy with is that it takes the time from my system.
is it possible to get the time from some public server or something?
cause there always is a chance that the system clock gets manipulated within the time stamps, so that would be too unreliable.
some outside source would be great.
then i am not too sure how to get the difference between 2 times (using 2 date objects).
many issue that may pop up:
time being something like 3:59 and 6:12
so just comparing minutes would give the wrong idea.
so we consider hours too.
biut there the issue with the modulo 24.
day 3 23:59 and day 4 0:33 wouldnt be viewed proper either.
so including days too.
then the modulo 30 thing, even though that on top changes month for month.
so month and year to be included as well.
so we would need the whole date, everything from current year to second (because second would be nice too, for precision)
and comparing them would require tons of if clauses for year, month, etc.
do the date objects have some predfeined date comparision function that actually keeps all these things in mind (havent even mentioned leap years yet, have I)?
time would be very important cause exactly at the 59 minutes mark (+-max 5 seconds wouldnt matter but getting rmeitely close to 60 is forbidden)
a certain function would have to be used that without fail closes a website.
script opens website at mark 0 min, does some stuff rinse and repeat style and closes page at 59 min mark.
checking the time like every few seconds would be smart.
Any good ideas how to implement such a time comparision that doesnt take too more computer power yet is efficient as in new month starting and stuff doesnt mess it up?
You can compare the two Date times, but when creating a date time there is a parameter of DateTime(value) which you can use.
You can use this API to get the current UTC time which returns a example JSON array like this:
{
"$id":"1",
"currentDateTime":"2019-11-09T21:12Z",
"utcOffset":"00:00:00",
"isDayLightSavingsTime":false,
"dayOfTheWeek":"Saturday",
"timeZoneName":"UTC",
"currentFileTime":132178075626292927,
"ordinalDate":"2019-313",
"serviceResponse":null
}
So you can use either the currentFileTime or the currentDateTime return from that API to construct your date object.
Example:
const date1 = new Date('2019-11-09T21:12Z') // time when I started writing this answer
const date2 = new Date('2019-11-09T21:16Z') // time when I finished writing this answer
const diff = new Date(date2-date1)
console.log(diff.toTimeString()) // time it took me to write this
Please keep in mind that due to network speeds, the time API will be a little bit off (by a few milliseconds)

Logic for tracking hours worked

I was wanting to write a program in C that I can simply type in the hours that I worked for each day of the week, including time on break, that will take my input and return the total number of hours I have worked for that week. It's dumb, I know, but I am not sure how to do the math for this regarding time on the clock.
Thank you
At beginning of work: get the current date, make it into seconds.
At end of work: get the current date, make it into seconds.
So working seconds = end seconds - beginning seconds
Then you'll just have to make those into hours.

Resources