Learnitweb

Understanding Intervals and Overlapping Intervals

In this tutorial, we will explore the concept of intervals, which are essential in solving many algorithmic and interview problems, particularly those involving time, tasks, or scheduling. One of the most important subtopics in intervals is overlapping intervals, often asked in coding interviews.

1. What is an Interval?

An interval is a range defined by two numbers: a start and an end.

1.1 Example:

  • [5, 8]: This interval starts at 5 and ends at 8.
  • [6, 10]: This interval starts at 6 and ends at 10.

1.2 Use Cases

Intervals are often used in scenarios involving time, such as:

  • Start and end times of meetings
  • Execution time of processes or tasks
  • Time slots in calendars

Each interval can represent a duration.

1.3 Task Time Intervals Example:

Let’s consider tasks and their execution time intervals (assumed in seconds):

TaskStartEnd
A13
B45
C810
D911

These intervals can be visualized on a timeline to understand how tasks are distributed over time.

2. Visualizing Intervals on a Time Axis

Let’s plot the above tasks on a timeline:

Time Axis (Seconds):

0 1 2 3 4 5 6 7 8 9 10 11

A:    [1---3]
B:            [4-5]
C:                    [8---10]
D:                      [9---11]
  • Task A takes 2 seconds: from 1 to 3.
  • Task B takes 1 second: from 4 to 5.
  • Task C takes 2 seconds: from 8 to 10.
  • Task D takes 2 seconds: from 9 to 11.

3. Representing an Interval in Code

We can represent intervals in code using a class structure:

class Interval {
    int start;
    int end;

    Interval(int start, int end) {
        this.start = start;
        this.end = end;
    }
}

Each instance of Interval represents a range from start to end.

4. What are Overlapping Intervals?

Overlapping intervals are intervals that share some part of their range.

Let’s take two intervals:

  • A = [1, 5]
  • B = [2, 6]

When plotted:

luaCopyEditA: [1---5]
B:   [2---6]

These intervals overlap between 2 and 5.

5. Types of Interval Relationships

Let’s explore all possible relationships between two intervals A and B.

Case 1: No Overlap

A and B do not overlap.

Example:

  • A = [1, 3]
  • B = [4, 5]
A: [1---3]
B:       [4---5]

Case 2: Partial Overlap, B Ends After A

B starts before A ends and ends after A ends.

Example:

  • A = [1, 4]
  • B = [3, 6]
A: [1---4]
B:     [3---6]

Case 3: A Completely Overlaps B

Interval A starts before B and ends after B ends.

Example:

  • A = [1, 6]
  • B = [2, 4]
A: [1-------6]
B:    [2---4]

Case 4: B Ends Before A, Still Overlaps

B starts before A ends but ends before A ends.

Example:

  • A = [3, 6]
  • B = [1, 4]
B: [1---4]
A:     [3---6]

Case 5: B Completely Overlaps A

B starts before A and ends after A ends.

Example:

  • A = [2, 4]
  • B = [1, 6]
B: [1-------6]
A:    [2---4]

Case 6: A and B Do Not Overlap (Again)

This is the same as Case 1, but in reverse order.

6. Conclusion

Intervals and overlapping intervals form a core concept for many algorithm problems such as:

  • Merge Intervals
  • Insert Interval
  • Meeting Rooms
  • Interval Scheduling

Understanding how intervals overlap and being able to visualize them on a timeline is critical. In interviews, you’ll often be asked to write logic that merges intervals, detects overlaps, or finds free time slots.