import itertools


def _IntersectIntervals(intervals):
  """Gets the intersection of a set of intervals.

  Args:
    intervals: A list of interval groups, where each interval group is itself
               a list of (start, stop) tuples (ordered by start time and
               non-overlapping).

  Returns:
    An interval group, as a list of (start, stop) tuples, corresponding to the
    intersection (i.e. overlap) of the given |intervals|.
  """
  if not intervals:
    return []

  intersection = []
  indices = [0] * len(intervals)
  lengths = [len(i) for i in intervals]
  while all(i < l for i, l in zip(indices, lengths)):
    current_intervals = [intervals[i][j] for (i, j) in
                         zip(itertools.count(), indices)]
    start = max([s[0] for s in current_intervals])
    end, end_index = min([(e[1], i) for e, i in
                          zip(current_intervals, itertools.count())])
    if start < end:
      intersection.append((start, end))
    indices[end_index] += 1

  return intersection