DEV Community

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

Posted on

1

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/

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay