Skip to content
Katabench
Try free

Test the Interval Merger

Hard About 30 min Free, no account needed

You're given a correct interval merger. Write the tests that pin overlap, touching, containment, and sorting.

The brief

Merging intervals hides a surprising number of edge cases: overlap, intervals that merely touch, one contained in another, and unsorted input. The merger here is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class IntervalMerger
{
    // Merges overlapping or touching intervals, sorted by start, formatted "1-6,8-10".
    // Empty input returns "".
    public string Merge(int[][] intervals) { ... }
}

Write a Tests class. Each public, parameterless method is one test: it passes if it runs without throwing and fails when an assertion throws. The result is a string, so compare it directly. A tiny assertion library is in scope (no using needed):

C#
Assert.True(condition);          Assert.False(condition);
Assert.Equal(expected, actual);  Assert.NotEqual(a, b);
Assert.Null(x);                  Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);

// Build the input as a jagged array, e.g.:
new IntervalMerger().Merge(new int[][] { new[] { 1, 3 }, new[] { 2, 6 } }); // "1-6"

How you're graded

Your suite is run twice over:

  1. Against the correct IntervalMerger above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken mergers you never see. A mutant is caught when at least one of your tests fails against it.

You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants skip the sort, refuse to merge touching intervals, keep the wrong end, drop the final interval, or compare against the wrong edge. One overlapping pair catches almost none of them. Cover touch, overlap, containment, disjoint, and unsorted input. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `IntervalMerger` class. Each public, parameterless method is one test; it
// passes if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy
// versions, the interesting cases are overlap, intervals that just touch, containment, and sorting.
public class Tests
{
    public void A_single_interval_is_unchanged()
    {
        Assert.Equal("1-3", new IntervalMerger().Merge(new int[][] { new[] { 1, 3 } }));
    }

    // TODO: add overlapping, touching, contained, disjoint, and unsorted inputs.
}

How it is graded

You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.

C# unit testing exercises →

What it practices

Equivalence partitioning
Cover each class of input and every branch with one deliberate case.
Boundary value testing
Pin behavior exactly at, just below and just above every boundary.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas