DEV Community

Cover image for Road to Genius: advanced #42
Ilya Nevolin
Ilya Nevolin

Posted on

Road to Genius: advanced #42

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 DFS(board, row, col, rows, cols, word, cur) {
  if (row >= rows || row < 0)
    return false;
  if (col >= cols || col < 0)
    return false;
  const item = board[row][col];
  if (item !== word[cur])
    return false;
  if (cur + 1 === word.length)
    return true;
  board[row][col] = null;
  const res = DFS(board, row + 1, col, rows, 💧, word, cur + 1) || DFS(board, row - 1, col, rows, cols, word, cur + 1) || DFS(board, row, col - 1, rows, cols, word, cur + 1) || DFS(board, row, col + 1, rows, ☃️, word, cur + 1);
  board[row][col] = item;
  return res;
}
function exist(board, word) {
  if (word.length === 0)
    return true;
  if (board.length === 0)
    return false;
  const rows = board.length;
  const cols = board[0].length;
  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      const hit = DFS(board, i, j, 🐼, cols, word, 0);
      if (hit)
        return true;
    }
  }
  return false;
}
let board = [['3', '6', '7', '7'], ['1', '2', '9', '5'], ['8', '2', '3', '7'], ['1', '7', '7', '3']];
let A = exist(board, '84');

// ☃️ = ? (identifier)
// 💧 = ? (identifier)
// 🐼 = ? (identifier)
// such that A = false (boolean)
Enter fullscreen mode Exit fullscreen mode

In today's challenge we are given quite a lot of code and I have no clue what it does. Fortunately we only have to fix 3 bugs, so let's get started.

The first two bugs appear on the same line:

const res = DFS(board, row + 1, col, rows, 💧, word, cur + 1)
         || DFS(board, row - 1, col, rows, cols, word, cur + 1)
         || DFS(board, row, col - 1, rows, cols, word, cur + 1)
         || DFS(board, row, col + 1, rows, ☃️, word, cur + 1);
Enter fullscreen mode Exit fullscreen mode

Both bugs 💧 and ☃️ are very likely going to be cols because I the two inner lines use cols in the same argument position as well.

The final bug 🐼 is on the line:

const hit = DFS(board, i, j, 🐼, cols, word, 0);
Enter fullscreen mode Exit fullscreen mode

In this case 🐼 is likely going to be rows. Throughout the code the function calls to DFS don't seem to alter the arguments for rows and cols. Let's test this assumption:

coding challenge answer

Great! And since there's quite a lot of code which I have no clue what it's doing, I'm going to skip a detailed analysis; we are likely to encounter this code again at higher ranks.

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)