Checking if two time intervals overlap
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I have two time intervals i1
and i2
, each defined by two values, begin
and end
(epoch time). I need to write a function returning a boolean value indicating whether the intervals overlap.
According to my understanding, an overlap can occur in four following cases:
while the last case (where the i2
is fully included within i1
is already covered by first two cases so can be dismissed.
The remaining three cases lead me to the following logic:
private static boolean isThereOverlap(Interval t1, Interval t2)
t1.end.isAfter(t2.begin) && t1.end.isBefore(t2.end)
where:
t1
and t2
represent i1
and i2
accordingly.
I wonder whether there's a more concise way to identify the overlap or a
way to simplify the code?
EDIT1:
Including the Interval
class which is utilizing java.time
:
import java.time.Instant;
private class Interval
private Instant begin;
private Instant end;
private Interval(long begin, long end)
this.begin = Instant.ofEpochMilli(begin);
this.end = Instant.ofEpochMilli(end);
java datetime interval
add a comment |Â
up vote
4
down vote
favorite
I have two time intervals i1
and i2
, each defined by two values, begin
and end
(epoch time). I need to write a function returning a boolean value indicating whether the intervals overlap.
According to my understanding, an overlap can occur in four following cases:
while the last case (where the i2
is fully included within i1
is already covered by first two cases so can be dismissed.
The remaining three cases lead me to the following logic:
private static boolean isThereOverlap(Interval t1, Interval t2)
t1.end.isAfter(t2.begin) && t1.end.isBefore(t2.end)
where:
t1
and t2
represent i1
and i2
accordingly.
I wonder whether there's a more concise way to identify the overlap or a
way to simplify the code?
EDIT1:
Including the Interval
class which is utilizing java.time
:
import java.time.Instant;
private class Interval
private Instant begin;
private Instant end;
private Interval(long begin, long end)
this.begin = Instant.ofEpochMilli(begin);
this.end = Instant.ofEpochMilli(end);
java datetime interval
1
Is thatInterval
constructor reallyprivate
? How then are you creating new instances of it? Please show your real actual code.
– Simon Forsberg♦
1 hour ago
@SimonForsberg Yes, it is. TheInterval
class and theisThereOverlap
method, both reside in the same class.
– Eugene S
1 hour ago
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have two time intervals i1
and i2
, each defined by two values, begin
and end
(epoch time). I need to write a function returning a boolean value indicating whether the intervals overlap.
According to my understanding, an overlap can occur in four following cases:
while the last case (where the i2
is fully included within i1
is already covered by first two cases so can be dismissed.
The remaining three cases lead me to the following logic:
private static boolean isThereOverlap(Interval t1, Interval t2)
t1.end.isAfter(t2.begin) && t1.end.isBefore(t2.end)
where:
t1
and t2
represent i1
and i2
accordingly.
I wonder whether there's a more concise way to identify the overlap or a
way to simplify the code?
EDIT1:
Including the Interval
class which is utilizing java.time
:
import java.time.Instant;
private class Interval
private Instant begin;
private Instant end;
private Interval(long begin, long end)
this.begin = Instant.ofEpochMilli(begin);
this.end = Instant.ofEpochMilli(end);
java datetime interval
I have two time intervals i1
and i2
, each defined by two values, begin
and end
(epoch time). I need to write a function returning a boolean value indicating whether the intervals overlap.
According to my understanding, an overlap can occur in four following cases:
while the last case (where the i2
is fully included within i1
is already covered by first two cases so can be dismissed.
The remaining three cases lead me to the following logic:
private static boolean isThereOverlap(Interval t1, Interval t2)
t1.end.isAfter(t2.begin) && t1.end.isBefore(t2.end)
where:
t1
and t2
represent i1
and i2
accordingly.
I wonder whether there's a more concise way to identify the overlap or a
way to simplify the code?
EDIT1:
Including the Interval
class which is utilizing java.time
:
import java.time.Instant;
private class Interval
private Instant begin;
private Instant end;
private Interval(long begin, long end)
this.begin = Instant.ofEpochMilli(begin);
this.end = Instant.ofEpochMilli(end);
java datetime interval
java datetime interval
edited 2 hours ago
asked 5 hours ago
Eugene S
1535
1535
1
Is thatInterval
constructor reallyprivate
? How then are you creating new instances of it? Please show your real actual code.
– Simon Forsberg♦
1 hour ago
@SimonForsberg Yes, it is. TheInterval
class and theisThereOverlap
method, both reside in the same class.
– Eugene S
1 hour ago
add a comment |Â
1
Is thatInterval
constructor reallyprivate
? How then are you creating new instances of it? Please show your real actual code.
– Simon Forsberg♦
1 hour ago
@SimonForsberg Yes, it is. TheInterval
class and theisThereOverlap
method, both reside in the same class.
– Eugene S
1 hour ago
1
1
Is that
Interval
constructor really private
? How then are you creating new instances of it? Please show your real actual code.– Simon Forsberg♦
1 hour ago
Is that
Interval
constructor really private
? How then are you creating new instances of it? Please show your real actual code.– Simon Forsberg♦
1 hour ago
@SimonForsberg Yes, it is. The
Interval
class and the isThereOverlap
method, both reside in the same class.– Eugene S
1 hour ago
@SimonForsberg Yes, it is. The
Interval
class and the isThereOverlap
method, both reside in the same class.– Eugene S
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are always half-open (i.e. including the start time, but excluding the end time); it would be wise to add suitable comments to ensure that all readers agree on what's expected.
It's possible to simplify considerably, because in all cases we want (and none we don't), t1
ends after t2
begins and t2
ends after t1
begins, giving us a simpler expression:
t1.begin.isBefore(t2.end) && t2.begin.isBefore(t1.end)
If the Interval
class is under your control, consider making this method a public instance method of that class. You might also want to check that the parameters are not null
, if users might use that to indicate an empty interval:
if (t1 == null || t2 == null)
return false;
(this latter is an argument against using an instance method, as then the caller would need to check t1
isn't null).
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
add a comment |Â
up vote
0
down vote
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spns are completely distinct. The spans are distinct if the first span starts after the other ends, or ends before the other one starts. This test is easy. The spans overlap if they are not distinct, so you can negate the logic for the isThereOverlap
method.
private static boolean isThereOverlap(Interval t1, Interval t2) t1.begin.isAfter(t2.end));
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are always half-open (i.e. including the start time, but excluding the end time); it would be wise to add suitable comments to ensure that all readers agree on what's expected.
It's possible to simplify considerably, because in all cases we want (and none we don't), t1
ends after t2
begins and t2
ends after t1
begins, giving us a simpler expression:
t1.begin.isBefore(t2.end) && t2.begin.isBefore(t1.end)
If the Interval
class is under your control, consider making this method a public instance method of that class. You might also want to check that the parameters are not null
, if users might use that to indicate an empty interval:
if (t1 == null || t2 == null)
return false;
(this latter is an argument against using an instance method, as then the caller would need to check t1
isn't null).
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
add a comment |Â
up vote
4
down vote
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are always half-open (i.e. including the start time, but excluding the end time); it would be wise to add suitable comments to ensure that all readers agree on what's expected.
It's possible to simplify considerably, because in all cases we want (and none we don't), t1
ends after t2
begins and t2
ends after t1
begins, giving us a simpler expression:
t1.begin.isBefore(t2.end) && t2.begin.isBefore(t1.end)
If the Interval
class is under your control, consider making this method a public instance method of that class. You might also want to check that the parameters are not null
, if users might use that to indicate an empty interval:
if (t1 == null || t2 == null)
return false;
(this latter is an argument against using an instance method, as then the caller would need to check t1
isn't null).
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are always half-open (i.e. including the start time, but excluding the end time); it would be wise to add suitable comments to ensure that all readers agree on what's expected.
It's possible to simplify considerably, because in all cases we want (and none we don't), t1
ends after t2
begins and t2
ends after t1
begins, giving us a simpler expression:
t1.begin.isBefore(t2.end) && t2.begin.isBefore(t1.end)
If the Interval
class is under your control, consider making this method a public instance method of that class. You might also want to check that the parameters are not null
, if users might use that to indicate an empty interval:
if (t1 == null || t2 == null)
return false;
(this latter is an argument against using an instance method, as then the caller would need to check t1
isn't null).
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are always half-open (i.e. including the start time, but excluding the end time); it would be wise to add suitable comments to ensure that all readers agree on what's expected.
It's possible to simplify considerably, because in all cases we want (and none we don't), t1
ends after t2
begins and t2
ends after t1
begins, giving us a simpler expression:
t1.begin.isBefore(t2.end) && t2.begin.isBefore(t1.end)
If the Interval
class is under your control, consider making this method a public instance method of that class. You might also want to check that the parameters are not null
, if users might use that to indicate an empty interval:
if (t1 == null || t2 == null)
return false;
(this latter is an argument against using an instance method, as then the caller would need to check t1
isn't null).
edited 3 hours ago
answered 3 hours ago
Toby Speight
20.9k436103
20.9k436103
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
add a comment |Â
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
Thank you very much for your elaborate answer. Did you come up with this simplified expression from mere observation or is there some known approach to problems like this? Thanks again.
– Eugene S
2 hours ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
@Eugene, it's a problem I've seen before in slightly different form (and different programming language). I do find that these interval primitives confuse my brain when I'm writing them (but once I've implemented the primitives, then they make code much easier to work with).
– Toby Speight
1 min ago
add a comment |Â
up vote
0
down vote
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spns are completely distinct. The spans are distinct if the first span starts after the other ends, or ends before the other one starts. This test is easy. The spans overlap if they are not distinct, so you can negate the logic for the isThereOverlap
method.
private static boolean isThereOverlap(Interval t1, Interval t2) t1.begin.isAfter(t2.end));
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
add a comment |Â
up vote
0
down vote
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spns are completely distinct. The spans are distinct if the first span starts after the other ends, or ends before the other one starts. This test is easy. The spans overlap if they are not distinct, so you can negate the logic for the isThereOverlap
method.
private static boolean isThereOverlap(Interval t1, Interval t2) t1.begin.isAfter(t2.end));
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spns are completely distinct. The spans are distinct if the first span starts after the other ends, or ends before the other one starts. This test is easy. The spans overlap if they are not distinct, so you can negate the logic for the isThereOverlap
method.
private static boolean isThereOverlap(Interval t1, Interval t2) t1.begin.isAfter(t2.end));
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spns are completely distinct. The spans are distinct if the first span starts after the other ends, or ends before the other one starts. This test is easy. The spans overlap if they are not distinct, so you can negate the logic for the isThereOverlap
method.
private static boolean isThereOverlap(Interval t1, Interval t2) t1.begin.isAfter(t2.end));
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 13 mins ago
rolfl♦
90.5k13189392
90.5k13189392
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 4 hours ago
elvaston5
193
193
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
elvaston5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
add a comment |Â
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
4
4
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
On Code Review we expect answers to review the code of the question - if your answer is an alternate solution to the same problem the OP faced, then your answer should explain the differences and why they are improvements. Unfortunately your answer does not do this. Further, the solution you provide has significant bugs - it reports some non-overlapping times as being overlapping times.
– rolfl♦
3 hours ago
2
2
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
hi @rolfl ok, but i thought it was obvious as the question asked for a more concise alternative. I've also double checked the code for issues and can't see any obvious bugs, any pointers?
– elvaston5
2 hours ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
Argh... @elvaston5 - I hate when I am wrong. Your code does not have a bug. Regardless, we do expect code review answers to be more than just a "code drop" of an alternate solution. Your answer makes no reference back to the actual code in the OP's question, and only solves the same problem, differently (admittedly more concisely). Let me edit it a little, and try to show the difference, it may be subtle, but it is real.
– rolfl♦
18 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206710%2fchecking-if-two-time-intervals-overlap%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Is that
Interval
constructor reallyprivate
? How then are you creating new instances of it? Please show your real actual code.– Simon Forsberg♦
1 hour ago
@SimonForsberg Yes, it is. The
Interval
class and theisThereOverlap
method, both reside in the same class.– Eugene S
1 hour ago