Question bank / Software Engineering, Coding, Intermediate

How to answer: Merge overlapping activity intervals and return the total active time.

Whether you sort first and keep the merging logic tidy.

What the interviewer is testing.

Sorting, interval invariants, complexity. Whether you sort first and keep the merging logic tidy.

An outline for this question.

  1. Sort the intervals by start time.
  2. Walk through them, keeping a current merged interval. If the next one overlaps, extend its end; if not, close it and start a new one.
  3. Add up the lengths of the merged intervals for the total active time.
  4. State O(n log n) for the sort and O(n) after it, and test touching intervals and one interval inside another.

The mistake to avoid.

Skipping the sort. Without it, overlaps that are far apart in the list are missed.

The general method: clarify, plan, code, test.

The same shape works for most coding questions, whatever the topic.

  • Restate the problem and ask about inputs, size limits and edge cases before you write anything.
  • Describe your approach and its complexity out loud, then check it makes sense with the interviewer.
  • Write clean code in small steps, narrating as you go.
  • Walk through a normal case and an edge case by hand, then say what you would test.

Practice it, out loud.

Reading an outline is not the same as saying it. Answer this question in a mock interview, in your own words, then compare it with the outline above.

Practice this questionSee live interview help

This is a Replysis practice question. It is not a leaked or official question from any company, and the outline is a guide to structure, not a script. Use your own experience and facts.