A place to cache linked articles (think custom and personal wayback machine)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.md 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. title: Interviewing for a JavaScript Job
  2. url: http://raganwald.com/2015/02/21/interviewing-for-a-front-end-job.html
  3. hash_url: 8b2231129eb55375d546f931b81c06f1
  4. <p>“The Carpenter” was a JavaScript programmer, well-known for a meticulous attention to detail and love for hand-crafted, exquisitely joined code. The Carpenter normally worked through personal referrals, but from time to time a recruiter would slip through his screen. One such recruiter was Bob Plissken. Bob was well-known in the Python community, but his clients often needed experience with other languages.</p>
  5. <p>Plissken lined up a technical interview with a well-funded startup in San Francisco. The Carpenter arrived early for his meeting with “Thing Software,” and was shown to conference room 13. A few minutes later, he was joined by one of the company’s developers, Christine.</p>
  6. <h3 id="the-problem">the problem</h3>
  7. <p>After some small talk, Christine explained that they liked to ask candidates to whiteboard some code. Despite his experience and industry longevity, the Carpenter did not mind being asked to demonstrate that he was, in fact, the person described on the resumé.</p>
  8. <p>Many companies use white-boarding code as an excuse to have a technical conversation with a candidate, and The Carpenter felt that being asked to whiteboard code was an excuse to have a technical conversation with a future colleague. “Win, win” he thought to himself.</p>
  9. <p><a href="https://www.flickr.com/photos/stigrudeholm/6710684795"><img src="/assets/images/chessboard.jpg" alt="Chessboard"/></a></p>
  10. <p>Christine intoned the question, as if by rote:</p>
  11. <blockquote>
  12. <p>Consider a finite checkerboard of unknown size. On each square, we randomly place an arrow pointing to one of its four sides. A chequer is placed randomly on the checkerboard. Each move consists of moving the chequer one square in the direction of the arrow in the square it occupies. If the arrow should cause the chequer to move off the edge of the board, the game halts.</p>
  13. </blockquote>
  14. <blockquote>
  15. <p>The problem is this: The game board is hidden from us. A player moves the chequer, following the rules. As the player moves the chequer, they calls out the direction of movement, e.g. “↑, →, ↑, ↓, ↑, →…” Write an algorithm that will determine whether the game halts, strictly from the called out directions, in finite time and space.</p>
  16. </blockquote>
  17. <p>“So,” The Carpenter asked, “I am to write an algorithm that takes a possibly infinite stream of…”</p>
  18. <p>Christine interrupted. “To save time, we have written a template of the solution for you in ECMASCript 2015 notation. Fill in the blanks. Your code should not presume anything about the gameboard’s size or contents, only that it is given an arrow every time though the while loop. You may use <a href="http://babeljs.io">babeljs.io</a>, or <a href="http://www.es6fiddle.net">ES6Fiddle</a> to check your work. “</p>
  19. <p>Christine quickly scribbled on the whiteboard:</p>
  20. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">Game</span> <span class="o">=</span> <span class="p">(</span><span class="nx">size</span> <span class="o">=</span> <span class="mi">8</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  21. <span class="c1">// initialize the board</span>
  22. <span class="kr">const</span> <span class="nx">board</span> <span class="o">=</span> <span class="p">[];</span>
  23. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
  24. <span class="nx">board</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[];</span>
  25. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">;</span> <span class="o">++</span><span class="nx">j</span><span class="p">)</span> <span class="p">{</span>
  26. <span class="nx">board</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'←→↓↑'</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)];</span>
  27. <span class="p">}</span>
  28. <span class="p">}</span>
  29. <span class="c1">// initialize the position</span>
  30. <span class="kd">let</span> <span class="nx">initialPosition</span> <span class="o">=</span> <span class="p">[</span>
  31. <span class="mi">2</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="nx">size</span> <span class="o">-</span> <span class="mi">4</span><span class="p">)),</span>
  32. <span class="mi">2</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="nx">size</span> <span class="o">-</span> <span class="mi">4</span><span class="p">))</span>
  33. <span class="p">];</span>
  34. <span class="c1">// ???</span>
  35. <span class="kd">let</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]</span> <span class="o">=</span> <span class="nx">initialPosition</span><span class="p">;</span>
  36. <span class="kr">const</span> <span class="nx">MOVE</span> <span class="o">=</span> <span class="p">{</span>
  37. <span class="s2">"←"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  38. <span class="s2">"→"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  39. <span class="s2">"↓"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span>
  40. <span class="s2">"↑"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
  41. <span class="p">};</span>
  42. <span class="k">while</span> <span class="p">(</span><span class="nx">x</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">y</span> <span class="o">&gt;=</span><span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">x</span> <span class="o">&lt;</span> <span class="nx">size</span> <span class="o">&amp;&amp;</span> <span class="nx">y</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">)</span> <span class="p">{</span>
  43. <span class="kr">const</span> <span class="nx">arrow</span> <span class="o">=</span> <span class="nx">board</span><span class="p">[</span><span class="nx">x</span><span class="p">][</span><span class="nx">y</span><span class="p">];</span>
  44. <span class="c1">// ???</span>
  45. <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]</span> <span class="o">=</span> <span class="nx">MOVE</span><span class="p">[</span><span class="nx">arrow</span><span class="p">]([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]);</span>
  46. <span class="p">}</span>
  47. <span class="c1">// ???</span>
  48. <span class="p">};</span></code></pre></div>
  49. <p>“What,” Christine asked, “Do you write in place of the three <code>// ???</code> placeholders to determine whether the game halts?”</p>
  50. <h3 id="the-carpenters-solution">the carpenter’s solution</h3>
  51. <p>The Carpenter was not surprised at the problem. Bob Plissken was a crafty, almost reptilian recruiter that traded in information and secrets. Whenever Bob sent a candidate to a job interview, he debriefed them afterwards and got them to disclose what questions were asked in the interview. He then coached subsequent candidates to give polished answers to the company’s pet technical questions.</p>
  52. <p>And just as companies often pick a problem that gives them broad latitude for discussing alternate approaches and determining that depth of a candidate’s experience, The Carpenter liked to sketch out solutions that provided an opportunity to judge the interviewer’s experience and provide an easy excuse to discuss the company’s approach to software design.</p>
  53. <p>Bob had, in fact, warned The Carpenter that “Thing” liked to ask either or both of two questions: Determine how to detect a loop in a linked list, and determine whether the chequerboard game would halt. To save time, The Carpenter had prepared the same answer for both questions.</p>
  54. <p>The Carpenter coughed softly, then began. “To begin with, I’ll transform a game into an iterable that generates arrows, using the ‘Starman’ notation for generators.”</p>
  55. <p>“I will add just five lines of code the <code>Game</code> function, and two of those are closing braces:”</p>
  56. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="k">return</span> <span class="p">({</span>
  57. <span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="o">:</span> <span class="kd">function</span><span class="o">*</span> <span class="p">()</span> <span class="p">{</span></code></pre></div>
  58. <p>And:</p>
  59. <p>And:</p>
  60. <p>“The finished function reads:”</p>
  61. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">Game</span> <span class="o">=</span> <span class="p">(</span><span class="nx">size</span> <span class="o">=</span> <span class="mi">8</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  62. <span class="c1">// initialize the board</span>
  63. <span class="kr">const</span> <span class="nx">board</span> <span class="o">=</span> <span class="p">[];</span>
  64. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
  65. <span class="nx">board</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="p">[];</span>
  66. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">;</span> <span class="o">++</span><span class="nx">j</span><span class="p">)</span> <span class="p">{</span>
  67. <span class="nx">board</span><span class="p">[</span><span class="nx">i</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'←→↓↑'</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)];</span>
  68. <span class="p">}</span>
  69. <span class="p">}</span>
  70. <span class="c1">// initialize the position</span>
  71. <span class="kd">let</span> <span class="nx">initialPosition</span> <span class="o">=</span> <span class="p">[</span>
  72. <span class="mi">2</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="nx">size</span> <span class="o">-</span> <span class="mi">4</span><span class="p">)),</span>
  73. <span class="mi">2</span> <span class="o">+</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="nx">size</span> <span class="o">-</span> <span class="mi">4</span><span class="p">))</span>
  74. <span class="p">];</span>
  75. <span class="k">return</span> <span class="p">({</span>
  76. <span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="o">:</span> <span class="kd">function</span><span class="o">*</span> <span class="p">()</span> <span class="p">{</span>
  77. <span class="kd">let</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]</span> <span class="o">=</span> <span class="nx">initialPosition</span><span class="p">;</span>
  78. <span class="kr">const</span> <span class="nx">MOVE</span> <span class="o">=</span> <span class="p">{</span>
  79. <span class="s2">"←"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  80. <span class="s2">"→"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  81. <span class="s2">"↓"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span>
  82. <span class="s2">"↑"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
  83. <span class="p">};</span>
  84. <span class="k">while</span> <span class="p">(</span><span class="nx">x</span> <span class="o">&gt;=</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">y</span> <span class="o">&gt;=</span><span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">x</span> <span class="o">&lt;</span> <span class="nx">size</span> <span class="o">&amp;&amp;</span> <span class="nx">y</span> <span class="o">&lt;</span> <span class="nx">size</span><span class="p">)</span> <span class="p">{</span>
  85. <span class="kr">const</span> <span class="nx">arrow</span> <span class="o">=</span> <span class="nx">board</span><span class="p">[</span><span class="nx">x</span><span class="p">][</span><span class="nx">y</span><span class="p">];</span>
  86. <span class="k">yield</span> <span class="nx">arrow</span><span class="p">;</span>
  87. <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]</span> <span class="o">=</span> <span class="nx">MOVE</span><span class="p">[</span><span class="nx">arrow</span><span class="p">]([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]);</span>
  88. <span class="p">}</span>
  89. <span class="p">}</span>
  90. <span class="p">});</span>
  91. <span class="p">};</span></code></pre></div>
  92. <p>“Now that we have an iterable, we can transform the iterable of arrows into an iterable of positions.” The Carpenter sketched quickly. “We’ll need some common utilities. You’ll find equivalents in a number of JavaScript libraries, but I’ll quote those given in <a href="https://leanpub.com/javascriptallongesix">JavaScript Allongé</a>:”</p>
  93. <p>“For starters, <code>takeIterable</code> transforms an iterable into one that yields at most a fixed number of elements. It’s handy for debugging. We’ll use it to check that our <code>Game</code> is working as an iterable:”</p>
  94. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">takeIterable</span> <span class="o">=</span> <span class="p">(</span><span class="nx">numberToTake</span><span class="p">,</span> <span class="nx">iterable</span><span class="p">)</span> <span class="o">=&gt;</span>
  95. <span class="p">({</span>
  96. <span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="o">:</span> <span class="kd">function</span><span class="o">*</span> <span class="p">()</span> <span class="p">{</span>
  97. <span class="kd">let</span> <span class="nx">remainingElements</span> <span class="o">=</span> <span class="nx">numberToTake</span><span class="p">;</span>
  98. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">element</span> <span class="nx">of</span> <span class="nx">iterable</span><span class="p">)</span> <span class="p">{</span>
  99. <span class="k">if</span> <span class="p">(</span><span class="nx">remainingElements</span><span class="o">--</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
  100. <span class="k">yield</span> <span class="nx">element</span><span class="p">;</span>
  101. <span class="p">}</span>
  102. <span class="p">}</span>
  103. <span class="p">});</span></code></pre></div>
  104. <p>“This doesn’t actually end up in our solution, it’s just to check our work as we go along. And you can find it in libraries, it’s not something we need to reinvent whenever we work with iterables.”</p>
  105. <p>“But now to the business. We want to take the arrows and convert them to positions. For that, we’ll map the Game iterable to positions. A <code>statefulMap</code> is a lazy map that preserves state from iteration to iteration. That’s what we need, because we need to know the current position to map each move to the next position.”</p>
  106. <p>“Again, this is a standard idiom we can obtain from libraries, we don’t reinvent the wheel. I’ll show it here for clarity:”</p>
  107. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">statefulMapIterableWith</span> <span class="o">=</span> <span class="p">(</span><span class="nx">fn</span><span class="p">,</span> <span class="nx">seed</span><span class="p">,</span> <span class="nx">iterable</span><span class="p">)</span> <span class="o">=&gt;</span>
  108. <span class="p">({</span>
  109. <span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]</span><span class="o">:</span> <span class="kd">function</span><span class="o">*</span> <span class="p">()</span> <span class="p">{</span>
  110. <span class="kd">let</span> <span class="nx">value</span><span class="p">,</span>
  111. <span class="nx">state</span> <span class="o">=</span> <span class="nx">seed</span><span class="p">;</span>
  112. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">element</span> <span class="nx">of</span> <span class="nx">iterable</span><span class="p">)</span> <span class="p">{</span>
  113. <span class="p">[</span><span class="nx">state</span><span class="p">,</span> <span class="nx">value</span><span class="p">]</span> <span class="o">=</span> <span class="nx">fn</span><span class="p">(</span><span class="nx">state</span><span class="p">,</span> <span class="nx">element</span><span class="p">);</span>
  114. <span class="k">yield</span> <span class="nx">value</span><span class="p">;</span>
  115. <span class="p">}</span>
  116. <span class="p">}</span>
  117. <span class="p">});</span></code></pre></div>
  118. <p>“Armed with this, it’s straightforward to map an iterable of directions to an iterable of strings representing positions:”</p>
  119. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">positionsOf</span> <span class="o">=</span> <span class="p">(</span><span class="nx">game</span><span class="p">)</span> <span class="o">=&gt;</span>
  120. <span class="nx">statefulMapIterableWith</span><span class="p">(</span>
  121. <span class="p">(</span><span class="nx">position</span><span class="p">,</span> <span class="nx">direction</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  122. <span class="kr">const</span> <span class="nx">MOVE</span> <span class="o">=</span> <span class="p">{</span>
  123. <span class="s2">"←"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  124. <span class="s2">"→"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="nx">y</span><span class="p">],</span>
  125. <span class="s2">"↓"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">-</span> <span class="mi">1</span><span class="p">],</span>
  126. <span class="s2">"↑"</span><span class="o">:</span> <span class="p">([</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">])</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
  127. <span class="p">};</span>
  128. <span class="kr">const</span> <span class="p">[</span><span class="nx">x</span><span class="p">,</span> <span class="nx">y</span><span class="p">]</span> <span class="o">=</span> <span class="nx">MOVE</span><span class="p">[</span><span class="nx">direction</span><span class="p">](</span><span class="nx">position</span><span class="p">);</span>
  129. <span class="k">return</span> <span class="p">[</span><span class="nx">position</span><span class="p">,</span> <span class="err">`</span><span class="nx">x</span><span class="o">:</span> <span class="nx">$</span><span class="p">{</span><span class="nx">x</span><span class="p">},</span> <span class="nx">y</span><span class="o">:</span> <span class="nx">$</span><span class="p">{</span><span class="nx">y</span><span class="p">}</span><span class="err">`</span><span class="p">];</span>
  130. <span class="p">},</span>
  131. <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
  132. <span class="nx">game</span><span class="p">);</span></code></pre></div>
  133. <p>The Carpenter reflected. “Having turned our game loop into an iterable, we can now see that our problem of whether the game terminates is isomorphic to the problem of detecting whether the positions given ever repeat themselves: If the chequer ever returns to a position it has previously visited, it will cycle endlessly.”</p>
  134. <p>“We could draw positions as nodes in a graph, connected by arcs representing the arrows. Detecting whether the game terminates is equivalent to detecting whether the graph contains a cycle.”</p>
  135. <p><img src="/assets/images/cycle.png" alt="Cycle Detection"/></p>
  136. <p>“There’s an old joke that a mathematician is someone who will take a five-minute problem, then spend an hour proving it is equivalent to another problem they have already solved. I approached this question in that spirit. Now that we have created an iterable of values that can be compared with <code>===</code>, I can show you this function:”</p>
  137. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="c1">// implements Tortoise and Hare cycle</span>
  138. <span class="c1">// detection algorithm.</span>
  139. <span class="kr">const</span> <span class="nx">hasCycle</span> <span class="o">=</span> <span class="p">(</span><span class="nx">orderedCollection</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  140. <span class="kr">const</span> <span class="nx">hare</span> <span class="o">=</span> <span class="nx">orderedCollection</span><span class="p">[</span><span class="nx">Symbol</span><span class="p">.</span><span class="nx">iterator</span><span class="p">]();</span>
  141. <span class="kd">let</span> <span class="nx">hareResult</span> <span class="o">=</span> <span class="p">(</span><span class="nx">hare</span><span class="p">.</span><span class="nx">next</span><span class="p">(),</span> <span class="nx">hare</span><span class="p">.</span><span class="nx">next</span><span class="p">());</span>
  142. <span class="k">for</span> <span class="p">(</span><span class="kd">let</span> <span class="nx">tortoiseValue</span> <span class="nx">of</span> <span class="nx">orderedCollection</span><span class="p">)</span> <span class="p">{</span>
  143. <span class="nx">hareResult</span> <span class="o">=</span> <span class="nx">hare</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
  144. <span class="k">if</span> <span class="p">(</span><span class="nx">hareResult</span><span class="p">.</span><span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
  145. <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
  146. <span class="p">}</span>
  147. <span class="k">if</span> <span class="p">(</span><span class="nx">tortoiseValue</span> <span class="o">===</span> <span class="nx">hareResult</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
  148. <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
  149. <span class="p">}</span>
  150. <span class="nx">hareResult</span> <span class="o">=</span> <span class="nx">hare</span><span class="p">.</span><span class="nx">next</span><span class="p">();</span>
  151. <span class="k">if</span> <span class="p">(</span><span class="nx">hareResult</span><span class="p">.</span><span class="nx">done</span><span class="p">)</span> <span class="p">{</span>
  152. <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
  153. <span class="p">}</span>
  154. <span class="k">if</span> <span class="p">(</span><span class="nx">tortoiseValue</span> <span class="o">===</span> <span class="nx">hareResult</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
  155. <span class="k">return</span> <span class="kc">true</span><span class="p">;</span>
  156. <span class="p">}</span>
  157. <span class="p">}</span>
  158. <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
  159. <span class="p">};</span></code></pre></div>
  160. <p>“A long time ago,” The Carpenter explained, “Someone asked me a question in an interview. I have never forgotten the question, or the general form of the solution. The question was, <em>Given a linked list, detect whether it contains a cycle. Use constant space.</em>”</p>
  161. <p>“This is, of course, the most common solution, it is <a href="https://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare">Floyd’s cycle-finding algorithm</a>, although there is some academic dispute as to whether Robert Floyd actually discovered it or was misattributed by Knuth.”</p>
  162. <p>“Thus, the solution to the game problem is:”</p>
  163. <div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">const</span> <span class="nx">terminates</span> <span class="o">=</span> <span class="p">(</span><span class="nx">game</span><span class="p">)</span> <span class="o">=&gt;</span>
  164. <span class="o">!</span><span class="nx">hasCycle</span><span class="p">(</span><span class="nx">positionsOf</span><span class="p">(</span><span class="nx">game</span><span class="p">))</span></code></pre></div>
  165. <p>“This solution makes use of iterables and a single utility function, <code>statefulMapIterableWith</code>. It also cleanly separates the mechanics of the game from the algorithm for detecting cycles in a graph.”</p>
  166. <h3 id="the-aftermath">the aftermath</h3>
  167. <p>The Carpenter sat down and waited. This type of solution provided an excellent opportunity to explore lazy versus eager evaluation, the performance of iterators versus native iteration, single responsibility design, and many other rich topics.</p>
  168. <p>The Carpenter was confident that although nobody would write this exact code in production, prospective employers would also recognize that nobody would try to detect whether a chequer game terminates in production, either. It’s all just a pretext for kicking off an interesting conversation, right?</p>
  169. <p><a href="https://www.flickr.com/photos/jlhopgood/6795353385"><img src="/assets/images/time.jpg" alt="Time"/></a></p>
  170. <p>Christine looked at the solution on the board, frowned, and glanced at the clock on the wall. “<em>Well, where has the time gone?</em>”</p>
  171. <p>“We at the Thing Software company are very grateful you made some time to visit with us, but alas, that is all the time we have today. If we wish to talk to you further, we’ll be in touch.”</p>
  172. <p>The Carpenter never did hear back from them, but the next day there was an email containing a generous contract from Friends of Ghosts (“FOG”), a codename for a stealth startup doing interesting work, and the Thing interview was forgotten.</p>
  173. <p>Some time later, The Carpenter ran into Bob Plissken at a local technology meet-up. “John! What happened at Thing?” Bob wanted to know, “I asked them what they thought of you, and all they would say was, <em>Writes unreadable code</em>. I thought it was a lock! I thought you’d finally make your escape from New York.”</p>
  174. <p>The Carpenter smiled. “I forgot about them, it’s been a while. So, do They Live?”</p>
  175. <hr/>
  176. <p>p.s. <a href="http://raganwald.com/2015/02/23/the-last-word-on-interviewing.html">(unlikely to be) The Last Word on Interviewing for a JavaScript Job</a></p>
  177. <p>p.p.s. The Carpenter probably cribbed the solution from <a href="http://raganwald.com/2013/02/17/a-drunken-walk.html">The “Drunken Walk” Programming Problem</a>, and <a href="http://raganwald.com/2013/02/18/drunken-walk-solution.html">Solving the “Drunken Walk” problem with iterators</a>.</p>