{"id":2164,"date":"2025-01-03T20:11:58","date_gmt":"2025-01-04T01:11:58","guid":{"rendered":"https:\/\/www.unliterate.net\/?p=2164"},"modified":"2025-01-03T20:12:56","modified_gmt":"2025-01-04T01:12:56","slug":"interview-coding-challenge-failure","status":"publish","type":"post","link":"https:\/\/www.unliterate.net\/index.php\/2025\/01\/03\/interview-coding-challenge-failure\/","title":{"rendered":"Interview coding challenge failure&#8230;"},"content":{"rendered":"\n<p>Today I went through a standard coding challenge: &#8220;Create a minesweeper board with variable rows, columns, and bombs&#8221;. The challenge was for 20 minutes, spent 31 minutes, and cannot really figure out why it could never generate.<\/p>\n\n\n\n<p>After my previous posting of &#8220;omg my server didn&#8217;t have virtual memory&#8221; I decided to fiddle it out. With my own fingers this took 12 minutes tops, but the amount of regret I feel is priceless.<\/p>\n\n\n\n<!--more-->\n\n\n\n<pre class=\"wp-block-code\"><code>function makeMinesweeperBoard(rows = 5, cols = 5, mines = 5) {\n  var board = &#91;];\n  \/* Make a blank board with 0s *\/\n  for (let r = 0; r &lt; rows; r++) {\n  \tlet l = new Array();\n  \tfor (let c= 0 ; c &lt; cols; c++) {\n    \tl.push('0');\n    }\n    board.push(l);\n  }\n  \/* Fill board with mines, aka Xs *\/\n  while (mines &gt; 0) {\n  \tlet c = Math.floor(Math.random() * cols);\n    let r = Math.floor(Math.random() * rows);\n    if (board&#91;r]&#91;c] == 'X') { continue; }\n    board&#91;r]&#91;c] = 'X';\n    mines--;\n  }\n  \/* Put the numbers where they need to be *\/\n  for (let r = 0; r &lt; rows; r++) {\n  \tfor (let c = 0; c &lt; cols; c++) {\n    \tif (board&#91;r]&#91;c] == 'X') { continue; }\n    \tlet totalMines = 0;\n      for (let x = -1; x &lt;= 1; x++) {\n      \tfor (let y = -1; y &lt;= 1; y++) {\n        \tif ((r + x) &lt; 0) { continue; }\n          if ((c + y) &lt; 0) { continue; }\n          if ((r + x) &gt;= rows) { continue; }\n          if ((c + y) &gt;= cols) { continue; }\n        \tif (board&#91;r + x]&#91;c + y] == \"X\") {\n          \ttotalMines++;\n          }\n        }\n      }\n      board&#91;r]&#91;c] = totalMines.toString();\n    }\n  }\n  return board;\n}\n\nconsole.log(makeMinesweeperBoard());<\/code><\/pre>\n\n\n\n<p>The first mistake was in board\/array generation. The clue was specifically on the screen in that an array of Objects were created when I needed an array of arrays. I also misused Array.prototype.fill() in my derpiness and ended up on Round 2 just hand-pushing my 0s.<\/p>\n\n\n\n<p>Second mistake was on mine laying. I didn&#8217;t check if the space actually had a mine or not, so when actually generating a specific number of mines it had a 1 in rows*cols chance of duplicating.<\/p>\n\n\n\n<p>Third mistake was in the mine counting. I made my tester aware that it would exceed array bounds and I felt a bit under pressure (since time was ticking down) to remedy it.<\/p>\n\n\n\n<p>Regardless it took me less than 20 minutes to jsfiddle this solution together with my own fingers and console.log()&#8217;s.<\/p>\n\n\n\n<p><a href=\"https:\/\/jsfiddle.net\/mjheick\/7zuh2v3L\/25\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/jsfiddle.net\/mjheick\/7zuh2v3L\/25<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I went through a standard coding challenge: &#8220;Create a minesweeper board with variable rows, columns, and bombs&#8221;. The challenge was for 20 minutes, spent 31 minutes, and cannot really figure out why it could never generate. After my previous posting of &#8220;omg my server didn&#8217;t have virtual memory&#8221; I decided to fiddle it out. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-2164","post","type-post","status-publish","format-standard","hentry","category-geek-instructions"],"_links":{"self":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/comments?post=2164"}],"version-history":[{"count":2,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2164\/revisions"}],"predecessor-version":[{"id":2166,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2164\/revisions\/2166"}],"wp:attachment":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/media?parent=2164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/categories?post=2164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/tags?post=2164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}