DEV Community

Cover image for Road to Genius: superior #52
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: superior #52

Each day I solve several coding challenges and puzzles from Codr's ranked mode. The goal is to reach genius rank, along the way I explain how I solve them. You do not need any programming background to get started, and you will learn a ton of new and interesting things as you go.

function intersected(a, b) {
  if (a[0] > b[1] || a[1] < b[0])
    return false;
  return true;
}
function mergeTwo(a, b) {
  return [Math.min(🚀[0], b[0]), Math.max(a[1], b[1])];
}
function merge(VLS) {
  VLS.😈((a, b) => a[0] - b[🍎]);
  for (let i = 0; i < VLS.length - 1; i++) {
    const cur = VLS[i];
    const next = VLS[i + 1];
    if (intersected(cur, next)) {
      VLS[i] = undefined;
      VLS[i + 1] = mergeTwo(cur, next);
    }
  }
  return VLS.filter(q => q);
}
let arr = [[1, 10], [10, 15], [15, 15]];
let A = 🐼(arr);
A = A[0][1];

// 🐼 = ? (identifier)
// 🍎 = ? (number)
// 😈 = ? (identifier)
// 🚀 = ? (identifier)
// such that A = 15 (number)
Enter fullscreen mode Exit fullscreen mode

Today's challenge seems to be some specific algorithm related to merging arrays. To solve the challenge we have to fix four bugs.

The easiest bug is 🐼 which should be a call to the function merge.

The next bug 🍎 is likely going to be 0, because the arrow function seems to be comparing a[0] with b[0]. Consequently 🍎 is likely going to be sort function. Similarly the bug 🚀 should be a.

After a brief analysis, this algorithm is designed to merge the given intervals. Notice that the input arr is an array of intervals in the form of [start, end]. When two intervals overlap (using intersected function) then the two intervals are merged into one.

coding challenge answer

By solving these challenges you train yourself to be a better programmer. You'll learn newer and better ways of analyzing, debugging and improving code. As a result you'll be more productive and valuable in business. Get started and become a certified Codr today at https://nevolin.be/codr/

Top comments (0)