Funny quiz

Hello everybody,

Today one friend of mine asked me to help to solve a simply “5 minutes” issue, because he is stuck and do not see the way to solve it.

Issue: you have 3 variables:

All of them are measured within a single day in format 00:00:00 – 23:59:59, no matter what is the day of the week etc. Lets assume these values are parsed from the configuration file and has no date, they are simply time of the day.

You need to automate start/stop application’s schedule and your random application should start after startTime and stop before stopTime. Every n seconds you are getting currentTime. And every time when you get currentTime need to check using simple static method if application’s work allowed or prohibited, so return true or false.

For example: startTime = 8:00; stopTime = 22:00; So logically if currentTime = 7:00 or 23:00; applications should be stopped and started only after 8:00 and till 22:00.

This is an ordinary issue, which should be solved quickly without unit testing and involving QA in general, agree?

But try to solve this issue yourself:
a) without writing code, just logically;
b) implement the method to check if you logic works.

Check how many time you will spend to implement this simple method. Then continue reading this post 🙂

————- You did the method and want to check, where is the trick ——————–

Okay, lets assume you have completed the method. But, check how it works if startTime = 8:00 and stopTime = 1:00; with currentTime = 0:30 and 2:00?

85% of readers should say: man, I have failed to implement a simple method 🙂
Because it will be something like: if (startTime < currentTime < stopTime) return true;

Okay, lets try to implement method with check if startTime > stopTime? Did it? Try values: startTime = 17:00; stopTime = 7:00; currentTime = 20:00? Any luck? 🙂

Of course, possibly you did it from first attempt 🙂 But measure how many time you have spent to solve this issue and is it really 5 minutes issue?

I have solved it by the following logic (Java console app), excluding minutes, just to check the logic:

package time;

* @author yahor
public class Main {

* @param args the command line arguments
public static void main(String[] args) {
// TODO code application logic here
int current = 0;
int start = 8;
int stop = 1;

boolean stopLessStart;
boolean currentMoreEqualStart;
boolean currentLessEqualStop;
boolean allowStart;
// Test loop to check the logic itself without minutes
for (int hours = 48; hours > 0; hours–) {

// To avoid extra boolean arithmetic computing, lets compute these values one time per iteration.
// By this way we will get ~ x2 faster computing for whole expression in the worst case
stopLessStart = stop < start;
currentMoreEqualStart = current >= start;
currentLessEqualStop = current <= stop; allowStart = (stopLessStart) && (currentMoreEqualStart || currentLessEqualStop) ||
(!stopLessStart) && (currentMoreEqualStart && currentLessEqualStop);

if (allowStart) {
System.out.println(current + “: ” + allowStart);
} else {
System.out.println(current + “: prohibited!”);
if (current == 24)
{ System.out.println(“—– new day —–“);
current = 0; } } } }

That’s it. To be honestly it took near 1 hour for me to solve this simple issue 🙂

Just try to imagine what if such simple modules are not covered by good unit tests?

If you find the bug in my logic, or you want to share your successful logic – comments are very welcome!

Happy testing (:

Best regards,