{"id":261,"date":"2015-04-30T18:11:27","date_gmt":"2015-05-01T00:11:27","guid":{"rendered":"http:\/\/www.managerjs.com\/blog\/?p=261"},"modified":"2015-04-30T18:11:27","modified_gmt":"2015-05-01T00:11:27","slug":"whole-root-challenge","status":"publish","type":"post","link":"https:\/\/www.managerjs.com\/blog\/2015\/04\/whole-root-challenge\/","title":{"rendered":"Whole Root Challenge"},"content":{"rendered":"<p>Interviewed another candidate today. \u00a0A colleague used a challenge based on computing square roots. \u00a0It&#8217;s a very simple problem and still gives a quick feel for a candidate&#8217;s comfort with JavaScript and solving problems with code.<\/p>\n<p>I&#8217;ve added it to the (tiny) suite of interview code challenges. See\u00a0<a href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root\">github.com\/ManagerJS paper-code\/whole-root<\/a>. I hope you find this useful conducting interviews and preparing for interviews.<\/p>\n<p>Here&#8217;s a copy of the challenge for your convenience:<!--more--><\/p>\n<h1>Whole Root<\/h1>\n<p>A simplified approach to finding the square root of a number.<\/p>\n<h2><a id=\"user-content-problem-statement\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#problem-statement\"><\/a>Problem Statement<\/h2>\n<p>Let&#8217;s look at how you solve a fairly straightforward math problem. We&#8217;re going to explore square roots.<\/p>\n<p>Write a function that will take a number <code>x<\/code> and tell you what the square root of that number is.<\/p>\n<p>Here&#8217;s a twist to make the problem a bit simpler: only return the square root if it would be a whole number (0, 1, 2, and so on). If <code>x<\/code> doesn&#8217;t have a whole number for it&#8217;s square root then simply return <code>false<\/code>.<\/p>\n<p>Here are some examples of what your function should return when we call it:<\/p>\n<div class=\"highlight highlight-javascript\">\n<pre>wholeRoot(<span class=\"pl-c1\">4<\/span>); <span class=\"pl-c\">\/\/ returns 2<\/span>\nwholeRoot(<span class=\"pl-c1\">9<\/span>); <span class=\"pl-c\">\/\/ returns 3<\/span>\nwholeRoot(<span class=\"pl-c1\">13<\/span>); <span class=\"pl-c\">\/\/ returns false<\/span>\nwholeRoot(<span class=\"pl-c1\">null<\/span>); <span class=\"pl-c\">\/\/ returns false<\/span>\nwholeRoot(<span class=\"pl-s\"><span class=\"pl-pds\">'<\/span>candybar<span class=\"pl-pds\">'<\/span><\/span>); <span class=\"pl-c\">\/\/ returns false<\/span>\nwholeRoot(<span class=\"pl-c1\">0<\/span>); <span class=\"pl-c\">\/\/ returns 0<\/span>\nwholeRoot(<span class=\"pl-k\">-<\/span><span class=\"pl-c1\">4<\/span>); <span class=\"pl-c\">\/\/ returns false<\/span><\/pre>\n<\/div>\n<h2><a id=\"user-content-suitability\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#suitability\"><\/a>Suitability<\/h2>\n<p>The math in this problem is primary school level. The simplification makes it straightforward to solve with a brute force solution. Several optimizations are possible.<\/p>\n<p>Using basic math and requiring no special computer science approach makes this suitable for a broad range of candidates and shouldn&#8217;t give an unfair advantage to any particular background.<\/p>\n<h2><a id=\"user-content-implementation-quirks\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#implementation-quirks\"><\/a>Implementation Quirks<\/h2>\n<p>The most straightforward solution uses a loop to start <code>i<\/code> at <code>0<\/code> and compute every square on the way to <code>x<\/code>. If <code>i*i === x<\/code> then return <code>i<\/code>.<\/p>\n<p>Candidates should eventually consider checking the type of <code>x<\/code> and immediately returning <code>false<\/code> when <code>x&lt;0<\/code>.<\/p>\n<h2><a id=\"user-content-interview-waypoints\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#interview-waypoints\"><\/a>Interview Waypoints<\/h2>\n<p>State the problem clearly and consistently every time using the <strong>Problem Statement<\/strong> above.<\/p>\n<ul>\n<li>Candidates commonly employ a loop to brute force the solution and they should be able to identify this possibility on their own.<\/li>\n<li>Candidates may be embarrassed to attempt such a brute force technique. You may probe them on their thinking and get around to reassuring them that a brute force solution would be an acceptable starting point.<\/li>\n<li>Candidates may fail to check the input. Point them to the examples showing the function being called with strings,<code>null<\/code>, and negative numbers. See if they add checks to their solution.<\/li>\n<li>Once a brute force solution is employed, probe on how the candidate might make the solution more efficient. They should be able to identify at least some of the following ideas:\n<ul>\n<li>What about exiting the loop as soon as <code>i*i &gt; x<\/code>?<\/li>\n<li>What about performing a binary search rather than incrementing by 1?<\/li>\n<li>with the exception of 0, and 1, the whole root of a number is never greater than <code>x\/2<\/code>. What about terminating when <code>i &gt; x\/2<\/code>?<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><a id=\"user-content-expanding-beyond-js\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#expanding-beyond-js\"><\/a>Expanding Beyond JS<\/h2>\n<p>Once the function has been defined you can quickly probe into their comfort with assembling web pages by asking them to expand their solution into a simple web page.<\/p>\n<h3><a id=\"user-content-problem-statement-1\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#problem-statement-1\"><\/a>Problem Statement:<\/h3>\n<p>Given the function you wrote please put it in it&#8217;s own file. Write the markup for an html page that when viewed in the browser shows two rows: row 1 shows an empty input, row 2 the word <code>false<\/code>. Link your function to the page and write the glue code to make it take the input from row 1 as <code>x<\/code> and display the whole root of <code>x<\/code> in row 2.<\/p>\n<p>Finally, link a CSS file to the page. In the CSS file write a rule that causes the result in row 2 to be green when it&#8217;s a number and red when it&#8217;s <code>false<\/code>.<\/p>\n<h2><a id=\"user-content-implementation-quirks-1\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#implementation-quirks-1\"><\/a>Implementation Quirks<\/h2>\n<p>Expanding the problem into a simple web page gives you the chance to see how comfortable the applicant is making a complete web page. Do they understand the mechanics? Having the syntax memorized isn&#8217;t necessary, but do they know the kinds of tags and attributes required?<\/p>\n<p>You might ask them how they would go about finding the proper syntax. Where would they get their boilerplate from?<\/p>\n<h2><a id=\"user-content-conclusion\" class=\"anchor\" href=\"https:\/\/github.com\/ManagerJS\/paper-code\/tree\/master\/whole-root#conclusion\"><\/a>Conclusion<\/h2>\n<p>The whole root problem isn&#8217;t mathematically intense. It involves enough fundamental concepts to give you a peek into the candidate&#8217;s coding style and speed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interviewed another candidate today. \u00a0A colleague used a challenge based on computing square roots. \u00a0It&#8217;s a very simple problem and still gives a quick feel for a candidate&#8217;s comfort with JavaScript and solving problems with code. I&#8217;ve added it to the (tiny) suite of interview code challenges. See\u00a0github.com\/ManagerJS paper-code\/whole-root. I hope you find this useful&hellip; <a class=\"more-link\" href=\"https:\/\/www.managerjs.com\/blog\/2015\/04\/whole-root-challenge\/\">Continue reading <span class=\"screen-reader-text\">Whole Root Challenge<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"link","meta":{"footnotes":""},"categories":[3],"tags":[56,46,75,76],"class_list":["post-261","post","type-post","status-publish","format-link","hentry","category-applying","tag-hiring","tag-javascript","tag-paper-code","tag-programming-challenge","post_format-post-format-link","wow fadeInUp","entry"],"_links":{"self":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/comments?post=261"}],"version-history":[{"count":1,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/posts\/261\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/media?parent=261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/categories?post=261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.managerjs.com\/blog\/wp-json\/wp\/v2\/tags?post=261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}