Your question is Function Comprehension and Testing. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
A teammate on the calendar view at Asana Spa left this function behind with no tests and a name that says nothing about what it does. It's supposed to take a list of meeting blocks for a day, sorted or not, and merge any that overlap into a single busy block so the calendar UI can render fewer, cleaner rectangles.
function doIt(arr) {
arr.sort();
const result = [];
for (let i = 0; i < arr.length; i++) {
const block = arr[i];
const last = result[result.length - 1];
if (last && block.start <= last.end) {
last.end = Math.max(last.end, block.end);
} else {
result.push(block);
}
}
return result;
}
Tell me what this function actually does, propose a better name for it, and state its time complexity. Then, given the input below, predict exactly what it returns with the current code, and walk me through the test cases you would write to validate (and to catch) this function's behavior.
const meetings = [
{ start: 540, end: 570, title: "Standup" }, // 9:00-9:30
{ start: 570, end: 600, title: "1:1 with manager" }, // 9:30-10:00
{ start: 660, end: 705, title: "Client sync" }, // 11:00-11:45
];
doIt(meetings);