Skip to content
← All answers
AmeriGA · AI answers archive

A function that merges overlapping intervals

Asked on November 20, 2026

What the AIs say

All solutions correctly implement the algorithm for merging intervals. However, Gemini 3.1 Flash Lite, Gemini 3.5 Flash, Gemini 3.8 Flash, and Nemotron 3 Ultra mutate the input array elements in place, which can cause subtle side effects; ERNIE 5.5 Flash is the cleanest solution because it avoids mutating the input data.

Each AI's answer

Gemini 3.1 Flash Lite

Google

The function first sorts the intervals by their start times. It then initializes the result array with the first interval. Iterating through the sorted list, it checks if the current interval's start time is less than or equal to the previous interval's end time. If they overlap, it merges them by updating the end time. Otherwise, it appends the current interval to the list.

function solve(intervals) {
  if (intervals.length <= 1) return intervals;

  // Sort by start time
  intervals.sort((a, b) => a[0] - b[0]);

  const merged = [intervals[0]];

  for (let i = 1; i < intervals.length; i++) {
    const current = intervals[i];
    const lastMerged = merged[merged.length - 1];

    // If overlap exists (start <= previous end)
    if (current[0] <= lastMerged[1]) {
      lastMerged[1] = Math.max(lastMerged[1], current[1]);
    } else {
      // No overlap, push interval
      merged.push(current);
    }
  }

  return merged;
}

Gemini 3.5 Flash

Google

The function first sorts the array of intervals based on their start values. It then iterates through the sorted intervals, comparing each interval with the last interval in the result array. If the current interval overlaps with the last one (its start is less than or equal to the last one's end), it merges them by updating the end boundary. Otherwise, it adds the current interval as a new entry.

function merge(intervals) {
  if (!intervals.length) return [];
  intervals.sort((a, b) => a[0] - b[0]);
  const merged = [intervals[0]];
  for (let i = 1; i < intervals.length; i++) {
    const current = intervals[i];
    const last = merged[merged.length - 1];
    if (current[0] <= last[1]) {
      last[1] = Math.max(last[1], current[1]);
    } else {
      merged.push(current);
    }
  }
  return merged;
}

Gemini 3.8 Flash

Google

The function first sorts intervals by their start times. It initializes the result array with the first interval. Then, it iterates through the remaining intervals, comparing each with the last merged interval. If the current start is within the bounds of the previous end, it updates the previous end to the maximum of both. Otherwise, it appends the current interval as a new non-overlapping range.

function merge(intervals) {
  if (!intervals || intervals.length <= 1) return intervals || [];
  
  const sorted = intervals.slice().sort((a, b) => a[0] - b[0]);
  const result = [sorted[0]];
  
  for (let i = 1; i < sorted.length; i++) {
    const current = sorted[i];
    const lastMerged = result[result.length - 1];
    
    if (current[0] <= lastMerged[1]) {
      lastMerged[1] = Math.max(lastMerged[1], current[1]);
    } else {
      result.push(current);
    }
  }
  
  return result;
}

Nemotron 3 Ultra

NVIDIA

Sort intervals by start, then iterate merging overlapping intervals by extending the end while the next start is within the current interval.

function mergeIntervals(intervals) {
  if (!intervals || intervals.length === 0) return [];
  // Sort intervals by start value
  intervals.sort((a, b) => a[0] - b[0]);
  const merged = [];
  let current = intervals[0];
  for (let i = 1; i < intervals.length; i++) {
    const next = intervals[i];
    if (next[0] <= current[1]) {
      // Overlapping: merge by extending the end
      current[1] = Math.max(current[1], next[1]);
    } else {
      // No overlap: push current and move to next
      merged.push(current);
      current = next;
    }
  }
  merged.push(current);
  return merged;
}

ERNIE 5.5 Flash ★

Baidu

Sorts intervals by start, then merges any interval whose start is at or before the current interval's end. Returns the merged intervals as an array of start/end pairs.

function solve(intervals) {
  if (!Array.isArray(intervals) || intervals.length === 0) return [];

  const sorted = intervals
    .map(([start, end]) => [start, end])
    .sort((a, b) => a[0] - b[0] || a[1] - b[1]);

  const merged = [];

  for (const [start, end] of sorted) {
    const last = merged[merged.length - 1];

    if (!last || start > last[1]) {
      merged.push([start, end]);
    } else if (end > last[1]) {
      last[1] = end;
    }
  }

  return merged;
}

Ask your own question

Ask anything: many AIs answer at once, side by side.

Ask your own question