Step through the algorithm visually — use Play or the step buttons (inspired by AlgoMaster / visualgo).
Airbnb interview context: Merge Intervals is a Medium Intervals problem — Walk through the animated steps, then implement stdin/stdout solution.
Use the animation above to step through each move before writing code.
Pattern: Intervals
Read from stdin, write to stdout. Classic interview problem #56.
Merge Intervals — Airbnb interview prep · Intervals
Classic interview problem #56.
Input (stdin)
Line 1: n\nNext n lines: start end
Output (stdout)
Count after merging
Your program must read from stdin and write the answer to stdout (no extra debug text).
4 1 3 2 6 8 10 15 18
3
| Test | Status | Details |
|---|
Ready — edit the code above and click Run or Submit.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static int[] Ria(string line = null)
{
line ??= Console.ReadLine();
if (string.IsNullOrWhiteSpace(line)) return Array.Empty<int>();
return line.Trim().Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries)
.Select(int.Parse).ToArray();
}
static string[] Rsa()
{
int n = int.Parse(Console.ReadLine());
var arr = new string[n];
for (int i = 0; i < n; i++) arr[i] = Console.ReadLine();
return arr;
}
static void W(params object[] parts) => Console.WriteLine(string.Join(" ", parts));
static void Wb(bool v) => Console.WriteLine(v ? "true" : "false");
static void Wi(int v) => Console.WriteLine(v);
static void Ws(string v) => Console.WriteLine(v);
static void Main()
{
int n = int.Parse(Console.ReadLine());
var list = new List<int[]>();
for (int i = 0; i < n; i++) list.Add(Ria());
list.Sort((a, b) => a[0].CompareTo(b[0]));
var merged = new List<int[]>();
foreach (var iv in list) {
if (merged.Count == 0 || merged[^1][1] < iv[0]) merged.Add(iv);
else merged[^1][1] = Math.Max(merged[^1][1], iv[1]);
}
Wi(merged.Count);
}
}
Try solving on your own first, then reveal the official answer.