Given "n" appointments, find the conflicting appointments

Pattern: Search, insert and delete

medium

Process appointments in order and keep the earlier ones in an interval tree: a BST ordered by start time where every node also stores the largest end time in its subtree. To check a new appointment, walk down: go left only if something there ends after the new start.

The problem

Appointments [start, end) arrive in order. Return the indexes of the appointments that overlap at least one earlier appointment. Two appointments overlap when each starts before the other ends, so touching ones like [1, 2] and [2, 3] do not. The GFG page prints the clashing pairs; this guide returns the indexes, which is easier to check.

Think first

  • What makes two intervals overlap?
  • Why is a plain BST of start times not enough?
  • What extra value lets you skip a subtree?

Hints

    Brute force, and why it fails

    Compare each appointment with every earlier one: O(n²).

    The key insight

    Storing the maximum end in each subtree tells you which side can still contain an overlap, so each query follows one path.

    Dry run

    conflicts

    [1, 5], [3, 7], [2, 6], [10, 15], [5, 6], [4, 100]. Appointment 0 has nothing before it.

    Step 1 of 3

    The plan

    for each (s, e) in order:
        walk from the root: if node overlaps: conflict
          go left if left.maxEnd > s, else go right
        insert (s, e) by start, updating maxEnd on the way
    

    Complexity

    Time
    O(n h)
    Space
    O(n)

    Each query and insert walks one path; a balanced tree makes it O(n log n).

    Edge cases

    • One appointment.
    • Touching appointments do not clash.
    • One long appointment overlapping everything.
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        private static class Node {                  // an interval tree: a BST by start, with the largest end below each node
            int start, end, maxEnd;
            Node left, right;
            Node(int s, int e) { start = s; end = e; maxEnd = e; }
        }
    
        public List<Integer> conflicting(int[][] appointments) {
            List<Integer> out = new ArrayList<>();
            Node root = null;
            for (int i = 0; i < appointments.length; i++) {
                int s = appointments[i][0], e = appointments[i][1];
                if (overlaps(root, s, e)) out.add(i);    // clashes with an earlier one
                root = insert(root, s, e);
            }
            return out;
        }
    
        private boolean overlaps(Node n, int s, int e) {
            while (n != null) {
                if (n.start < e && s < n.end) return true;
                if (n.left != null && n.left.maxEnd > s) n = n.left;   // something on the left still ends after s
                else n = n.right;
            }
            return false;
        }
    
        private Node insert(Node n, int s, int e) {
            if (n == null) return new Node(s, e);
            if (s < n.start) n.left = insert(n.left, s, e);
            else n.right = insert(n.right, s, e);
            n.maxEnd = Math.max(n.maxEnd, e);
            return n;
        }
    }

    C++

    class Solution {
    public:
        struct Node {                                // an interval tree: a BST by start, with the largest end below each node
            int start, end, maxEnd;
            Node* left;
            Node* right;
            Node(int s, int e) : start(s), end(e), maxEnd(e), left(NULL), right(NULL) {}
        };
    
        vector<int> conflicting(vector<vector<int>>& appointments) {
            vector<int> out;
            Node* root = NULL;
            for (int i = 0; i < static_cast<int>(appointments.size()); i++) {
                int s = appointments[i][0], e = appointments[i][1];
                if (overlaps(root, s, e)) out.push_back(i);   // clashes with an earlier one
                root = insert(root, s, e);
            }
            return out;
        }
    
        bool overlaps(Node* n, int s, int e) {
            while (n != NULL) {
                if (n->start < e && s < n->end) return true;
                if (n->left != NULL && n->left->maxEnd > s) n = n->left;   // something on the left still ends after s
                else n = n->right;
            }
            return false;
        }
    
        Node* insert(Node* n, int s, int e) {
            if (n == NULL) return new Node(s, e);
            if (s < n->start) n->left = insert(n->left, s, e);
            else n->right = insert(n->right, s, e);
            n->maxEnd = max(n->maxEnd, e);
            return n;
        }
    };

    Python

    class Solution:
        def conflicting(self, appointments: list[list[int]]) -> list[int]:
            # an interval tree: a BST by start; each node is [start, end, maxEnd, left, right]
            root, out = None, []
    
            def overlaps(n, s, e) -> bool:
                while n:
                    if n[0] < e and s < n[1]:
                        return True
                    n = n[3] if n[3] and n[3][2] > s else n[4]   # left only if something there ends after s
                return False
    
            def insert(n, s, e):
                if n is None:
                    return [s, e, e, None, None]
                if s < n[0]:
                    n[3] = insert(n[3], s, e)
                else:
                    n[4] = insert(n[4], s, e)
                n[2] = max(n[2], e)
                return n
    
            for i, (s, e) in enumerate(appointments):
                if overlaps(root, s, e):
                    out.append(i)                   # clashes with an earlier one
                root = insert(root, s, e)
            return out

    Common mistakes

    • Forgetting to update maxEnd on insertion.
    • Treating touching intervals as overlapping.

    Similar problems